FindFirstFile 返回句柄无效

bfas2010 2013-07-24 01:44:21
#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中建立了一些文件和文件夹。
...全文
276 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
似缘非缘 2013-10-24
  • 打赏
  • 举报
回复
1L正解,参见http://www.cnblogs.com/zjlinkpro/archive/2011/02/13/1953195.html
「已注销」 2013-07-24
  • 打赏
  • 举报
回复
嗯,UNICODE也是个问题
「已注销」 2013-07-24
  • 打赏
  • 举报
回复
const char *str = "D:\\test\\*.*"; 这样吧,难道你搜索的只是 test 文件?
www_adintr_com 2013-07-24
  • 打赏
  • 举报
回复
你的工程是 MutliByte 的还是 Unicode 的? 如果是 Multi-Byte, 上面的代码是没有问题的. 如果是 Unicode 的, 第一句得改成 TCHAR *str = _T("D:\\test"); 才行. 另外, printf("%\s\n",MFind); 也得改成 _tprintf(_T("%\s\n"),MFind); , 当然, 这句不影响结果, 只影响显示

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧