我想编一个程序监测指定目录树中的文件/目录的改变,而且能够知道是哪个文件/目录在改变,我用到了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设置的问题?或者其他?

解决方案 »

  1.   

    # include "Winbase.h"#program comment(lib,Kernel32.lib)
      

  2.   

    #program comment(lib,"Kernel32")
      

  3.   

    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 ) ){其他代码}
      

  4.   

    如果用的是98就把函数换为ReadDirectoryChangesA试试
      

  5.   

    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)是什么意思?
      

  6.   

    这是因为你的编译路径不对,在option的directory的include设定中,调整一下你类库的包含顺序。也可以检查一下你的预编译宏,看是否这个定义被跳过。
      

  7.   

    #program comment(lib,Kernel32.lib)
    就是加入Kernel32.lib文件,添加在文件前面就可以了
      

  8.   

    可能需要在.h文件中包含 #define _WIN32_WINNT 0x0400 
    看看这个:http://www.codeguru.com/Cpp/controls/treeview/directorybrowsers/article.php/c737/另外,陌生人说的应该是#pragma comment(lib,"Kernel32.lib")
    表示包含了这个lib
      

  9.   

    1."这是因为你的编译路径不对,在option的directory的include设定中,调整一下你类库的包含顺序。也可以检查一下你的预编译宏,看是否这个定义被跳过"
    应该是什么顺序?能否说得详细一点?
    2."就是加入Kernel32.lib文件,添加在文件前面就可以了"
    怎么加?能否说得详细一点?
    不好意思,我是菜鸟.
      

  10.   

    对,就是这个原因:
    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!
      

  11.   

    在.h文件中我已经包含 #define _WIN32_WINNT 0x0500 了
    可是还是不好使.
      

  12.   

    要不,试试每个头文件都加上?或者按上面说的,
    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/
      

  13.   

    哈哈,我终于搞定了!终于在第二天的凌晨把它搞定了!
    应该把#define _WIN32_WINNT 0x0400 加到StdAfx.h文件中去,或者加到每个头文件中.
    感谢 pomelowu(羽战士) 以及给与我帮助的所有朋友.加分之!!