谷维素的价格:使用vs2008制作dll文件,生成动态链接库
来源:百度文库 编辑:偶看新闻 时间:2024/05/02 13:25:53
使用vs2008制作dll文件,生成动态链接库,采用显示加载,以下记录实现过程:
1.制作dll文件
- vs2008中,File > New Project > Other Language > Win32 > Win32 Console Application. 输入项目名称:MyDll。点击【OK】,【Next】,选择dll单选按钮,完成。
- vs2008会自动生成一些代码,不用管它。创建MyDll.h,代码如下 MyDll.h 为了提高可用性,写了可回带值的函数供参考Cpp代码
- //MyDLL.h
- extern "C" _declspec(dllexport) int Max(int a, int b);
- extern "C" _declspec(dllexport) int Min(int a, int b);
- extern "C" _declspec(dllexport) bool cim2scada(char* cimId, char** scadaId);
//MyDLL.h extern "C" _declspec(dllexport) int Max(int a, int b); extern "C" _declspec(dllexport) int Min(int a, int b); extern "C" _declspec(dllexport) bool cim2scada(char* cimId, char** scadaId);
- 创建MyDll.cpp
- Cpp代码
- // MyDll.cpp : Defines the exported functions for the DLL application.
- //
- #include "stdafx.h"
- #include"MyDll.h"
- #include
- int Max(int a, int b)
- {
- if(a>=b)return a;
- else
- return b;
- }
- int Min(int a, int b)
- {
- if(a>=b)return b;
- else
- return a;
- }
- bool cim2scada(char* cimId, char** scadaId){
- char *result;
- if(cimId == NULL){
- printf("空指针!");
- return false;
- }else if(strlen(cimId) == 0){
- printf("cimId无值!");
- return false;
- }
- if(strcmp(cimId, "aaaaaa") == 0){
- result = "1000";
- }else if(strcmp(cimId, "bbbbbb") == 0){
- result = "2000";
- }
- *scadaId = result;
- printf("fun scadaId:%s\n", *scadaId);
- return true;
- }
// MyDll.cpp : Defines the exported functions for the DLL application.//#include "stdafx.h"#include"MyDll.h" #include
int Max(int a, int b) { if(a>=b)return a; else return b; } int Min(int a, int b) { if(a>=b)return b; else return a; } bool cim2scada(char* cimId, char** scadaId){char *result;if(cimId == NULL){printf("空指针!");return false;}else if(strlen(cimId) == 0){printf("cimId无值!");return false;}if(strcmp(cimId, "aaaaaa") == 0){result = "1000";}else if(strcmp(cimId, "bbbbbb") == 0){result = "2000";}*scadaId = result;printf("fun scadaId:%s\n", *scadaId);return true;} - 编译一下,到工程的DEBUG目录,就可以找到MyDll.dll文件
2.创建测试程序
- vs2008中,File > New Project > Other Language > Win32 > Win32 Console Application. 输入项目名称:DllTest。点击【OK】,【Next】,选择console单选按钮,去掉“预编译头”默认勾选项,完成。
- Cpp代码
- #include "windows.h"
- #include
- void main(){
- typedef int(*pMax)(int a, int b);
- typedef int(*pMin)(int a, int b);
- typedef bool(*pcim2scada)(char* cimid, char** scada);
- HINSTANCE HDLL;
- HDLL=LoadLibrary(L"MyDll.dll");//加载动态链接库MyDll.dll文件;
- pMax Max=(pMax)GetProcAddress(HDLL,"Max");
- pMin Min=(pMin)GetProcAddress(HDLL,"Min");
- pcim2scada cim2scada=(pcim2scada)GetProcAddress(HDLL,"cim2scada");
- int a = Max(5, 12);
- char cimId[] = "aaaaaa";
- char * scadaId = "";
- bool b = cim2scada(cimId, &scadaId);
- printf("Max结果为%d",a);
- printf("cimId:%s, scadaId:%s", cimId, scadaId);
- FreeLibrary(HDLL);//卸载MyDll.dll文件;
- }
#include "windows.h"#include
void main(){typedef int(*pMax)(int a, int b); typedef int(*pMin)(int a, int b); typedef bool(*pcim2scada)(char* cimid, char** scada);HINSTANCE HDLL; HDLL=LoadLibrary(L"MyDll.dll");//加载动态链接库MyDll.dll文件; pMax Max=(pMax)GetProcAddress(HDLL,"Max"); pMin Min=(pMin)GetProcAddress(HDLL,"Min"); pcim2scada cim2scada=(pcim2scada)GetProcAddress(HDLL,"cim2scada"); int a = Max(5, 12); char cimId[] = "aaaaaa";char * scadaId = "";bool b = cim2scada(cimId, &scadaId);printf("Max结果为%d",a); printf("cimId:%s, scadaId:%s", cimId, scadaId);FreeLibrary(HDLL);//卸载MyDll.dll文件; } - 编译一下
- 拷贝MyDll.dll文件到DllTest项目的Debug目录下
- 打开一个控制台(cmd),进入DllTest > Debug目录,运行DllTest.exe
- 输出结果: Cpp代码
- fun scadaId : 1000
- Max结果为12
- cimId:aaaaaa, scadaId:1000
怎么动态生成.js文件?
dll这个动态数据库连接文件?
如何使用ASP在线动态生成表
php 怎样动态生成下载文件
GCC能调用/生成DLL动态连接库吗
DLL文件是怎样生成的?
什么软件可生成.dll文件呀??
使用动态连接库DLL的好处
使用动态连接库DLL的好处
dll动态连接文件,求教大师!
动态连接库文件supdate.dll损坏
在VF中通过编译生成的.DLL文件怎么使用啊!请高手指教!!谢谢~~
请教如何用jsp动态生成js文件 谢谢大家
图标文件如何制作成DLL文件
如何制作dll文件和ocx文件
VB中,是什么生成了dll文件?与dll同时生成的exp文件lib文件有什么用?
请问使用ASP如何生成shtml文件?
vb6.0的程序如何生成dll文件?
调用VC++生成的.DLL文件提示“内存访问冲突”
有C++的源文件,怎么能生成dll文件?
怎样把做好的delphi程序生成DLL文件??
动态链接库文件(.DLL)丢失了怎么办
动态连接文件(*.dll)是怎么工作的?
动态链接库文件DLL和注册表有什么关系?