vs2005下的FindFirstFile

来源:百度知道 编辑:UC知道 时间:2024/07/07 12:58:24
我用VS2005做了一个控制台程序。
MSDN上的例子。我粘贴下来,在VS2005下运行时,错误是error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
return (0);
}
else
{
printf ("The first file found is %s\n",
FindFileData.cFileName);
FindClose(hFind);
return (1);
}
}
若改成hFind = FindFirstFile("f:\\*.*", &FindFileData);
错误error C2664: 'FindF

可能是你的程序指定为 Unicode版本的吧,在工程属性 -》 语言那里可以改的,你改成asni的可能就可以了 ,调用时写hFind = FindFirstFile("f:\\*.*", &FindFileData);
应该就可以了吧。

要么你要写hFind = FindFirstFile( L"f:\\*.*", &FindFileData);

这样指定字符串为unicode的宽字符,应该也可以

为了保证 ANSI/UNICODE 的移置性, 常量字符串应该使用 _T 来包围:
_T("hello world")
这样, 在非 UNICODE 版本下, 就是:
"hello world"
在 UNICODE 版本下, 就是:
L"hello world"

吧FindFirstFile写成FindFirstFileA即可,就不需要LPCWSTR的方法来转化了