FindFirstFile() and macros for Windows header files

 

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: Finding a file

To show: FindFirstFile() and macros for Windows header files

 

// The legacy macros in use by the Windows header files for different Windows OSes

// including the different Service Packs. Define appropriate macro for your target Windows platform.

// Windows Vista and Windows Server "Longhorn" _WIN32_WINNT>=0x0600, WINVER>=0x0600

// Windows Server 2003 _WIN32_WINNT>=0x0502, WINVER>=0x0502

// Windows XP _WIN32_WINNT>=0x0501, WINVER>=0x0501

// Windows 2000 _WIN32_WINNT>=0x0500, WINVER>=0x0500

// Windows NT 4.0 _WIN32_WINNT>=0x0400, WINVER>=0x0400

// Windows Me _WIN32_WINDOWS=0x0500, WINVER>=0x0500

// Windows 98 _WIN32_WINDOWS>=0x0410, WINVER>=0x0410

// Windows 95 _WIN32_WINDOWS>=0x0400, WINVER>=0x0400

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

 

// This program must be run at command prompt

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

{

WIN32_FIND_DATA FindFileData;

HANDLE hFind;

 

// If there is no input...

if(argc == 1)

{

printf("The command need a file name as an input! Error: %d\n", GetLastError());

printf("%s <target_file_name>\n", argv[0]);

return (1);

}

else

printf ("Target file is %s.\n", argv[1]);

hFind = FindFirstFile((LPCWSTR)argv[1], &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)

{

printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ());

return (1);

}

else

{

printf ("The first file found is %s\n", FindFileData.cFileName);

FindClose(hFind);

return (0);

}

}

 

Output example:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

The command need a file name as an input! Error: 0

cplus <target_file_name>

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

Target file is C:\data.txt.

Invalid File Handle. GetLastError reports 3

F:\vc2005project\cplus\debug>cplus cplus.lib

Target file is cplus.lib.

Invalid File Handle. GetLastError reports 3

F:\vc2005project\cplus\debug>

 

 

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