Search code examples
c++winapidllmessageboxdllmain

Dllmain function is not being called


I searched here but none of these questions helped me so yeah I'll explain: My Dllmain function is not being called when it attaches to a process (rundll32.exe) in visual studio project settings I changed it to attach to rundll32.exe it was supposed to show a messagebox on attach but it just doesn't do it. My code:

// 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(NULL,L"ThumbsUp",L"Attached",MB_ICONINFORMATION);
   case DLL_THREAD_ATTACH:
   case DLL_THREAD_DETACH:
   case DLL_PROCESS_DETACH:
       break;
   }
   return TRUE;
}

Thanks


Solution

  • If you use rundll32.exe as the loader program, you must call it using an entry point, like this:

    enter image description here

    In this case, DllMain has been called, but you'll get a messagebox error like :

    Error in D:\blah\blahblah.dll
    Missing entry: x
    

    That's normal, it's because you don't have exported any externally callable function from your .dll. "x" is here just a placeholder to force rundll32 to load the .dll. If you really want to use rundll32, then read this: How does RunDll32 work?