文号
827309
阅读
11095
回复
1
收藏
1
编写用于rundll32调用的dll
2016/10/30
IP:四川

rundll32.exe是windows下的一个工具程序,用来运行dll文件中的特定函数。比如下列命令打开Win2000样式的“用户账户”(“用户和密码”)对话框,一般用来设置自动登录。

rundll32 netplwiz.dll,UsersRunDll

首先,rundll32的语法如下:

rundll32 DLL文件名,入口点名称 参数字符串

rundll32调用的函数原型必须为如下:

// Win98/Me版本
extern "C"
void CALLBACK MyEntry(HWND hparent, HINSTANCE hrundll32, LPSTR szCmdLine, int nShowCmd);

// Win2000/XP版本
extern "C"
void CALLBACK MyEntryW(HWND hparent, HINSTANCE hrundll32, LPWSTR szCmdLine, int nShowCmd);

接收宽字符的版本必须有-W后缀,没有后缀的版本只能接收ANSI字符串。

必须在myrundll.def文件里声明导出,而不是用__declspec(dllexport)导出(不然会被导出为_MyEntry@16和_MyEntryW@16):

EXPORTS
	MyEntry
	MyEntryW

rundll32命令:

rundll32 myrundll.dll,MyEntry 命令行参数

完整的代码:

// myrundll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <windows.h>

HINSTANCE g_hdll;

// 接收进程和线程通知
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ulReason, LPVOID lpReserved)
{
	if (ulReason == DLL_PROCESS_ATTACH)
	{
		g_hdll = hModule;
	}
    return TRUE;
}

// Win98/Me的入口点
// 需要用def文件EXPORTS导出,不能使用__declspec(dllexport),否则会导出为_MyEntry@16
extern "C"
void CALLBACK MyEntry(HWND hparent, HINSTANCE hrundll32, LPSTR szCmdLine, int nShowCmd)
{
	char str[32768] = "";
	char fmtbuf[1024] = "";
	wsprintfA(fmtbuf, "%x", hparent);
	lstrcatA(str, fmtbuf);
	lstrcatA(str, "\r\n");
	char fn[MAX_PATH] = "";
	GetModuleFileNameA(hrundll32, fn, MAX_PATH);
	lstrcatA(str, fn);
	lstrcatA(str, "\r\n");
	lstrcatA(str, szCmdLine);
	lstrcatA(str, "\r\n");
	wsprintfA(fmtbuf, "%d", nShowCmd);
	lstrcatA(str, fmtbuf);
	lstrcatA(str, "\r\n");
	MessageBoxA(hparent, str, "Win98/Me的Rundll32", MB_ICONINFORMATION);
}

// Win2000/XP的入口点
// 需要用def文件EXPORTS导出,不能使用__declspec(dllexport),否则会导出为_MyEntryW@16
extern "C"
void CALLBACK MyEntryW(HWND hparent, HINSTANCE hrundll32, LPWSTR szCmdLine, int nShowCmd)
{
	wchar_t str[32768] = L"";
	wchar_t fmtbuf[1024] = L"";
	wsprintfW(fmtbuf, L"%x", hparent);
	lstrcatW(str, fmtbuf);
	lstrcatW(str, L"\r\n");
	wchar_t fn[MAX_PATH] = L"";
	GetModuleFileNameW(hrundll32, fn, MAX_PATH);
	lstrcatW(str, fn);
	lstrcatW(str, L"\r\n");
	lstrcatW(str, szCmdLine);
	lstrcatW(str, L"\r\n");
	wsprintfW(fmtbuf, L"%d", nShowCmd);
	lstrcatW(str, fmtbuf);
	lstrcatW(str, L"\r\n");
	MessageBoxW(hparent, str, L"Win2000/XP的Rundll32", MB_ICONINFORMATION);
}
</windows.h>

不需要特别为64位系统编写DLL,system32\rundll32.exe检测到DLL是32位的以后会自动调用syswow64\rundll32.exe。

rundll.png

[ 修改于10年1个月前 - 2016/10/31 00:57:15 ]
acmilan作者
10年1个月前IP:四川
827404
1楼
其实个人感觉这个技术对于一般应用情景来说没啥用。。。直接写exe就好了。。。
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的