yii2类参考手册:vs2008自制dll,显式方式调用dll
来源:百度文库 编辑:偶看新闻 时间:2024/10/05 11:07:08
使用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
vb调用dll出错
.dll动态链接调用?
直接调用dll
DLL静态调用
asp调用dll出错
VB调用DLL
VB调用MFC DLL
易趣dll页面调用
C#如何调用.DLL
_hook.dll MAGKEY.DL GAMEKEY.DLL;
Ox80040707 dll 调用函数失败
首页调用msxml3.dll 出错
skinmagic 的dll调用问题
ASP如何调用.dll文件
为什么没有调用到DLL??
vb 调用未注册dll
vb调用 dll 奇怪问题
asp调用DLL文件问题
asp.net中如何调用DLL
VB怎样调用DLL中的资源
ASP如何调用客户端DLL(急)
软件调用TWAIN.DLL文件错误
DLL函数调用失败:ISQT-SdshowMsg
向高手请教DLL的调用