我用FindFirstChangeNotification监视目录变化,为什么把第二个参数(是否包含子目录)设为True时,函数总是返回INVALID_HAND

  • 主题发起人 主题发起人 netstone
  • 开始时间 开始时间
N

netstone

Unregistered / Unconfirmed
GUEST, unregistred user!
我用FindFirstChangeNotification监视目录变化,为什么把第二个参数(是否包含子目录)设为True时,函数总是返回INVALID_HANDLE_VALUE?(100分)<br />同题目!谢谢谢谢!
 
很急呀,拜托各位了!
 
up<br><br>拜托了~~~~:(
 
记住目录一定要存在哦,如果不存在就会无效的窗体句柄<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var dwChangeHandles:Array[0..1]of THANDLE;<br>&nbsp; &nbsp; dwWaitStatus:DWORD;<br>begin<br>//DWORD dwWaitStatus;<br>//HANDLE dwChangeHandles[2];<br><br>// Watch the C:/WINDOWS directory for file creation and<br>// deletion.<br><br>&nbsp; &nbsp; dwChangeHandles[0] := FindFirstChangeNotification(<br>&nbsp; &nbsp; &nbsp; 'C://winnt', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // directory to watch<br>&nbsp; &nbsp; &nbsp; FALSE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do not watch the subtree<br>&nbsp; &nbsp; &nbsp; FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes<br><br>&nbsp; &nbsp; if (dwChangeHandles[0] = INVALID_HANDLE_VALUE) then<br>&nbsp; &nbsp; &nbsp; //ExitProcess(GetLastError());<br>&nbsp; &nbsp; &nbsp; Exit;<br><br>// Watch the C:/ subtree for directory creation and<br>// deletion.<br><br>&nbsp; &nbsp; dwChangeHandles[1] := FindFirstChangeNotification(<br>&nbsp; &nbsp; &nbsp; 'C://', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// directory to watch<br>&nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// watch the subtree<br>&nbsp; &nbsp; &nbsp; FILE_NOTIFY_CHANGE_DIR_NAME); &nbsp;// watch dir. name changes<br><br>&nbsp; &nbsp; if (dwChangeHandles[1] = INVALID_HANDLE_VALUE) then<br>// &nbsp; &nbsp;ExitProcess(GetLastError());<br>&nbsp; &nbsp; &nbsp; exit;<br><br>// Change notification is set. Now wait on both notification<br>// handles and refresh accordingly.<br><br>&nbsp; &nbsp; while 1&gt;0 do<br>&nbsp; &nbsp; begin<br><br>&nbsp; &nbsp; // Wait for notification.<br><br>&nbsp; &nbsp; &nbsp; dwWaitStatus := WaitForMultipleObjects(2, @dwChangeHandles,<br>&nbsp; &nbsp; &nbsp; &nbsp; FALSE, INFINITE);<br><br>&nbsp; &nbsp; &nbsp; Case (dwWaitStatus) of<br>&nbsp; &nbsp; &nbsp; &nbsp; WAIT_OBJECT_0:<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; // A file was created or deleted in C:/WINDOWS.<br>&nbsp; &nbsp; &nbsp; &nbsp; // Refresh this directory and restart the<br>&nbsp; &nbsp; &nbsp; &nbsp; // change notification. RefreshDirectory is an<br>&nbsp; &nbsp; &nbsp; &nbsp; // application-defined function.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //RefreshDirectory('C://WINDOWS');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add('c:/windows changed :'+ FormatDateTime('yyyy-MM-dd hh:mm:ss',now));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( FindNextChangeNotification(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwChangeHandles[0]) = FALSE ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //ExitProcess(GetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //break;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; WAIT_OBJECT_0 + 1:<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; // A directory was created or deleted in C:/.<br>&nbsp; &nbsp; &nbsp; &nbsp; // Refresh the directory tree and restart the<br>&nbsp; &nbsp; &nbsp; &nbsp; // change notification. RefreshTree is an<br>&nbsp; &nbsp; &nbsp; &nbsp; // application-defined function.<br><br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RefreshTree('C://');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add('c:// changed :'+ FormatDateTime('yyyy-MM-dd hh:mm:ss',now));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (FindNextChangeNotification(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dwChangeHandles[1]) = FALSE) then<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExitProcess(GetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //ExitProcess(GetLastError());<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>
 
TO:yunye<br><br>&nbsp; 大哥,好象不行诶~~~:(
 
非常关注!!!!!!!!!!!!!!!!!!!!!
 
我的测试代码:<br><br>Procedure TForm1.Button1Click(Sender: TObject);<br>Var<br>&nbsp; h: Thandle;<br>Begin<br>&nbsp; h := FindFirstChangeNotification('d:/temp', True,<br>&nbsp; &nbsp; FILE_NOTIFY_CHANGE_FILE_NAME);<br>&nbsp; If h = INVALID_HANDLE_VALUE Then<br>&nbsp; &nbsp; showmessage('No')<br>&nbsp; Else<br>&nbsp; &nbsp; showmessage('Yes');<br>End;
 
改成 'd://temp' <br>我这是可以的,win2000环境
 
我用的是win98,不行啊~~~~:(
 
我找了台98也可以,你用我的代码试一下,监测一下 c://
 
我直接用你的代码,可是一点反应都没有~:(<br><br>今天是没时间了,明天我再仔细的看一下吧,不管怎么样,都先谢谢啦~~~:)
 
确实不行,但设置为false就可以,另外路径在98下要用'/',<br>在2000下可以为true,好象是98不支持哦
 
路径应该没问题,我在98下直接用类似'c:/windows'的路径,<br>只要设成False,就都没问题呀,但是设成True,就不行了~~:(<br><br>救命呀!!!~~~~~~
 
我用VC写是可以的,用delphi确实不行,而且是在98下不行,估计是对longbool的设置不一致,<br>你最好用vc写吧,用vc写的我贴给你<br>void CHookdirDlg::OnOK() <br>{<br> // TODO: Add extra validation here<br>DWORD dwWaitStatus; <br>HANDLE dwChangeHandles[2]; <br>&nbsp;<br>// Watch the C:/WINDOWS directory for file creation and <br>// deletion. <br>CString ss; <br><br><br>dwChangeHandles[0] = FindFirstChangeNotification( <br>&nbsp; &nbsp; "C://temp1", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // directory to watch <br>&nbsp; &nbsp; FALSE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do not watch the subtree <br>&nbsp; &nbsp; FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes <br>&nbsp;<br>if (dwChangeHandles[0] == INVALID_HANDLE_VALUE) {<br>&nbsp; &nbsp; //ExitProcess(GetLastError()); <br> ss.Format("Step 1 Error code:%d",GetLastError());<br> m_lbInfo.AddString(ss);<br> return;<br>}<br>&nbsp;<br>// Watch the C:/ subtree for directory creation and <br>// deletion. <br> <br>dwChangeHandles[1] = FindFirstChangeNotification( <br>&nbsp; &nbsp; "C://temp2", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// directory to watch <br>&nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// watch the subtree <br>&nbsp; &nbsp; FILE_NOTIFY_CHANGE_FILE_NAME); &nbsp;// watch dir. name changes <br>&nbsp;<br>if (dwChangeHandles[1] == INVALID_HANDLE_VALUE) {<br>&nbsp; &nbsp; ss.Format("Step 2 Error code:%d",GetLastError());<br> m_lbInfo.AddString(ss);<br> return;<br>}<br>&nbsp;<br>// Change notification is set. Now wait on both notification <br>// handles and refresh accordingly. <br>&nbsp;<br> while (TRUE) <br> { <br>&nbsp;<br>&nbsp; &nbsp; // Wait for notification.<br>&nbsp;<br> dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles, <br> FALSE, INFINITE); <br>&nbsp;<br> switch (dwWaitStatus) <br> { <br> case WAIT_OBJECT_0: <br>&nbsp;<br> // A file was created or deleted in C:/WINDOWS. <br> // Refresh this directory and restart the <br> // change notification. RefreshDirectory is an <br> // application-defined function. <br>&nbsp;<br> //RefreshDirectory("C://WINDOWS") <br> if ( FindNextChangeNotification( <br> dwChangeHandles[0]) == FALSE ) <br> ExitProcess(GetLastError()); <br> //break;<br> ss="Step 1 OK ";<br> m_lbInfo.AddString(ss);<br> return; <br> case WAIT_OBJECT_0 + 1: <br>&nbsp;<br> // A directory was created or deleted in C:/. <br> // Refresh the directory tree and restart the <br> // change notification. RefreshTree is an <br> // application-defined function. <br>&nbsp;<br> // &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RefreshTree("C://"); <br> if (FindNextChangeNotification( <br> dwChangeHandles[1]) == FALSE) <br> ExitProcess(GetLastError()); <br> //break; <br> ss="Step 2 OK ";<br> m_lbInfo.AddString(ss);<br> return; <br> default: <br> ExitProcess(GetLastError()); <br> }<br>&nbsp; &nbsp; } <br> <br> CDialog::OnOK();<br>}
 
先谢谢yunye兄~~:)<br><br>可是用Delphi解决不了这个问题吗?~~~:(
 
各位大哥们,帮帮忙呀,拜托看一看~~:(
 
哎,算了~~
 
后退
顶部