Googling "rundll32", the 3rd hit was a link to documentation,
http://support.microsoft.com/kb/164787
According to that documentation, rundll32
calls a user-specified function with signature like wWinMain
(except the first argument here is a window handle instead of an instance handle),
void CALLBACK
EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
So, trying this out:
// File [foo.def]
EXPORTS
sayHello
// File [foo.cpp]
#include <iostream>
namespace myCode {
void sayHello()
{
using namespace std;
cout << "Hello, world!" << endl;
}
} // namespace myCode
#undef UNICODE
#define UNICODE
#include <windows.h>
extern "C"
__declspec( dllexport )
void CALLBACK sayHello( HWND, HINSTANCE, wchar_t const*, int )
{
AllocConsole();
freopen( "CONIN$", "r", stdin );
freopen( "CONOUT$", "w", stdout );
freopen( "CONOUT$", "w", stderr );
DWORD const infoBoxOptions = MB_ICONINFORMATION | MB_SETFOREGROUND;
MessageBox( 0, L"Before call...", L"DLL message:", infoBoxOptions );
myCode::sayHello();
MessageBox( 0, L"After call...", L"DLL message:", infoBoxOptions );
}
Building & running:
[d:\dev\test]
> cl foo.cpp foo.def user32.lib /MD /LD /D _CRT_SECURE_NO_WARNINGS
foo.cpp
Creating library foo.lib and object foo.exp
[d:\dev\test]
> rundll32 foo.dll,sayHello
[d:\dev\test]
> _
The output is presented in its own console window, created via AllocConsole
, which is generally necessary since rundll32 is a GUI subsystem program (this is also the reason for the freopen
calls).
To present the output in an existing console window one can just omit the calls to AllocConsole
and freopen
, and redirect standard output of rundll32
to a pipe. E.g. standard output can be piped through Windows’ more
when the output is just a few lines, or through some *nix-utility cat
for more lines. However, in the standard command interpreter [cmd.exe] it doesn’t work to just redirect the output to con
.