Reputation: 253
I am trying to create a DLL which can be executed with RunDLL32. I know that RunDLL32 is running correctly because if I execute the following command, it pops up a message box:
rundll32 printui.dll,PrintUIEntry
/.
However I cannot get it to execute a DLL that I created, which looks like this:
// 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
RunDLL32 MyDLL.dll
Nothing happens. There are no error messages, not output, and no message boxes. Why is that?
Upvotes: 0
Views: 2544
Reputation: 598089
Since your are compiling a 64bit DLL, make sure you are running the 64bit version of RunDll32:
rundll32.exe equivalent for 64-bit DLLs
Per this page:
If you pass the wrong type of DLL to Rundll32, it may fail to run without returning any error message.
Even if you could get RunDLL32 to load your DLL, you can't safely call MessageBox()
in DllMain()
at all.
You need to export a function for RunDLL32 to execute besides your DllMain
. You can call MessageBox()
in that function, eg:
// 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"
Upvotes: 2