Using RUNDLL32.exe to call a function within a dll

Using RUNDLL32.exe to call a function within a dll
        
Rundll32 is a utility included with Windows that allows you to execute an exported DLL-function from a command line.
Consider the following (exported) function in a DLL:

#include <windows.h>

extern "C" __declspec (dllexport) void __cdecl rdl (
   HWND hwnd,        // handle to owner window
   HINSTANCE hinst,  // instance handle for the DLL
   LPTSTR lpCmdLine, // string the DLL will parse
   int nCmdShow      // show state
)
{
  ::MessageBox(0,lpCmdLine,0,0);
}

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved)
{
    return TRUE;
}

The function rdl within the dll can now be called using rundll32:

rundll32 c:\path\to\dll\rdl.dll,rdl hallo

Note: the function rdl is specified with __declspec (dllexport). This is needed in order to export the function such that its address can be gotten with GetProcAddress. rundll32 used GetProcAddress. Also, the function rdl is specified with extern "C" and __cdecl so that rdl is not name-mangled (in case it is compiled with a c++ compiler).
Printing an HTML document

rundll32.exe %windir%\system32\mshtml.dll,PrintHTML "C:\x.html"

Note, the document name (at least on my system) needs to be enclosed in quotes.

posted @ 2015-08-02 15:51  老榕树  阅读(261)  评论(0编辑  收藏  举报