gpt4 book ai didi

c++ - 通过 psapi.dll 调用 GetModuleFileNameEx 不起作用,但为什么呢?

转载 作者:行者123 更新时间:2023-11-27 22:30:24 25 4
gpt4 key购买 nike

我正在使用不具备完整功能的 MinGW。例如。它没有 wchar_t 流支持。我已经设法通过编写一组小型操纵器(下面代码中的 wcusT())来解决这个问题。但我发现我再次受阻于 GetModuleFileNameEx。我无法在 native 运行 GetModuleFileNameEx()此函数在 <psapi.h> 中定义,但它似乎没有任何链接。这是我的第一个问题:MinGW 能否/是否/是否能够运行 GetModuleFileNameEx?我需要做什么?我错过了一些简单的东西吗?作为一种解决方法,我尝试通过调用其位于 Windows system32 文件夹中的 dll (psapi.dll) 来间接运行它……但出了点问题。我有另一个禁区。如果对下面的代码有任何评论,我将不胜感激。谢谢

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=
// ======================

最佳答案

您错误地调用了 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" );

关于c++ - 通过 psapi.dll 调用 GetModuleFileNameEx 不起作用,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3232257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com