谷歌搜索“rundll32”,第三个命中是文档链接,
http://support.microsoft.com/kb/164787 http://support.microsoft.com/kb/164787
根据该文件,rundll32
调用用户指定的函数,其签名如下wWinMain
(除了这里的第一个参数是窗口句柄而不是实例句柄),
void CALLBACK
EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
所以,尝试一下:
// 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 );
}
构建和运行:
[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]
> _
输出显示在其自己的控制台窗口中,通过创建AllocConsole
,这通常是必要的,因为 rundll32 是一个 GUI 子系统程序(这也是freopen
calls).
要在现有控制台窗口中显示输出,只需省略对AllocConsole
and freopen
,并重定向标准输出rundll32
到管道。例如。标准输出可以通过 Windows 进行管道传输more
当输出只有几行或通过某些 *nix-utility 时cat
更多线路。但是,在标准命令解释器 [cmd.exe] 中,仅将输出重定向到con
.