在C++中如何调用第三方dll动态链接库呢?方法很简单,只需要将第三方的dll文件放置在"项目名称\Debug"目录下,然后使用LoadLibrary函数导入即可,注意点:如果您引入的DLL不是C++语言开发的,则需要加入“_stdcall”关键字,请看下面的案例:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
typedef int(_stdcall *Func)(int &, int &);
short main()
{
HMODULE hDll = LoadLibrary(L"hostcm32.dll");
if (hDll != NULL)
{
Func open = (Func)GetProcAddress(hDll, "hst_open");
int com = 1;
int param = 2;
if (open != NULL)
{
int result = open(com, param);
cout << "结果:" << result << endl;
}
printf("\r\n");
system("pause");
}
return 0;
}说明:
在C++调用dll动态连接库的时候,如上述要用到LoadLibrary(string)方法中的dll前面要加入大写的“L”,否则会报错。
例如我们要调用hostcm32.dll这个连接库里面的hst_open(int a, int b)方法,则需要在前面“typedef int(_stdcall *Func)(int &, int &);”这样定义一下,Func并不是系统的函数,而是小编自定义的,你可以随便写什么。
然后通过“Func open = (Func)GetProcAddress(hDll, "hst_open");”这样调用dll里面的“hst_open”方法,open表示的就是“hst_open”方法。