#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
int a,b;
cout<<"请输入一个整数a=";
cin>>a;
cout<<endl;
cout<<"请输入一个整数b=";
cin>>b;
cout<<endl;
typedef int(*pTest)(int a,int b);// 函数指针
pTest Test=NULL;
HINSTANCE hDLL;
hDLL=LoadLibrary("dllTest.dll");
Test=(pTest)GetProcAddress(hDLL,"Test");
if (Test)
{
int C=Test(a,b);
cout<<"OK!"<<endl;
}
FreeLibrary(hDLL);
return 0;
}
另一个例子:
#include <stdio.h>
#include <windows.h>
// 函数声明 lpAddFun 是一个指向函数的指针,该函数有两个参数都是int类型,函数的返回值也是int类型
typedef void(*lpAddFun)();
int main(){
HINSTANCE hDll; // DLL句柄
lpAddFun addFun; // 函数指针
char path[] = "d:\\hi.dll";
hDll = LoadLibraryA(path);
if (hDll != NULL){
char fn[] = {'h','i',0};
addFun = (lpAddFun)GetProcAddress(hDll, fn);
if (addFun != NULL){
addFun();
}
FreeLibrary(hDll);
}
system("pause");
return 0;
}