100分紧急求助:为什么VC中在支持MFC的Project中无法使用ReadDirectoryChangesW函数?

thebirdofwonder 2004-07-19 09:45:47
我想编一个程序监测指定目录树中的文件/目录的改变,而且能够知道是哪个文件/目录在改变,我用到了ReadDirectoryChangesW函数.如果建立工程时选择的是new -> project 选win32 console application 然后选 a simple application ,这样使用ReadDirectoryChangesW函数时就能正常运行.
但是如果选择an application support MFC ,其他均相同,需要的头文件也include了,就会在编译时显示出错信息:error C2065: 'ReadDirectoryChangesW' : undeclared identifier.紧急!希望大家指点迷津.谢谢.

我是这样做的:
1.new -> project 选win32 console application
2.选 an application support MFC
其中部分代码如下:
if( ReadDirectoryChangesW( hDir,
pNotify,
sizeof(buf),
true,
FILE_NOTIFY_CHANGE_FILE_NAME|
FILE_NOTIFY_CHANGE_DIR_NAME|
FILE_NOTIFY_CHANGE_ATTRIBUTES|
FILE_NOTIFY_CHANGE_SIZE|
FILE_NOTIFY_CHANGE_LAST_WRITE|
FILE_NOTIFY_CHANGE_LAST_ACCESS|
FILE_NOTIFY_CHANGE_CREATION|
FILE_NOTIFY_CHANGE_SECURITY,
&BytesReturned,
NULL,
NULL ) ){其他代码}
是因为头文件的问题吗?还是库的问题?还是VC设置的问题?或者其他?
...全文
223 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
thebirdofwonder 2004-07-20
  • 打赏
  • 举报
回复
哈哈,我终于搞定了!终于在第二天的凌晨把它搞定了!
应该把#define _WIN32_WINNT 0x0400 加到StdAfx.h文件中去,或者加到每个头文件中.
感谢 pomelowu(羽战士) 以及给与我帮助的所有朋友.加分之!!
pomelowu 2004-07-19
  • 打赏
  • 举报
回复
要不,试试每个头文件都加上?或者按上面说的,
So all you have to do is declare something like :

#define _WIN32_WINNT 0x0400

(or 0x0500 if you like, for Windows 2000) somewhere near the top of the file and that should be all that you need to get it to compile!

另外,codeguru上的这个例子是mfc的,也用到了ReadDirectoryChangesW,参考一下吧,我编译通过的:
http://www.codeguru.com/Cpp/controls/treeview/directorybrowsers/article.php/c737/
thebirdofwonder 2004-07-19
  • 打赏
  • 举报
回复
在.h文件中我已经包含 #define _WIN32_WINNT 0x0500 了
可是还是不好使.
pomelowu 2004-07-19
  • 打赏
  • 举报
回复
对,就是这个原因:
The answer is pretty simple actually. It's in the bottom of the first MSDN link I posted above (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/readdirectorychangesw.asp). Quoting directly from the page:

"To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0400 or later."

So all you have to do is declare something like :

#define _WIN32_WINNT 0x0400

(or 0x0500 if you like, for Windows 2000) somewhere near the top of the file and that should be all that you need to get it to compile!
thebirdofwonder 2004-07-19
  • 打赏
  • 举报
回复
1."这是因为你的编译路径不对,在option的directory的include设定中,调整一下你类库的包含顺序。也可以检查一下你的预编译宏,看是否这个定义被跳过"
应该是什么顺序?能否说得详细一点?
2."就是加入Kernel32.lib文件,添加在文件前面就可以了"
怎么加?能否说得详细一点?
不好意思,我是菜鸟.

pomelowu 2004-07-19
  • 打赏
  • 举报
回复
可能需要在.h文件中包含 #define _WIN32_WINNT 0x0400
看看这个:http://www.codeguru.com/Cpp/controls/treeview/directorybrowsers/article.php/c737/

另外,陌生人说的应该是#pragma comment(lib,"Kernel32.lib")
表示包含了这个lib
Kudeet 2004-07-19
  • 打赏
  • 举报
回复
#program comment(lib,Kernel32.lib)
就是加入Kernel32.lib文件,添加在文件前面就可以了
holyeagle 2004-07-19
  • 打赏
  • 举报
回复
这是因为你的编译路径不对,在option的directory的include设定中,调整一下你类库的包含顺序。也可以检查一下你的预编译宏,看是否这个定义被跳过。
thebirdofwonder 2004-07-19
  • 打赏
  • 举报
回复
1.#include "Winbase.h"我已经加了
2.win2000/9x 都不好使,编译都通不过.我用的是2000
3.函数名改成 ::ReadDirectoryChangesW 错误提示:error C2039: 'ReadDirectoryChangesW' : is not a member of '`global namespace'' error C2065: 'ReadDirectoryChangesW' : undeclared identifier
4.ReadDirectoryChangesA结果也一样
5.#program comment(lib,Kernel32.lib)是什么意思?

Kudeet 2004-07-19
  • 打赏
  • 举报
回复
如果用的是98就把函数换为ReadDirectoryChangesA试试
pomelowu 2004-07-19
  • 打赏
  • 举报
回复
1 不支持win9x,只支持NT构架的系统
2 函数名改成
::ReadDirectoryChangesW
这样用:
if( ::ReadDirectoryChangesW( hDir,
pNotify,
sizeof(buf),
true,
FILE_NOTIFY_CHANGE_FILE_NAME|
FILE_NOTIFY_CHANGE_DIR_NAME|
FILE_NOTIFY_CHANGE_ATTRIBUTES|
FILE_NOTIFY_CHANGE_SIZE|
FILE_NOTIFY_CHANGE_LAST_WRITE|
FILE_NOTIFY_CHANGE_LAST_ACCESS|
FILE_NOTIFY_CHANGE_CREATION|
FILE_NOTIFY_CHANGE_SECURITY,
&BytesReturned,
NULL,
NULL ) ){其他代码}
rainstormmaster 2004-07-19
  • 打赏
  • 举报
回复
你用的不会是win9x吧?
Kudeet 2004-07-19
  • 打赏
  • 举报
回复
#program comment(lib,"Kernel32")
Kudeet 2004-07-19
  • 打赏
  • 举报
回复
# include "Winbase.h"

#program comment(lib,Kernel32.lib)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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