编写用于rundll32调用的dll
acmilan2016/10/30软件综合 IP:四川

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

rundll32 netplwiz.dll,UsersRunDll

首先,rundll32的语法如下:

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

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

<code class="language-cpp">// 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);
</code>

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

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

<code class="language-def">EXPORTS
	MyEntry
	MyEntryW
</code>

rundll32命令:

rundll32 myrundll.dll,MyEntry 命令行参数

完整的代码:

<code class="language-cpp">// 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></code>

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

rundll.png

[修改于 7年6个月前 - 2016/10/31 00:57:15]

来自:计算机科学 / 软件综合
1
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
acmilan 作者
7年6个月前 IP:四川
827404
其实个人感觉这个技术对于一般应用情景来说没啥用。。。直接写exe就好了。。。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
acmilan
进士 学者 笔友
文章
461
回复
2934
学术分
4
2009/05/30注册,5年2个月前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}