您的位置:首页 > 编程语言 > C语言/C++

C函数的DLL,C++动态调用。

2016-12-15 10:02 197 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: