1、项目结构
2、建立c项目,其实很简单,只要源文件是.c的即可。
CDLL.h头文件
#ifndef __CDLL_H__
#define __CDLL_H__
extern int _declspec(dllexport) foo(int x, int y);
#endif
CDLL.c文件
#include "CDLL.h"
int foo(int x, int y)
{
return x + y;
}
3、C++动态调用
#include "DLLDemo.h"
#include<iostream>
#include<Windows.h>
using namespace std;
typedef int(*lp)(int, int);
int main()
{
HINSTANCE hdll;
lp func;
hdll = LoadLibrary("CDLL.dll"); //或者:hdll = LoadLibrary("../Debug/CDLL.dll"); 要保证相对路径正确
if (hdll != NULL)
{
func = (lp)GetProcAddress(hdll, "foo");
cout << func(2, 3) << endl;
}
FreeLibrary(hdll);
return 0;
}