VS編寫動態鏈接庫DLL(VS2015)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM