Condividi tramite


Funzione PathFindNextComponentW (shlwapi.h)

Analizza un percorso e restituisce la parte di tale percorso che segue la prima barra rovesciata.

Sintassi

LPCWSTR PathFindNextComponentW(
  [in] LPCWSTR pszPath
);

Parametri

[in] pszPath

Tipo: PTSTR

Puntatore a una stringa con terminazione null contenente il percorso da analizzare. Questa stringa non deve essere più lunga di MAX_PATH caratteri, oltre al carattere null terminante. I componenti del percorso sono delimitati da barre rovesciate. Ad esempio, il percorso "c:\path1\path2\file.txt" include quattro componenti: c:, path1, path2 e file.txt.

Valore restituito

Tipo: PTSTR

Restituisce un puntatore a una stringa con terminazione null contenente il percorso troncato.

Se pszPath punta all'ultimo componente nel percorso, questa funzione restituisce un puntatore al carattere Null di terminazione.

Se pszPath punta al carattere Null di terminazione o se la chiamata ha esito negativo, questa funzione restituisce NULL.

Commenti

PathFindNextComponent illustra una stringa di percorso finché non rileva una barra rovesciata ("\"), ignora tutto fino a quel punto, inclusa la barra rovesciata e restituisce il resto del percorso. Pertanto, se un percorso inizia con una barra rovesciata (ad esempio \path1\path2), la funzione rimuove semplicemente la barra rovesciata iniziale e restituisce il resto (path1\path2).

Esempio

L'applicazione console semplice seguente passa varie stringhe a PathFindNextComponent per illustrare cosa riconosce la funzione come componente del percorso e per mostrare cosa viene restituito. Per eseguire questo codice in Visual Studio, è necessario collegare Shlwapi.lib e definire UNICODE nei comandi del preprocessore nelle impostazioni del progetto.


#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

Nota

L'intestazione shlwapi.h definisce PathFindNextComponent come alias che seleziona automaticamente la versione ANSI o Unicode di questa funzione in base alla definizione della costante preprocessore UNICODE. La combinazione dell'utilizzo dell'alias di codifica neutrale con il codice che non è neutrale dalla codifica può causare errori di corrispondenza che causano errori di compilazione o runtime. Per altre informazioni, vedere Convenzioni per i prototipi di funzione.

Requisiti

   
Client minimo supportato Windows 2000 Professional, Windows XP [solo app desktop]
Server minimo supportato Windows 2000 Server [solo app desktop]
Piattaforma di destinazione Windows
Intestazione shlwapi.h
Libreria Shlwapi.lib
DLL Shlwapi.dll (versione 4.71 o successiva)