如何使用GetProcAddress函数调用MessageBox? [英] How to call MessageBox with GetProcAddress function?

查看:161
本文介绍了如何使用GetProcAddress函数调用MessageBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下方式调用MessageBox()函数:

1).加载所需的库
2).获取函数地址
3).叫它

因此,出于我理解的目的,我应该在MessageBox函数中使用所有类型的参数定义新类型.

它返回INT并接受:HWND,LPCSTR,LPCSTR,UNIT.

所以我注册了新类型:

  typedef int(__ stdcall * msgbox)(HWND,LPCSTR,LPCSTR,UINT); 

我在调用此类函数时遇到问题.这样对所有功能有效还是仅对导出有效?
我该如何准确地调用MessageBox?

完整代码:

  #include< iostream>#include< windows.h>使用命名空间std;typedef int(__ stdcall * msgbox)(HWND,LPCSTR,LPCSTR,UINT);int main(无效){HINSTANCE__ * hModule = LoadLibrary(L"\\ Windows \\ System32 \\ User32.dll");msgbox * me = 0;if(hModule!= 0){我=(msgbox *)GetProcAddress(hModule,"MessageBox");}返回0;} 

解决方案

为什么要将所有内容都声明为指针?

LoadLibrary 返回一个 HMODULE ,而不是一个 HINSTANCE__ * (它可以与后者一起使用,但是最好遵循文档).

类似地, msgbox typedef 转换为函数指针类型,所以 me msgbox ,不是 msgbox * .

GetProcAddress 失败的原因是因为user32.dll导出了两个函数 MessageBoxA MessageBoxW .当您简单地在代码中调用 MessageBox 时,Windows.h中定义的宏会根据是否为 UNICODE 进行编译将其替换为2个实际函数名称之一..但是,当您尝试直接访问导出的函数时,需要显式指定要获取指针的函数.

  #include< iostream>#include< windows.h>typedef int(__ stdcall * msgbox)(HWND,LPCSTR,LPCSTR,UINT);int main(无效){HMODULE hModule = :: LoadLibrary(L"User32.dll");msgbox me = NULL;if(hModule!= NULL){我= reinterpret_cast< msgbox>(:: GetProcAddress(hModule,"MessageBoxA"));}if(我!= NULL){(* me)(NULL,我是一个消息框",你好",MB_OK);}if(hModule!= NULL){:: FreeLibrary(hModule);}} 

I want to call MessageBox() function in such way:

1). load needed library
2). get the function address
3). call it

So, for such aim as I understand, I should define new type with all types of arguments in MessageBox function.

It returnes INT and accepts: HWND, LPCSTR, LPCSTR, UNIT.

So I registred new type:

typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);

I have problems with calling such function. Does such way work for all functions or only for exported?
How can I call MessageBox exactly in such way?

Full code:

#include <iostream>
#include <windows.h>

using namespace std;

typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);

int main(void)
{
    HINSTANCE__ *hModule = LoadLibrary(L"\\Windows\\System32\\User32.dll");
    msgbox *me = 0;

    if(hModule != 0)
    {
        me = (msgbox*)GetProcAddress(hModule, "MessageBox");
    }

    return 0;
}

解决方案

Why are you declaring everything as a pointer?

LoadLibrary returns an HMODULE, not an HINSTANCE__ * (it will work with the latter but it's always better to adhere to the documentation).

Similarly, msgbox is typedef'd to a function pointer type, so me is a msgbox, not a msgbox *.

The reason why GetProcAddress fails is because user32.dll exports 2 functions, MessageBoxA and MessageBoxW. When you simply call MessageBox in your code, macros defined in Windows.h replace it with one of the 2 actual function names depending on whether you're compiling for UNICODE or not. But when you're trying to directly access the exported function as you are doing, you need to explicitly specify which one you're trying to get a pointer to.

#include <iostream>
#include <windows.h>

typedef int(__stdcall *msgbox)(HWND, LPCSTR, LPCSTR, UINT);

int main(void)
{
  HMODULE hModule = ::LoadLibrary(L"User32.dll");
  msgbox me = NULL;

  if( hModule != NULL ) {
    me = reinterpret_cast<msgbox>( ::GetProcAddress(hModule, "MessageBoxA") );
  }

  if( me != NULL ) {
    (*me)( NULL, "I'm a MessageBox", "Hello", MB_OK );
  }

  if( hModule != NULL ) {
    ::FreeLibrary( hModule );
  }
}

这篇关于如何使用GetProcAddress函数调用MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆