Partager via


PathFindNextComponentW, fonction (shlwapi.h)

Analyse un chemin et retourne la partie de ce chemin qui suit la première barre oblique inverse.

Syntaxe

LPCWSTR PathFindNextComponentW(
  [in] LPCWSTR pszPath
);

Paramètres

[in] pszPath

Type : PTSTR

Pointeur vers une chaîne terminée par null qui contient le chemin d’accès à analyser. Cette chaîne ne doit pas dépasser MAX_PATH caractères, plus le caractère null de fin. Les composants de chemin d’accès sont délimités par des barres obliques inverses. Pour instance, le chemin « c:\path1\path2\file.txt » comporte quatre composants : c:, path1, path2 et file.txt.

Valeur retournée

Type : PTSTR

Retourne un pointeur vers une chaîne terminée par null qui contient le chemin d’accès tronqué.

Si pszPath pointe vers le dernier composant du chemin d’accès, cette fonction retourne un pointeur vers le caractère null de fin.

Si pszPath pointe vers le caractère null de fin ou si l’appel échoue, cette fonction retourne NULL.

Remarques

PathFindNextComponent parcourt une chaîne de chemin jusqu’à ce qu’elle rencontre une barre oblique inverse (« \ »), ignore tout jusqu’à ce point, y compris la barre oblique inverse, et retourne le reste du chemin. Par conséquent, si un chemin commence par une barre oblique inverse (comme \path1\path2), la fonction supprime simplement la barre oblique inverse initiale et retourne le reste (chemin1\path2).

Exemples

L’application console simple suivante transmet différentes chaînes à PathFindNextComponent pour illustrer ce que la fonction reconnaît en tant que composant de chemin d’accès et pour montrer ce qui est retourné. Pour exécuter ce code dans Visual Studio, vous devez créer un lien vers Shlwapi.lib et définir UNICODE dans les commandes de préprocesseur dans les paramètres du projet.


#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

Notes

L’en-tête shlwapi.h définit PathFindNextComponent en tant qu’alias qui sélectionne automatiquement la version ANSI ou Unicode de cette fonction en fonction de la définition de la constante de préprocesseur UNICODE. La combinaison de l’utilisation de l’alias neutre en encodage avec du code qui n’est pas neutre en encodage peut entraîner des incompatibilités qui entraînent des erreurs de compilation ou d’exécution. Pour plus d’informations, consultez Conventions pour les prototypes de fonction.

Configuration requise

   
Client minimal pris en charge Windows 2000 Professionnel, Windows XP [applications de bureau uniquement]
Serveur minimal pris en charge Windows 2000 Server [applications de bureau uniquement]
Plateforme cible Windows
En-tête shlwapi.h
Bibliothèque Shlwapi.lib
DLL Shlwapi.dll (version 4.71 ou ultérieure)