1、新建win32项目,项目取名为simpledll(随你自己取,最好用英文,不要包含中文)
2、勾选DLL(d),空项目,如下图所示
点击完成
3、添加新建头文件
选择(.h),取个对应的名字
4、添加新建源文件
选择C++文件(.cpp),同样取个名字
5、在simpledll.h文件中添加如下代码:
#pragma once; #include<iostream> #ifdef DLL_IMPLEMENT #define DLL_API _declspec(dllexport) #else #define DLL_API _declspec(dllimport) #endif extern "C" DLL_API struct idata* rtu(int d, int e, int f); extern "C" DLL_API int ssd(struct idata* lgd); extern "C" DLL_API int add(int a, int b, int c, struct idata* d); //extern "C"是解决C与C++的兼容问题
6、在simpledll.cpp文件中添加如下代码:
#define DLL_IMPLEMENT #include"simpledll.h" #include<windows.h> #include<intrin.h> #include<stdlib.h> #include<string.h> #include<stdio.h> struct idata { int a; int b; int c; }; _declspec(dllexport) extern "C" struct idata* rtu(int d, int e, int f) { struct idata edg; struct idata* rng; edg.a = d; edg.b = e; edg.c = f; rng = &edg; return rng; } //初始化结构体数据 _declspec(dllexport) extern "C" int ssd(struct idata* lgd) { struct idata* rtu(int d, int e, int f); *lgd = *rtu(1, 2, 3); return 0; }//赋值结构体数据到另一个结构体 _declspec(dllexport) extern "C" int add(int a, int b, int c, struct idata* d) { d->a = a; d->b = b; d->c = c; return a+b+c; }//结构体内数据求和
7、点击 生成/生成解决方案
ok 成功
下一篇介绍如何调用该生成的DLL