win32 - 从dll中导出api并使用

从User32.dll中导出MessageBoxW

代码:

#include <Windows.h>
#include <iostream>
#include <functional>

typedef int(__stdcall *MsgBOX)
(
    HWND    hWnd,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT    uType
    );

int main(int argc, char* argv[])
{
    HINSTANCE handle_user32_dll = LoadLibrary(TEXT("User32.dll"));
    std::function<int(HWND, LPCWSTR, LPCWSTR, UINT)> MsgBoxInstance;

    if (!handle_user32_dll)
    {
        std::cout << "Dll isn't loaded successfuly." << std::endl;
    }
    else
    {
        MsgBoxInstance = reinterpret_cast<MsgBOX>(GetProcAddress(handle_user32_dll, "MessageBoxW"));

        if (!MsgBoxInstance)
        {
            std::cout << "Function didn't resolved.";
        }
        else
        {
            MsgBoxInstance(NULL, TEXT("Resource not available\nDo you want to try again?"), TEXT("Account Details"), MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2);
        }
    }

    FreeLibrary(handle_user32_dll);

    return 0;
}

相关: How to call MessageBox with GetProcAddress function?

           GetModuleFileNameExA won't work like it should when I try to link the function on runtime from psapi.dll (windows)

posted @ 2020-12-28 17:48  strive-sun  阅读(254)  评论(0编辑  收藏  举报