将EXE文件转化为DLL文件的方法比较简单,只需要使用Visual Studio等开发工具,将EXE文件的代码和数据封装成一个DLL文件即可。具体操作如下:
第一种:
- 1)在Visual Studio中创建一个新的DLL项目。
- 2)将EXE文件中的代码和数据复制到DLL项目中,在本地目录中将源码文件夹拷至dll项目中。
- 3)修改DLL项目的代码,将其中与EXE文件相关的部分进行调整。__declspec(dllexport)、头文件声明、无效参数删除等
- 4)编译DLL项目,生成目标格式的DLL文件。
1)在Visual Studio中创建一个新的DLL项目。
文件->新建->项目
选择visual c++->控制台应用程序->确认
下一步
DLL->附加项去除->完成
2)将EXE文件中的代码和数据复制到DLL项目中,在本地目录中将源码文件夹拷至dll项目中。
右击->在文件资源管理器中打开文件夹
选择项目文件夹
资源文件->添加->现有项
选择刚才添加的项目->将源文件夹选择添加进去
3)修改DLL项目的代码,将其中与EXE文件相关的部分进行调整。__declspec(dllexport)、头文件声明、无效参数删除等
找到main函数
修改main函数调用名等
修改为如下图
资源文件右击->添加
选择头文件 重命名
Main函数中添加头文件引用
将下列内容添加至main函数所在文件最末尾
右击项目->属性
常规->配置类型->动态库
选择为release
生成->重新生成解决方案
完成!
第二种方式:
- 将项目属性中生成文件从exe改为dll
- 修改项目main函数函数名,并添加__declspec(dllexport)
- 将dll项目生成的main复制到原项目中
- 编译DLL项目,生成目标格式的DLL文件。
1)将项目属性中生成文件从exe改为dll
前面与第一种一致
2)修改项目main函数函数名,并添加__declspec(dllexport)
项目自带main函数
修改后的项目main函数
3)将dll项目生成的main复制到原项目中,添加到最后即可
将属性中的生成改为dll
生成
完成!