PathFindNextComponentW 関数 (shlwapi.h)

パスを解析し、最初の円記号に続くパスの部分を返します。

構文

LPCWSTR PathFindNextComponentW(
  [in] LPCWSTR pszPath
);

パラメーター

[in] pszPath

種類: PTSTR

解析するパスを含む null で終わる文字列へのポインター。 この文字列は、MAX_PATH文字と終端の null 文字を超えてはなりません。 パス コンポーネントは円記号で区切られます。 たとえば、パス "c:\path1\path2\file.txt" には、c:、path1、path2、file.txt の 4 つのコンポーネントがあります。

戻り値

種類: PTSTR

切り捨てられたパスを含む null で終わる文字列へのポインターを返します。

pszPath がパス内の最後のコンポーネントを指している場合、この関数は終端の null 文字へのポインターを返します。

pszPath が終端の null 文字を指している場合、または呼び出しが失敗した場合、この関数は NULL を返します。

解説

PathFindNextComponent は、円記号 ("\") が発生するまでパス文字列を歩き、円記号を含むその時点までのすべてを無視し、パスの残りの部分を返します。 したがって、パスが円記号 (\path1\path2 など) で始まる場合、関数は最初の円記号を削除し、残り (path1\path2) を返します。

次の単純なコンソール アプリケーションは、 PathFindNextComponent にさまざまな文字列を渡して、関数がパス コンポーネントとして認識するものを示し、返される内容を示します。 Visual Studio でこのコードを実行するには、Shlwapi.lib にリンクし、プロジェクト設定のプリプロセッサ コマンドで UNICODE を定義する必要があります。


#include <windows.h>
#include <iostream>
#include <shlwapi.h>

#pragma comment(lib, "shlwapi.lib")     // Link to this file.

int main()
{
    using namespace std;
   
    PCWSTR path = L"c:\\path1\\path2\\file.txt";
 
    // This loop passes a full path to PathFindNextComponent and feeds the 
    // results of that call back into the function until the entire path has
    // been walked.
    while (path)
    {
        PCWSTR oldPath = path;
        path = PathFindNextComponent(path);
 
        // The path variable pointed to the terminating null character.
        if (path == NULL)
        {
            wcout << L"The terminating null character returns NULL\n\n";
        }
        // The path variable pointed to a path with only one component.
		else if (*path == 0)
        {
            wcout << L"The path " << oldPath 
                  << L" returns a pointer to the terminating null character\n"; 
        }
        else 
        {
            wcout << L"The path " << oldPath << L" returns " << path << L"\n";
        }
    }
 
    // These calls demonstrate the results of different path forms.
    // Note that where those paths begin with backslashes, those
    // backslashes are merely stripped away and the entire path is returned.

    PCWSTR path1 = L"\\path1";

    wcout << L"The path " << path1 << L" returns " 
          << PathFindNextComponent(path1);
        
    PCWSTR path2 = L"\\path1\\path2";

    wcout << L"\nThe path " << path2 << L" returns "
          << PathFindNextComponent(path2);
        
    PCWSTR path3 = L"path1\\path2";
 
    wcout << L"\nThe path " << path3 << L" returns "
          << PathFindNextComponent(path3);
 
    wcout << L"\nThe path " << L"c:\\file.txt" << L" returns "
          << PathFindNextComponent(L"c:\\file.txt");
 
    return 0;
}

OUTPUT:
===========
The path c:\path1\path2\file.txt returns path1\path2\file.txt
The path path1\path2\file.txt returns path2\file.txt
The path path2\file.txt returns file.txt
The path file.txt returns a pointer to the terminating null character
The terminating null character returns NULL

The path \path1 returns path1
The path \path1\path2 returns path1\path2
The path path1\path2 returns path2
The path c:\file.txt returns file.txt

注意

shlwapi.h ヘッダーは PathFindNextComponent をエイリアスとして定義します。このエイリアスは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI または Unicode バージョンを自動的に選択します。 encoding-neutral エイリアスの使用を encoding-neutral ではないコードと混在すると、コンパイル エラーまたはランタイム エラーが発生する不一致が発生する可能性があります。 詳細については、「 関数プロトタイプの規則」を参照してください。

要件

   
サポートされている最小のクライアント Windows 2000 Professional、Windows XP [デスクトップ アプリのみ]
サポートされている最小のサーバー Windows 2000 Server [デスクトップ アプリのみ]
対象プラットフォーム Windows
ヘッダー shlwapi.h
Library Shlwapi.lib
[DLL] Shlwapi.dll (バージョン 4.71 以降)