SearchPath() Function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Searching a file

To show: Using the SearchPath() function

 

// This program run at command prompt

#include <windows.h>

#include <stdio.h>

 

TCHAR lpBuffer[MAX_PATH];

LPWSTR *lpFilePart;

DWORD test;

 

int main(int argc, char *argv[])

{

// If not enough arguments supplied

if(argc != 3)

{

printf("Usage: %s <path> <filename>\n", argv[0]);

exit (1);

}

 

test = SearchPath(

NULL,

(LPCWSTR)argv[2],

NULL,

MAX_PATH,

lpBuffer,

lpFilePart

);

 

// if something wrong to the SearchPath()

if(test == 0)

{

printf("%s file not found! Error code: %d\n", argv[2], GetLastError());

printf("The return value is %d\n", test);

exit (1);

}

// Display the result...

else

{

printf("The return value is: %d, error: %d\n", test, GetLastError());

printf("The path is %S\n", lpBuffer);

printf("The path is %p\n", lpFilePart);

}

return 0;

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

Usage: cplus <path> <filename>

 

F:\vc2005project\cplus\debug>cplus C:\ original.txt

original.txt file not found! Error code: 2

The return value is 0

 

F:\vc2005project\cplus\d.e.b.u.g>

 

 

C and C++ Programming Resources | C & C++ Code Example Index