[英]FindNextFile fails on 64-bit Windows?
使用C ++ Builder 2007,FindFirstFile和FindNextFile函數似乎無法在64位版本的Vista和XP上找到某些文件。 我的測試應用程序是32位。
如果我使用它們來遍歷文件夾C:\\ Windows \\ System32 \\ Drivers,它們只能找到一些文件,盡管在命令提示符下發出dir命令時有185個文件。 使用相同的示例代碼可以在32位版本的XP上列出所有文件。
這是一個小例子程序:
int main(int argc, char* argv[])
{
HANDLE hFind;
WIN32_FIND_DATA FindData;
int ErrorCode;
bool cont = true;
cout << "FindFirst/Next demo." << endl << endl;
hFind = FindFirstFile("*.*", &FindData);
if(hFind == INVALID_HANDLE_VALUE)
{
ErrorCode = GetLastError();
if (ErrorCode == ERROR_FILE_NOT_FOUND)
{
cout << "There are no files matching that path/mask\n" << endl;
}
else
{
cout << "FindFirstFile() returned error code " << ErrorCode << endl;
}
cont = false;
}
else
{
cout << FindData.cFileName << endl;
}
if (cont)
{
while (FindNextFile(hFind, &FindData))
{
cout << FindData.cFileName << endl;
}
ErrorCode = GetLastError();
if (ErrorCode == ERROR_NO_MORE_FILES)
{
cout << endl << "All files logged." << endl;
}
else
{
cout << "FindNextFile() returned error code " << ErrorCode << endl;
}
if (!FindClose(hFind))
{
ErrorCode = GetLastError();
cout << "FindClose() returned error code " << ErrorCode << endl;
}
}
return 0;
}
在64位XP上的C:\\ Windows \\ System32 \\ Drivers文件夾中運行它會返回:
C:\WINDOWS\system32\drivers>t:\Project1.exe
FindFirst/Next demo.
.
..
AsIO.sys
ASUSHWIO.SYS
hfile.txt
raspti.zip
stcp2v30.sys
truecrypt.sys
All files logged.
同一系統上的dir命令返回:
C:\WINDOWS\system32\drivers>dir/p
Volume in drive C has no label.
Volume Serial Number is E8E1-0F1E
Directory of C:\WINDOWS\system32\drivers
16-09-2008 23:12 <DIR> .
16-09-2008 23:12 <DIR> ..
17-02-2007 00:02 80.384 1394bus.sys
16-09-2008 23:12 9.453 a.txt
17-02-2007 00:02 322.560 acpi.sys
29-03-2006 14:00 18.432 acpiec.sys
24-03-2005 17:11 188.928 aec.sys
21-06-2008 15:07 291.840 afd.sys
29-03-2006 14:00 51.712 amdk8.sys
17-02-2007 00:03 111.104 arp1394.sys
08-05-2006 20:19 8.192 ASACPI.sys
29-03-2006 14:00 25.088 asyncmac.sys
17-02-2007 00:03 150.016 atapi.sys
17-02-2007 00:03 106.496 atmarpc.sys
29-03-2006 14:00 57.344 atmepvc.sys
17-02-2007 00:03 91.648 atmlane.sys
17-02-2007 00:03 569.856 atmuni.sys
24-03-2005 19:12 5.632 audstub.sys
29-03-2006 14:00 6.144 beep.sys
Press any key to continue . . .
etc.
我很困惑。 這是什么原因?
布賴恩
是否有重定向? 請參閱Wow64DisableWow64FsRedirection上的評論http://msdn.microsoft.com/en-gb/library/aa365743.aspx
我在MSDN上發現了這個:
如果您正在編寫32位應用程序以列出目錄中的所有文件,並且應用程序可能在64位計算機上運行,則應在調用FindFirstFile之前調用Wow64DisableWow64FsRedirection函數,並在最后一次調用FindNextFile之后調用Wow64RevertWow64FsRedirection。 有關更多信息,請參閱文件系統重定向器。
我必須更新我的代碼因為:-)
您確定它與dir命令位於同一目錄中嗎? 它們似乎沒有任何共同的文件。
此外,這不是問題,但“所有文件”的正確外卡是*
*。*表示“名稱中至少有一個。所有文件”
編譯時是否有任何警告?
您是否為此特定測試打開了所有警告(因為它不起作用)?
確保首先解決警告。
示例代碼沒有問題。 我還有另一個失敗的應用程序,用Delphi編寫。 我想我找到了基於Kris關於重定向的答案的答案: http : //msdn.microsoft.com/en-gb/library/aa364418(VS.85).aspx
得到它了:
http://msdn.microsoft.com/en-gb/library/aa384187(VS.85).aspx
當32位應用程序從64位操作系統上的其中一個文件夾中讀取時:
%windir%\system32\catroot
%windir%\system32\catroot2
%windir%\system32\drivers\etc
%windir%\system32\logfiles
%windir%\system32\spool
Windows實際上列出了以下內容:
%windir%\SysWOW64\catroot
%windir%\SysWOW64\catroot2
%windir%\SysWOW64\drivers\etc
%windir%\SysWOW64\logfiles
%windir%\SysWOW64\spool
感謝您輸入Kris,這有助於我了解正在發生的事情。
編輯:謝謝你Ludvig :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.