2

I have created a dead-simple DLL in Visual Studio 2010, a win32 project of type DLL.

Then I changed the DllMain to this:

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

and I used rundll32 vahid-win32.dll,dllmain to run it. Message box shows, but after that it gives

Error in vahid-win32.dll

Missing entry: dllmain

What is wrong with my DLL? or with me? :-)

Thanks in advance

Community
  • 1
  • 1
vfsoraki
  • 2,186
  • 1
  • 20
  • 45
  • FOR FUTURE READERS: I am on Windows XP (SP3) and trying this, For me I couldn't even get the popup at all (the messagebox, that is) But making this simple change to the first line: BOOL APIENTRY DllMain(...) to extern "C" BOOL APIENTRY DllMain(...) Helped (Note, for me it did not solve the entire "thing" the error popped up afterwards, so future readers should follow the answer provided by @Haja Maideen Below) I just noted this to help the future users, which can probably be useful. (Hopefully) Edit: If this is wrong in some way, I would appreciate if someone pointed that out! thanks. – William Martens Dec 28 '20 at 15:56
  • Improved comment: IGNORE MY ABOVE COMMENT, THANK YOU: (I did not have size to post this) Instead of only having extern "C" and then BOOL APIENTRY ... Have it like so: extern "C" _declspec(dllexport) BOOL APIENTRY ... (Just like @Haja Maideen below said) This (for me, on Windows XP sp3) fixed any error msgs, and it loaded the messagebox. (I did: in an elevated cmd: rundll32.exe "thedllfile.dll", mydllmain) If this is wrong in some way, I would appreciate if someone pointed that out! thanks. – William Martens Dec 28 '20 at 16:07

2 Answers2

3

Your messagebox doesn't come from you passing DllMain function name. Rather it is automatically called. But Rundll32 looking for a export function with name DllMain with dllexport declaration as given below.

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

extern "C" __declspec(dllexport) void mydllmain()
{
   MessageBox(0,L"Hey there again!",0,0);
}

when you call RunDll32 with parameter mydllmain, it does me give both the message box with out error.

Haja Maideen
  • 450
  • 2
  • 4
1

There's no reason to call DllMain via RunDLL, it's called automatically when the DLL is loaded. Rather try running a custom function. Aside from that, the problem is probably the exported name. You need to write a .def file for the DLL.

DllMain it is caused automatically, always. it to cause through rundll32, it will be caused, for this purpose it and is "entry point", you repeatedly try to cause it.

Sathish
  • 3,740
  • 1
  • 17
  • 28