原创文章,欢迎转载。转载请注明:转载自 祥的博客
原文链接:http://blog.csdn.net/humanking7/article/details/78586478
接着上文《DLL之旅1 : 将程序打包成DLL》,现在调用动态链接库有两种方法。
需要文件(一个都不能少):
头文件: qShareDll.h
#ifndef _Q_SHARE_DLL_H
#define _Q_SHARE_DLL_H
extern "C" _declspec(dllexport) double qAdd(double a, double b);
extern "C" _declspec(dllexport) double qSub(double a, double b);
// extern "C" 解决函数名由于不同编译器造成的名字匹配问题
// 通常c++编译器编译时会对函数进行改名,而c编译器不会
// _declspec(dllexport)说明该函数为导出函数
/*
如果函数用"_stdcall"进行修饰,在动态引用的时候,要对"函数指针"也要进行"_stdcall"修饰
__stdcall:Windows API默认的函数调用协议
extern "C" _declspec(dllexport) double _stdcall qAdd(double a, double b);
extern "C" _declspec(dllexport) double _stdcall qSub(double a, double b);
*/
#endif
调用源文件: main.cpp
#include
using namespace std;
#include "qShareDll.h"
#pragma comment(lib,"TestDll.lib") //
int main()
{
double a, b;
cout << "静态加载" << endl;
while (true)
{
cout << "input Number a: ";
cin >> a;
cout << "input Number b: ";
cin >> b;
cout << "a + b = " << qAdd(a, b) << endl;
cout << "a - b = " << qSub(a, b) << endl << endl;
}
return 0;
}
需要文件: 就一个DLL,需要用Depend工具,查看函数名(但这是不够的),还需要知道函数是怎么声明的(不然无法定义函数指针)。
调用源文件: main.cpp
#include
using namespace std;
#include
int main()
{
double a, b;
cout << "动态加载" << endl;
HINSTANCE handle = LoadLibraryA("TestDll.dll"); //用于加载dll
//如果当初头文件中有 "_stdcall" 修饰
//typedef double(_stdcall *ADDPROC)(double, double);
//typedef double(_stdcall *SUBPROC)(double, double);
typedef double(*ADDPROC)(double, double);
typedef double(*SUBPROC)(double, double);
// GetProcAddress第二个参数有两种方法:
// 1、通过DLL中的函数名
// 2、通过Depend工具中Ordinal索引值来查看 - 编译不通过放弃(而且确实不好用)
ADDPROC MyAdd = (ADDPROC)GetProcAddress(handle, "qAdd");
SUBPROC MySub = (SUBPROC)GetProcAddress(handle, "qSub");
//SUBPROC MySub = (ADDPROC)GetProcAddress(handle, MAKEINTRESOURCE(2)); //编译不通过-放弃
cout << "input Number a: ";
cin >> a;
cout << "input Number b: ";
cin >> b;
cout << "a + b = " << MyAdd(a, b) << endl;
cout << "a - b = " << MySub(a, b) << endl << endl;
FreeLibrary(handle);
return 0;
}
条件编译,方便。
#include
using namespace std;
//#define FLG_CMP_STATIC
#ifdef FLG_CMP_STATIC //静态加载DLL-Lib
#include "qShareDll.h"
#pragma comment(lib,"TestDll.lib")
int main()
{
double a, b;
cout << "静态加载" << endl;
while (true)
{
cout << "input Number a: ";
cin >> a;
cout << "input Number b: ";
cin >> b;
cout << "a + b = " << qAdd(a, b) << endl;
cout << "a - b = " << qSub(a, b) << endl << endl;
}
return 0;
}
#else //动态加载DLL
#include
int main()
{
double a, b;
cout << "动态加载" << endl;
HINSTANCE handle = LoadLibraryA("TestDll.dll"); //用于加载dll
//typedef double(_stdcall *ADDPROC)(double, double);
//typedef double(_stdcall *SUBPROC)(double, double);
typedef double(*ADDPROC)(double, double);
typedef double(*SUBPROC)(double, double);
// GetProcAddress第二个参数有两种方法:
// 1、通过DLL中的函数名
// 2、通过Depend工具中Ordinal索引值来查看
ADDPROC MyAdd = (ADDPROC)GetProcAddress(handle, "qAdd");
//SUBPROC MySub = (ADDPROC)GetProcAddress(handle, MAKEINTRESOURCE(2)); // MAKEINTRESOURCE LPCSTR
SUBPROC MySub = (SUBPROC)GetProcAddress(handle, "qSub"); // MAKEINTRESOURCE LPCSTR
while (true)
{
cout << "input Number a: ";
cin >> a;
cout << "input Number b: ";
cin >> b;
cout << "a + b = " << MyAdd(a, b) << endl;
cout << "a - b = " << MySub(a, b) << endl << endl;
}
FreeLibrary(handle);
return 0;
}
#endif // FLG_CMP_STATIC
用Matlab的CS-function 去调用DLL,实现xPC可以编译多个共用的mexw文件
to be continue …