c语言getprocaddress,函数C / C ++中的LoadLibrary和GetProcAddress

尝试这样的事情:

#include

#include

std::map<:wstring hinstance> libs;

template

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值