C++全局函数的dll,C++动态调用。

来源:互联网 发布:centos cuda8安装 编辑:程序博客网 时间:2024/06/17 23:03

1、项目结构
这里写图片描述
其实很简单,只要将CDLL.c文件的后缀改为.cpp即可。
2、
CDLL.h内容:

#ifndef __CDLL_H__#define __CDLL_H__extern "C" int _declspec(dllexport) foo(int x, int y);#endif

这里多了一个”C”,是要求C++编译器在编译foo函数的时候,按照C编译器方式编译。

CDLL.cpp的内容:

#include "CDLL.h"int foo(int x, int y){    return x + y;}

3、C++调用:
DLLUse.cpp

#include "DLLDemo.h"#include<iostream>#include<Windows.h>using namespace std;typedef int(*lp)(int, int);int main(){    HINSTANCE hdll;    lp func;    hdll = LoadLibrary("../Debug/CDLL.dll");     if (hdll != NULL)    {        func = (lp)GetProcAddress(hdll, "foo");        cout << func(2, 3) << endl;    }    FreeLibrary(hdll);    return 0;}
0 0