#include "iostream"
#include "iomanip"
#include "fstream"
#include "cstdlib"
//#include "libxl.h"
#include <windows.h>
using namespace std;
typedef void (*lpconnect)();
//void connect();
//void disconnect();
int main(){
//dll 句柄
HINSTANCE hModule;
//函数指针
lpconnect connect;
//加载dll
hModule = LoadLibrary("..\\lib\\cygmysharelib.dll");
if(hModule!=NULL){
//传入dll句柄
connect = (lpconnect)GetProcAddress(hModule,"connect");
if(connect!=NULL){
connect();
}else{
cout<<"无法调用函数connect"<<endl;
}
//释放资源
FreeLibrary(hModule);
}
}
}
1.创建类库


2.动态调用
#include "iostream"
#include "iomanip"
#include "fstream"
#include "cstdlib"
//#include "libxl.h"
#include <windows.h>
using namespace std;
typedef void (*lpconnect)();
typedef void (*lpdisconnect)();
//void connect();
//void disconnect();
int main(){
//dll 句柄
HINSTANCE hModule;
//函数指针
lpconnect connect;
lpdisconnect disconnect;
//加载dll
hModule = LoadLibrary("..\\lib\\cygmysharelib.dll");
if(hModule!=NULL){
//传入dll句柄
connect = (lpconnect)GetProcAddress(hModule,"connect");
if(connect!=NULL){
connect();
}else{
cout<<"无法调用函数connect"<<endl;
}
disconnect = (lpdisconnect)GetProcAddress(hModule,"disconnect");
if(disconnect!=NULL){
disconnect();
}else{
cout<<"无法调用函数disconnect"<<endl;
}
//释放资源
FreeLibrary(hModule);
}
}

本文展示了如何在C++中使用`LoadLibrary`和`GetProcAddress`函数来动态加载并调用名为cygmysharelib.dll的库中的`connect`和`disconnect`函数。

2491

被折叠的 条评论
为什么被折叠?



