-3

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

SadLandscape
  • 75
  • 1
  • 8
  • 2
    Do not use `MessageBox` to debug issues concerning `DllMain`. Use [OutputDebugString](https://stackoverflow.com/questions/3179199/simplest-way-to-write-output-message-to-output-window-in-visual-studio-2010). I bet the function *is* being called. – PaulMcKenzie Aug 17 '21 at 09:28
  • @PaulMcKenzie thanks but it didn't work my output is like this https://pastebin.com/raw/S7QYXpe5 And my code is like this: https://pastebin.com/raw/HXVWEqSm I don't see any "I'm running" in output – SadLandscape Aug 17 '21 at 09:33
  • If you use rundll32, make sure you run it like this `rundll32 blahblah.dll,something` otherwise, rundll32 will do nothing. – Simon Mourier Aug 17 '21 at 09:45
  • in order to some function from dll will be called, need that dll itself first be loaded to process. your dll is loaded ? based on https://pastebin.com/raw/S7QYXpe5 - no. so what you want ? – RbMm Aug 17 '21 at 10:09
  • *"My Dllmain function is not being called when it attaches to a process"* - What are you doing that causes a process to actually load your DLL? Vital details like this are missing from the question, so it's hard to even guess what's wrong (the code, or your expectations). – IInspectable Aug 17 '21 at 10:58
  • @IInspectable I used visual studio's attach to process debugging settings – SadLandscape Aug 17 '21 at 11:41
  • @RbMm I used visual studio's attach to process debugging settings – SadLandscape Aug 17 '21 at 11:41
  • @SimonMourier is there any way to do that in visual studio so I don't have to build everytime then run it? – SadLandscape Aug 17 '21 at 11:42
  • *I used visual studio's attach to process debugging settings* ?!? – RbMm Aug 17 '21 at 11:50
  • you debug rundll32.exe - how this related to your dll ? – RbMm Aug 17 '21 at 12:09
  • @RbMm oh I tought I inject my dll to rundll32.exe so all this time I was debugging rundll32.exe? – SadLandscape Aug 17 '21 at 12:12
  • you "debug" rundll32.exe. your dll here not related – RbMm Aug 17 '21 at 12:15
  • @RbMm Thanks, can you please tell me how can I debug dll instead? I wanna test it to see if it works or not Thanks – SadLandscape Aug 17 '21 at 12:17
  • Sure, just do this like this: https://i.imgur.com/A25CkFx.png this should trigger the dll load – Simon Mourier Aug 17 '21 at 12:33
  • @SimonMourier Thank you so much post this as an answer and I'll mark it as an answer – SadLandscape Aug 17 '21 at 12:38
  • @SimonMourier after running it displays messagebox but after clicking ok it gives me error saying invalid entry x should I replace the "x"? – SadLandscape Aug 17 '21 at 12:43
  • @SimonMourier after running it displays messagebox but after clicking ok it gives me error saying missing entry x should I replace the "x"? – SadLandscape Aug 17 '21 at 12:43

1 Answers1

1

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?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298