FindFirstFile 返回句柄无效
#include "stdafx.h"
#include "iostream"
#include "windows.h"
#define MAX_PATH 300
using namespace std;
void MyFindFile1(LPCTSTR lpPath);
int _tmain(int argc, _TCHAR* argv[])
{
const char *str = "D:\\test";
LPCTSTR lpPath = (LPCTSTR)str;
MyFindFile1(lpPath);
system("Pause");
return 0;
}
void MyFindFile1(LPCTSTR lpPath)
{
TCHAR MFind[MAX_PATH] = {_T("\0")};
WIN32_FIND_DATA findFileDate ;
bool bRet;
_tcscpy_s(MFind,MAX_PATH,lpPath);
_tcscat_s(MFind,_T("\\*.*"));
printf("%\s\n",MFind);
HANDLE hFind = ::FindFirstFile(MFind, &findFileDate);
if(INVALID_HANDLE_VALUE == hFind)
{
printf("无效句柄!\n");
return;
}
else
{
printf("句柄有效\n");
}
}
始终是输出无效句柄,我已经在D:\\test中建立了一些文件和文件夹。 文件操作,FindFirstFile
[解决办法]
你的工程是 MutliByte 的还是 Unicode 的?
如果是 Multi-Byte, 上面的代码是没有问题的.
如果是 Unicode 的, 第一句得改成 TCHAR *str = _T("D:\\test"); 才行.
另外, printf("%\s\n",MFind); 也得改成 _tprintf(_T("%\s\n"),MFind); , 当然, 这句不影响结果, 只影响显示
[解决办法]
嗯,UNICODE也是个问题