Search code examples
cwinapimingwrundll32

How do I pass a string to a DLL using the Win32 API?


I am trying to pass a string (or char*) from Rundll32 to a DLL built (with MinGW) using this source:

#include <windows.h>

__declspec( dllexport ) int hello(LPSTR content) {

  MessageBox( NULL, content, "Message", MB_OK );
  return 0;

}

When running this I get random crashes. This is how I run it.

C:\workspace>c:\MinGW\bin\gdb.exe rundll32 -ex "run program1.dll,hello test"

I tried setting a breakpoint at hello() and it seems that "content" is pretty random. Am I passing the argument from rundll32 in the wrong way?

It works fine if I don't have arguments.


Solution

  • rundll32 entry points need to be declared in a very specific way. Check out this article which explains what to do. One thing I've noticed is that, for a symbol called "EntryPoint", the function name passed to rundll32 should be "_EntryPoint@16" for 32-bit DLLs, and just "EntryPoint" for 64-bit DLLs.