GetModuleFileNameEx, Access Denied Error
我正在尝试获取所有打开进程的名称。 这就是我所拥有的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include"stdafx.h" #include <Psapi.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int iCmdShow) { bool _result; DWORD *pProcessIds = new DWORD[1000]; DWORD cb; DWORD pBytesReturned; _result = EnumProcesses(pProcessIds, 1000, &pBytesReturned); HANDLE hndProccesse; for (int i = 0; i < pBytesReturned / sizeof(DWORD); ++i) { hndProccesse = OpenProcess(STANDARD_RIGHTS_ALL, false, *pProcessIds); DWORD _len; DWORD _len2 =0; LPWSTR lpFilename = new WCHAR[100]; _len =GetModuleFileNameEx(hndProccesse,NULL, lpFilename, _len2); DWORD _errr; _errr = GetLastError(); MessageBox(NULL, lpFilename, NULL, 0); CloseHandle(hndProccesse); pProcessIds ++; } return 0; } |
一切正常,直到
这也是消息框中显示的内容:
有任何想法吗?
我想猜想
更多注意事项:
请记住,乔纳森·波特(Jonathan Potter)所说的有关检查Windows API函数返回的错误的正确方法。您从
关于内存泄漏,您永远不会
通常,使用
您需要使用FormatMessage获得有用的错误描述。下面显示了一个示例。此示例与您的示例完全相同,但所做的只是合并了错误检查。它不能解决问题!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <windows.h> #include <Psapi.h> void ShowError(DWORD err) { LPTSTR lpMsgBuf = nullptr; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&lpMsgBuf), 0, nullptr); MessageBoxA(NULL, lpMsgBuf,"ERROR", 0); LocalFree(lpMsgBuf); } int main() { DWORD* pProcessIds = new DWORD[1000]; DWORD pBytesReturned = 0; bool res = EnumProcesses(&pProcessIds[0], 1000, &pBytesReturned); if (!res) { DWORD err = GetLastError(); MessageBoxW(NULL, L"ENUMPROCESSES FAILED", L"Error", 0); ShowError(err); delete[] pProcessIds; return EXIT_FAILURE; } for (unsigned int i = 0; i < pBytesReturned / sizeof(DWORD); ++i) { if (pProcessIds[i] == 0) //error.. process id is 0.. continue; wchar_t lpFilename[256] = {0}; HANDLE hndProccess = OpenProcess(STANDARD_RIGHTS_ALL, false, pProcessIds[i]); if (hndProccess == NULL || hndProccess == INVALID_HANDLE_VALUE) { DWORD err = GetLastError(); MessageBoxW(NULL, L"FAILED TO OPEN PROCESS", L"ERROR", 0); ShowError(err); delete[] pProcessIds; return EXIT_FAILURE; } int len = GetModuleFileNameExW(hndProccess, NULL, lpFilename, sizeof(lpFilename) / sizeof(lpFilename[0])); if (len <= 0) { DWORD err = GetLastError(); if (err) { MessageBoxW(NULL, L"FAILED TO GET MODULEFILENAME", L"ERROR", 0); ShowError(err); delete[] pProcessIds; return EXIT_FAILURE; } } CloseHandle(hndProccess); MessageBoxW(NULL, lpFilename, L"NAME", 0); } delete[] pProcessIds; return 0; } |