系统对话框






4.89/5 (66投票s)
本文档展示了一种非常简单的方法来显示系统对话框,例如“Internet选项”、“添加/删除程序”等。
引言
RunDll32.exe 是微软最初为内部使用而开发的最有用的工具之一,但由于这个简单实用程序的广泛应用,它也被提供给开发者。通过这个简单的实用程序,可以启动系统对话框,例如控制面板中的小程序,或者隐藏在某些 DLL 中的对话框,例如 Format 对话框或 设备管理器属性表 对话框。
要测试 Rundll32.exe,只需单击“开始”按钮,然后从“开始”菜单中选择“运行...”,并输入以下语法
Rundll32.exe <name of dll> <entry point function> <arguments>
例如,要启动格式化对话框,只需运行以下语句
Rundll32.exe Shell32.dll SHFormatDrive
Shell32.dll 是隐藏格式化对话框的 DLL,而 SHFormatDrive
是启动格式化对话框的入口函数。
RunDll32 内部机制
Rundll32.exe 尝试加载的入口函数具有以下语法(针对 Unicode)
void <entry point function> (HWND hwndStub, HINSTANCE hAppInstance, LPWSTR lpCmdLine, int nCmdShow);
对于 ANSI
void <entry point function> (HWND hwndStub, HINSTANCE hAppInstance, LPSTR lpCmdLine, int nCmdShow);
hwndStub
是调用函数的窗口句柄。hAppInstance
是应用程序的实例句柄,lpCmdLine
是入口函数的命令行参数,而 nCmdShow
指定如何显示对话框。
RunDll32.dll 只是加载库(*.dll),然后尝试使用 GetProcAddress()
加载所需的函数。我们也可以通过编写以下代码来做到这一点
#ifdef _UNICODE typedef void (_stdcall *PFUNCTION_ENTRYPOINT)(HWND hwndStub, HINSTANCE hAppInstance, LPWSTR lpCmdLine, int nCmdShow); #else typedef void (_stdcall *PFUNCTION_ENTRYPOINT)(HWND hwndStub, HINSTANCE hAppInstance, LPSTR lpCmdLine, int nCmdShow); #endif PFUNCTION_ENTRYPOINT pEntryPoint=NULL; HINSTANCE hInst=AfxGetInstanceHandle(); HMODULE hModule = LoadLibrary(DllName); if (hModule) { pEntryPoint = (PFUNCTION_ENTRYPOINT) GetProcAddress(hModule, FunctionName); } if (pEntryPoint) { pEntryPoint(hParent, hInst, CommandLine, SW_SHOW); }
CSystemDialog
CSystemDialog
是一个非常简单的类,可以自动执行此操作。它只有一个成员函数
void DoModal(int iDialogID, HWND hParent);
iDialogID
是对话框的标识符,而 hParent
是程序主窗口的句柄。这是一个例子
CSystemDialog dlg; dlg.DoModal(SD_GAME_CONTROLLERS, m_hWnd);
类的定义如下
#ifndef _SYSTEM_DIALOGS_H_ #define _SYSTEM_DIALOGS_H_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #define SD_FORMAT 1 #define SD_INTERNET_OPTIONS 2 #define SD_ADD_REMOVE_PROGRAMS 3 #define SD_DATE_TIME 4 #define SD_DISPLAY 5 #define SD_MODEM 6 #define SD_MULTIMEDIA 7 #define SD_MOUSE 8 #define SD_NETWORK 9 #define SD_PASSWORD 10 #define SD_SYSTEM 11 #define SD_REGIONAL_SETTINGS 12 #define SD_SOUNDS 13 #define SD_GAME_CONTROLLERS 14 #define SD_KEYBOARD 15 #define SD_DEVICE_MANAGER 16 typedef struct tagSystemDialog { int iSystemDialogID; TCHAR cDllName[100]; char cFuncName[256]; TCHAR cCommand[100]; } SystemDialog; class CSystemDialog { public: void DoModal(int iDialogID, HWND hParent); CSystemDialog(); virtual ~CSystemDialog(); }; #endif // _SYSTEM_DIALOGS_H_
尽情享用!