1
我試圖讓運行的所有可執行文件的路徑列表處理Process32Next失敗,並ERROR_INSUFFICIENT_BUFFER(Windows 7)中
的do-while循環(如下圖所示)開始起飛後約90東西迭代iterations它失敗一個ERROR_INSUFFICIENT_BUFFER錯誤。我認爲這是pBuffer,我嘗試了一個非常大的緩衝區,但仍然失敗。失敗迭代中的ProcessEntry結構在szExeFile中存在垃圾。請指教(我關閉手柄,下方未顯示)
代碼:
// Retrieve a handle to the process snapshot
HANDLE hProcessSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (INVALID_HANDLE_VALUE == hProcessSnapshot) ERROR;
// Retrieve information about the first process and exit if unsuccessful
PROCESSENTRY32 oProcessEntry;
memset(&oProcessEntry, 0x00, sizeof(oProcessEntry));
oProcessEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL bFileFound(Process32First(hProcessSnapshot, &oProcessEntry));
DWORD dwError;
if(!bFileFound) {
dwError = GetLastError();
if(ERROR_NO_MORE_FILES == dwError) return TPathList();
// Error
ERROR;
}
// Walk the snapshot of processes
TCHAR pBuffer[MAX_PATH];
TPathList lExecutablePaths;
do {
// Get handle to process
HANDLE hProcess(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
oProcessEntry.th32ProcessID));
if(INVALID_HANDLE_VALUE == hProcess) ERROR;
if (!hProcess) continue;
// Get the module path
if(GetModuleFileNameEx(hProcess, 0, pBuffer, MAX_PATH) == 0) ERROR;
lExecutablePaths.push_back(CPath(pBuffer));
}
// Get next process
while(Process32Next(hProcessSnapshot, &oProcessEntry));
// If we ran out of files return what has been found
dwError = GetLastError();
if(ERROR_NO_MORE_FILES == dwError) return lExecutablePaths;
ERROR;
謝謝你,但我遇到的問題是不是與GetModuleFileNameEx但Process32Next功能。這是ERROR_INSUFFICIENT_BUFFER失敗的那個 – ababeel