2010-07-12 73 views
0

我正在使用MinGW,它没有完整的功能。例如。它没有wchar_t流支持。 我已经设法通过编写一个小型的操纵器(下面的代码中的wcusT())来解决这个问题)..但是我发现我再次陷入GetModuleFileNameEx。 我还没有能够本机运行GetModuleFileNameEx() 此函数在<psapi.h>中定义,但它似乎没有任何链接。这是我的第一个问题:Can/does /是MinGW能够运行GetModuleFileNameEx吗?我需要做什么?我错过了一些简单的东西吗 作为一种解决方法,我试图通过调用Windows系统32文件夹中的dll(psapi.dll)来间接运行它...但出现了一些问题。 我有另一个不去的情况。我会很感激下面的代码有任何意见..谢谢通过psapi.dll调用GetModuleFileNameEx不起作用,但为什么?

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) 
{ /// typedef and load a dll function 
    /// =============================== 
    typedef DWORD (__stdcall *foo)(HANDLE, HMODULE, LPTSTR, DWORD); 
    LPTSTR ptcPSAPI_DLL = _T("C:\\WINDOWS\\system32\\psapi.dll"); 
    HMODULE hPSAPI_DLL = LoadLibrary(ptcPSAPI_DLL); 
    if(!hPSAPI_DLL) 
    { std::cout<<"ERROR: Failed to load "<<wcusT(ptcPSAPI_DLL)<<std::endl; 
    return 1; 
    } 
    foo GetModFnEx=(foo)GetProcAddress(hPSAPI_DLL, 
        #ifdef UNICODE 
         "GetModuleFileNameExW"); 
        #else 
         "GetModuleFileNameExA"); 
        #endif 

    /// call the dll library function 
    /// ============================= 
    HWND hWndNPP = FindWindow(_T("Notepad++"),NULL); // the window calass name 
    TCHAR ytcMFqFn[FILENAME_MAX]; // the buffer for the file name 
    DWORD dwBytes = (GetModFnEx)(hWndNPP, NULL, ytcMFqFn, sizeof(ytcMFqFn)); 
    DWORD dwError = GetLastError(); 

    std::cout<<wcusT(_T("hWndNPP "))<<"="<<hWndNPP  <<"="<<std::endl; 
    std::cout<<wcusT(_T("ytcMFqFn "))<<"="<<wcusT(ytcMFqFn)<<"="<<std::endl; 
    std::cout<<wcusT(_T("dwBytes "))<<"="<<dwBytes  <<"="<<std::endl; 
    std::cout<<wcusT(_T("dwError "))<<"="<<dwBytes  <<"="<<std::endl; 
    return 0; 

    // Output =============== 
    // SBCS 
    // hWndNPP =0x320606= 
    // ytcMFqFn == 
    // dwBytes =0= 
    // dwError =0= 
    // UNICODE 
    // h W n d N P P  =0x320606= 
    // y t c M F q F n =(☻æ|♀ = 
    // d w B y t e s  =0= 
    // d w E r r o r  =0= 
    // ====================== 

回答

5

您的通话GetModuleFileNameEx正确

HWND hWndNPP = FindWindow(_T("Notepad++"),NULL); 
DWORD dwBytes = (GetModFnEx)(hWndNPP // this is ment to be a process handle, not a HWND 
    , NULL, ytcMFqFn, sizeof(ytcMFqFn)); 

MSDN doc on GetModuleFileNameEx

你可能尝试获得使用以下

之一的进程句柄
::GetWindowThreadProcessId(hWnd, &dwProcessID); 
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID); 

// also in PSAPI - EnumProcesses will return an array of app process ids 
(BOOL(WINAPI *)(DWORD *,DWORD, DWORD *)) GetProcAddress(psapi, "EnumProcesses"); 
+0

感谢您的回答Greg ......多么尴尬。我遗漏了一个完整的函数调用GetWindowThreadProcessId()。我实际上知道它是需要的,但是我不能像以前那样破解所有的晚餐,并且它在咖啡和日出中迷失了。现在它至少输出PID ...但仍然没有任何东西会从dll中调用...我现在会打一个麻袋,稍后再检查它。 – 2010-07-12 23:12:16

+0

它的工作就像一个魅力现在..雷鸟们走了! ......这只是一个让这些和零点正确排列的问题......再次感谢格雷格......我会标记你的答案,但需要更多的代表来做到这一点。 – 2010-07-14 22:09:14

+0

总是很高兴看到取得成功的进展,祝你好运。 – 2010-08-25 20:47:57

相关问题