gpt4 book ai didi

c++ - C/C++ 函数中的 LoadLibrary 和 GetProcAddress

转载 作者:行者123 更新时间:2023-11-30 20:06:23 48 4
gpt4 key购买 nike

我有这个代码:

int main(int argc, char *argv[])
{
typedef HHOOK(__stdcall *f_funci)(int, HOOKPROC, HINSTANCE, DWORD);
HINSTANCE hGetProcIDDLL = LoadLibrary(L"User32.dll");

if (!hGetProcIDDLL) {
printf("could not load the dynamic library");
printf("%d", GetLastError());
return EXIT_FAILURE;
}

// resolve function address here
f_funci SetWindowsHookEx2 = (f_funci)GetProcAddress(hGetProcIDDLL, "SetWindowsHookExW");
if (!SetWindowsHookEx2) {
printf("could not locate the function SetWindowsHookExW");
printf("%d", GetLastError());
return EXIT_FAILURE;
}

hMouseHook = SetWindowsHookEx2(WH_MOUSE_LL, my_function, hInstance, NULL);

return 0;
}

如何将所有这些代码插入到函数中,然后从主函数中调用它来获取 SetWindoesHookEx2 并稍后使用它?

这里的问题是,返回的值是 typedef HHOOK(__stdcall *f_funci)(int, HOOKPROC, HINSTANCE, DWORD) 并且它是一个 HHOOK 结构,我不知道如何处理。

在简历中我想要这样的东西:

int main(void) {
SetWindowsHook2 = dynamyc_function("User32.dll", "SetWindowsHookExW", int, HOOKPROC, HINSTANCE, DWORD)
hMouseHook = SetWindowsHookEx2(WH_MOUSE_LL, my_function, hInstance, NULL);
return 0;
}

我做不到:

HHOOK load_hook(){
typedef HHOOK(__stdcall *f_funci)(int, HOOKPROC, HINSTANCE, DWORD);
HINSTANCE hGetProcIDDLL = LoadLibrary(L"User32.dll");

if (!hGetProcIDDLL) {
printf("could not load the dynamic library");
printf("%d", GetLastError());
return EXIT_FAILURE;
}

// resolve function address here
f_funci SetWindowsHookEx2 = (f_funci)GetProcAddress(hGetProcIDDLL, "SetWindowsHookExW");
if (!SetWindowsHookEx2) {
printf("could not locate the function SetWindowsHookExW");
printf("%d", GetLastError());
return EXIT_FAILURE;

}
return SetWindowsHookEx2;
}

有两个错误:'return':无法从 'int' 转换为 'HHOOK''return':无法从 'f_funci' 转换为 'HHOOK' > 第一个引用return EXIT_FAILURE;,第二个引用“return SetWindowsHookEx2;”

谢谢。

最佳答案

尝试这样的事情:

#include <map>
#include <string>

std::map<std::wstring, HINSTANCE> libs;

template<typename T>
bool dynamyc_function(LPCWSTR libname, LPCSTR funcname, T *func)
{
HINSTANCE hlib = libs[libname];
if (!hlib)
{
hlib = LoadLibraryW(libname);
if (!hlib)
{
wprintf(L"could not load the dynamic library %s! %d", libname, GetLastError());
return false;
}
libs[libname] = hlib;
}

*func = (T) GetProcAddress(hlib, funcname);
if (!*func)
{
printf("could not locate the function %s! %d", name, GetLastError());
return false;
}

return true;
}

int main(int argc, char *argv[])
{
typedef HHOOK(__stdcall *f_funci)(int, HOOKPROC, HINSTANCE, DWORD);

// resolve function address here
f_funci SetWindowsHookEx2;
if (!dynamyc_function(L"User32.dll", "SetWindowsHookExW", &SetWindowsHookEx2))
return EXIT_FAILURE;

HHOOK hMouseHook = SetWindowsHookEx2(WH_MOUSE_LL, my_function, hInstance, NULL);
if (!hMouseHook)
{
printf("could not set hook! %d", GetLastError());
return EXIT_FAILURE;
}
...
UnhookWindowsHookEx(hMouseHook);

return 0;
}

或者,如果您只对特定功能感兴趣,您可以尝试这样的方法:

typedef HHOOK(__stdcall *f_funci)(int, HOOKPROC, HINSTANCE, DWORD);

HHOOK __stdcall Impl_SetWindowsHookExW(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);
HHOOK __stdcall Stub_SetWindowsHookExW(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);
f_funci lpSetWindowsHookExW = &Stub_SetWindowsHookExW;

HHOOK __stdcall Impl_SetWindowsHookExW(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD
{
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return NULL;
}

HINSTANCE hUser32 = NULL;
HHOOK __stdcall Stub_SetWindowsHookExW(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId)
{
if (!hUser32)
{
hUser32 = LoadLibraryW("User32.dll");
if (!hUser32)
{
DWORD err = GetLastError();
printf("could not load the dynamic library! %d", err);
SetLastError(err);
return NULL;
}
}

f_funci func = (f_funci) GetProcAddress(hUser32, "SetWindowsHookExW");
if (!func)
{
DWORD err = GetLastError();
printf("could not locate the function! %d", err);
SetLastError(err);
func = &Impl_SetWindowsHookExW;
}

lpSetWindowsHookExW = func;
return func(idHook, lpfn, hMod, dwThreadId);
}

int main(int argc, char *argv[])
{
HHOOK hMouseHook = lpSetWindowsHookExW(WH_MOUSE_LL, my_function, hInstance, NULL);
if (!hMouseHook)
{
printf("could not set hook! %d", GetLastError());
return EXIT_FAILURE;
}
...
UnhookWindowsHookEx(hMouseHook);

return 0;
}

关于c++ - C/C++ 函数中的 LoadLibrary 和 GetProcAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149927/

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