简体   繁体   English

RunDLL32 不执行任何操作

[英]RunDLL32 does not execute anything

I am trying to create a DLL which can be executed with RunDLL32.我正在尝试创建一个可以使用 RunDLL32 执行的 DLL。 I know that RunDLL32 is running correctly because if I execute the following command, it pops up a message box: rundll32 printui.dll,PrintUIEntry /.我知道 RunDLL32 运行正常,因为如果我执行以下命令,它会弹出一个消息框: rundll32 printui.dll,PrintUIEntry /。 However I cannot get it to execute a DLL that I created, which looks like this:但是我无法让它执行我创建的 DLL,它看起来像这样:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        MessageBox(0, L"Hello1", 0, 0);
        break;
    case DLL_THREAD_ATTACH:
        MessageBox(0, L"Hello2", 0, 0);
        break;
    case DLL_THREAD_DETACH:
        MessageBox(0, L"Hello3", 0, 0);
        break;
    case DLL_PROCESS_DETACH:
        MessageBox(0, L"Hello4", 0, 0);
        break;
    }
    return TRUE;
}

The code compiles fine (In Visual Studio 2017, Release mode, x64) but when I execute代码编译得很好(在 Visual Studio 2017 中,发布模式,x64)但是当我执行时

RunDLL32 MyDLL.dll

Nothing happens.没发生什么事。 There are no error messages, not output, and no message boxes.没有错误消息,没有输出,也没有消息框。 Why is that?这是为什么?

Since your are compiling a 64bit DLL, make sure you are running the 64bit version of RunDll32:由于您正在编译 64 位 DLL,因此请确保您运行的是 64 位版本的 RunDll32:

rundll32.exe equivalent for 64-bit DLLs rundll32.exe 等效于 64 位 DLL

Per this page :本页面

If you pass the wrong type of DLL to Rundll32, it may fail to run without returning any error message.如果将错误类型的 DLL 传递给 Rundll32,它可能无法运行且不返回任何错误消息。

Even if you could get RunDLL32 to load your DLL, you can't safely call MessageBox() in DllMain() at all .即使您可以让 RunDLL32 加载您的 DLL,您也根本无法安全地调用DllMain()中的MessageBox()

You need to export a function for RunDLL32 to execute besides your DllMain .除了DllMain之外,您还需要为 RunDLL32 导出一个函数来执行 You can call MessageBox() in that function, eg:您可以在该函数中调用MessageBox() ,例如:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

extern "C" __declspec(dllexport) void CALLBACK MyFunctionW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow)
{
    MessageBoxW(hwnd, lpszCmdLine, L"Hello", MB_OK);
}
rundll32 my.dll,MyFunction "hello world"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM