C++ 动态链接库DLL创建及使用

一、动态链接库DLL创建

使用VS2022 创建

1、创建新解决方案

 创建即可

2、创建动态链接库新项目

右键解决方案

语言选择C++,选择动态链接库

填入项目名称,勾选:将解决方案和项目放在同一目录中

点击创建

3、创建后,显示dllmain.cpp文件,新建.cpp和.h文件

 

点击右侧项目,右键——添加 ——新建项目(ctrl+shift+a)

修改名称,新建.cpp和.h文件

4、在.h和.cpp中填写代码

.h文件

#pragma once

#ifdef FIBONACCILIBRARY_EXPORTS
#define FIBONACCILIBRARY_API __declspec(dllexport)
#else
#define FIBONACCILIBRARY_API __declspec(dllimport)
#endif

/*
斐波那契递归关系描述了一个序列F
其中F(n) =  n = 0, a
                    n = 1, b
                    n > 1, F(n-2) + F(n-1)

对于一些初始积分值a和b。
如果序列初始化为F(0) = 1, F(1) = 1,
那么这个关系就产生了著名的斐波那契
序列:1,1,2,3,5,8,13,21,34,…
*/

/*
//初始化一个斐波那契关系序列
// F(0) = a, F(1) = b。
//该函数必须在其他函数之前调用。
*/
void InitFibonacci(const unsigned long long a, const unsigned long long b);

/*
生成序列中的下一个值。
成功返回true并更新当前值和索引;
溢出时为false,保持当前值和索引不变。
*/
bool NextFibonacci();

// 获取序列中的当前值
unsigned long long CurrentFibonacci();

// 获取当前值在序列中的位置
unsigned IndexFibonacci();

// 运行斐波那契函数
extern "C" FIBONACCILIBRARY_API void RunFibonacci(const unsigned long long a, const unsigned long long b);

.cpp文件

#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#include <utility>
#include <limits.h>
#include <iostream>
#include "FibonacciLibrary.h"

static unsigned long long _previous;
static unsigned long long _current;
static unsigned _index;              

void InitFibonacci(const unsigned long long a, const unsigned long long b)
{
    _index = 0;
    _current = a;
    _previous = b;
}

bool NextFibonacci()
{
    if ((ULLONG_MAX - _previous < _current) ||
        (UINT_MAX == _index))
    {
        return false;
    }

    if (_index > 0)
    {
        _previous += _current;
    }
    std::swap(_current, _previous);
    ++_index;
    return true;
}

unsigned long long CurrentFibonacci()
{
    return _current;
}

unsigned IndexFibonacci()
{
    return _index;
}

void RunFibonacci(const unsigned long long a, const unsigned long long b)
{
    InitFibonacci(a, b);

    do {
        std::cout << IndexFibonacci() << ": " << CurrentFibonacci() << std::endl;
    } while (NextFibonacci());

    std::cout << IndexFibonacci() + 1 << " Fibonacci sequence values fit in an unsigned 64-bit integer." << std::endl;
}

5、重要***:配置动态链接库

① 生成lib和dll文件到本项目目录中(生成到——> \x64\Debug 中)

将输出目录中SolutionDir改为ProjectDir

② 重新生成,出现生成成功字样,生成到本项目的Debug中

二、c++客户端应用调用DLL库

1、创建c++客户端应用

右键解决方案——添加——新建项目

填入项目名称和保存位置

创建完成后,弹出xxx.cpp  int main方法中输出Hello World 

2、在.cpp文件中添加代码

#include <iostream>
#include "FibonacciLibrary.h"

int main()
{
    RunFibonacci(1, 1);
}

3、重点*** 对客户端应用程序进行设置

① 将DLL标头(.h)添加到包含路径(类似在应用程序中添加include文件夹)

注意:活动(Debug)需要改为所有配置

相对路径为..\xxxDll ,如果不在一个解决方案的文件夹中,请确定指定文件夹

② DLL导入库添加到应用程序项目中  (添加的是DLL中的.lib

附加库目录添加 (即 ...\xxxDll\x64\Debug的路径添加)

如果Dll项目和应用程序项目在一个文件夹下,直接 ..\xxxiDll\$(IntDir)

添加生成后.dll

添加的内容为: 

xcopy /y /d "..\xxx\$(IntDir)xxx.dll" "$(OutDir)"

④ 将生成的exe保存到自己的项目目录中(不修改,默认保存到解决方案目录中)

由 SolutionDir 改为 ProjectDir

完成以上步骤后,可将Dll项目中的 xxx.dll文件生成到应用程序中,可调用xxx.dll、xxx.lib和头文件

F5生成结果

三、C#调用c++动态库Dll的方法

能够调用方法,但是存在问题

调用方法

1、新建C#项目

2、运行一次项目,生成/bin/Debug/.net6.0/xxx.exe

3、将xxx.dll文件粘贴到.exe相同文件夹中

4、编写调用方法代码(调用时,需要知道方法的名称和参数)

// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dlltest
{
    class Program
    {
        // 导入自定义的dll
        [DllImport("CoordinateFibonacciDll.dll")]
        public static extern void RunFibonacci(ulong a, ulong b);

        public static void Main(string[] args)
        {
            RunFibonacci(1, 1);
        }
    }
}

C++与C#数据类型对照表

C++中的DLL函数原型为
extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned char* 变量名2)
extern "C" __declspec(dllexport) bool 方法名二(const unsigned char* 变量名1, char* 变量名2)
 
 
C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试
        c++:HANDLE(void   *)          ----    c#:System.IntPtr
        c++:Byte(unsigned   char)     ----    c#:System.Byte
        c++:SHORT(short)              ----    c#:System.Int16
        c++:WORD(unsigned   short)    ----    c#:System.UInt16
        c++:INT(int)                  ----    c#:System.Int16
        c++:INT(int)                  ----    c#:System.Int32
        c++:UINT(unsigned   int)      ----    c#:System.UInt16
        c++:UINT(unsigned   int)      ----    c#:System.UInt32
        c++:LONG(long)                ----    c#:System.Int32
        c++:ULONG(unsigned   long)    ----    c#:System.UInt32
        c++:DWORD(unsigned   long)    ----    c#:System.UInt32
        c++:DECIMAL                   ----    c#:System.Decimal
        c++:BOOL(long)                ----    c#:System.Boolean
        c++:CHAR(char)                ----    c#:System.Char
        c++:LPSTR(char   *)           ----    c#:System.String
        c++:LPWSTR(wchar_t   *)       ----    c#:System.String
        c++:LPCSTR(const   char   *)  ----    c#:System.String
c++:LPCWSTR(const   wchar_t   *)      ----    c#:System.String
        c++:PCAHR(char   *)           ----    c#:System.String
        c++:BSTR                      ----    c#:System.String
        c++:FLOAT(float)              ----    c#:System.Single
        c++:DOUBLE(double)            ----    c#:System.Double
        c++:VARIANT                   ----    c#:System.Object
        c++:PBYTE(byte   *)           ----    c#:System.Byte[]
 
 
        c++:BSTR                      ----    c#:StringBuilder
        c++:LPCTSTR                   ----    c#:StringBuilder
        c++:LPCTSTR                   ----    c#:string
        c++:LPTSTR                    ----    c#:[MarshalAs(UnmanagedType.LPTStr)] string
        c++:LPTSTR 输出变量名          ----    c#:StringBuilder 输出变量名
        c++:LPCWSTR                   ----    c#:IntPtr
        c++:BOOL                      ----    c#:bool  
        c++:HMODULE                   ----    c#:IntPtr   
        c++:HINSTANCE                 ----    c#:IntPtr
        c++:结构体                     ----    c#:public struct 结构体{};
        c++:结构体 **变量名            ----    c#:out 变量名   //C#中提前申明一个结构体实例化后的变量名
        c++:结构体 &变量名             ----    c#:ref 结构体 变量名
        
 
 
        c++:WORD                      ----    c#:ushort
        c++:DWORD                     ----    c#:uint
        c++:DWORD                     ----    c#:int
 
 
        c++:UCHAR                     ----    c#:int
        c++:UCHAR                     ----    c#:byte
        c++:UCHAR*                    ----    c#:string
        c++:UCHAR*                    ----    c#:IntPtr
 
 
        c++:GUID                      ----    c#:Guid
        c++:Handle                    ----    c#:IntPtr
        c++:HWND                      ----    c#:IntPtr
        c++:DWORD                     ----    c#:int
        c++:COLORREF                  ----    c#:uint
 
 
 
 
        c++:unsigned char            ----    c#:byte
        c++:unsigned char *          ----    c#:ref byte
       c++:unsigned char *           ----    c#:[MarshalAs(UnmanagedType.LPArray)] byte[]
        c++:unsigned char *          ----    c#:[MarshalAs(UnmanagedType.LPArray)] Intptr
 
 
        c++:unsigned char &           ----    c#:ref byte
        c++:unsigned char 变量名      ----    c#:byte 变量名
        c++:unsigned short 变量名     ----    c#:ushort 变量名
        c++:unsigned int 变量名       ----    c#:uint 变量名
        c++:unsigned long 变量名      ----    c#:ulong 变量名
 
 
c++:char 变量名  ----  c#:byte 变量名   //C++中一个字符用一个字节表示,C#中一符用两个字节表示
c++:char 数组名[数组大小]  ----  c#:MarshalAs(UnmanagedType.ByValTStr, SizeConst = 数组大小)]        public string 数组名; ushort
 
 
        c++:char *                   ----    c#:string       //传入参数
        c++:char *                   ----    c#:StringBuilder//传出参数
        c++:char *变量名              ----    c#:ref string 变量名
        c++:char *输入变量名          ----    c#:string 输入变量名
        c++:char *输出变量名          ----    c#:[MarshalAs(UnmanagedType.LPStr)] StringBuilder 输出变量名
 
 
        c++:char **                  ----    c#:string
        c++:char **变量名             ----    c#:ref string 变量名
        c++:const char *             ----    c#:string
        c++:char[]                   ----    c#:string
        c++:char 变量名[数组大小]     ----    c#:[MarshalAs(UnmanagedType.ByValTStr,SizeConst=数组大小)] public string 变量名;
 
 
        c++:struct 结构体名 *变量名   ----    c#:ref 结构体名 变量名
        c++:委托 变量名               ----    c#:委托 变量名
 
 
        c++:int                      ----    c#:int
        c++:int                      ----    c#:ref int
        c++:int &                    ----    c#:ref int
        c++:int *                    ----    c#:ref int  C#中调用前需定义int 变量名 = 0;
 
 
        c++:*int                     ----    c#:IntPtr
        c++:int32 PIPTR *            ----    c#:int32[]
        c++:float PIPTR *            ----    c#:float[]
       
 
 
        c++:double** 数组名           ----    c#:ref double 数组名
        c++:double*[] 数组名          ----    c#:ref double 数组名
        c++:long                     ----    c#:int
        c++:ulong                    ----    c#:int
       
        c++:UINT8 *     ----    c#:ref byte C#中调用前需定义byte 变量名 = new byte();       
 
 
 
 
        c++:handle                   ----    c#:IntPtr
        c++:hwnd                     ----    c#:IntPtr
       
       
        c++:void *                   ----    c#:IntPtr       
        c++:void * user_obj_param    ----    c#:IntPtr user_obj_param
 c++:void * 对象名称           ----    c#:([MarshalAs(UnmanagedType.AsAny)]Object 对象名称
 
 
 
 
       
c++:char, INT8, SBYTE, CHAR                               ----    c#:System.SByte 
c++:short, short int, INT16, SHORT                        ----    c#:System.Int16 
c++:int, long, long int, INT32, LONG32, BOOL , INT        ----    c#:System.Int32 
c++:__int64, INT64, LONGLONG                              ----    c#:System.Int64 
c++:unsigned char, UINT8, UCHAR , BYTE                    ----    c#:System.Byte 
c++:unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t----c#:System.UInt16 
c++:unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT-c#:System.UInt32 
c++:unsigned __int64, UINT64, DWORDLONG, ULONGLONG        ----    c#:System.UInt64 
c++:float, FLOAT                                          ----    c#:System.Single 
c++:double, long double, DOUBLE                           ----    c#:System.Double 
 
 
Win32 Types ----  CLR Type 
       
 
 
Struct需要在C#里重新定义一个Struct
CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);
 
 
unsigned char** ppImage替换成IntPtr ppImage
int& nWidth替换成ref int nWidth
int*, int&, 则都可用 ref int 对应
双针指类型参数,可以用 ref IntPtr
函数指针使用c++: typedef double (*fun_type1)(double); 对应 c#:public delegate double  fun_type1(double);
char* 的操作c++: char*; 对应 c#:StringBuilder;
c#中使用指针:在需要使用指针的地方 加 unsafe
 
 
 
 
unsigned   char对应public   byte
typedef void (*CALLBACKFUN1W)(wchar_t*, void* pArg);
typedef void (*CALLBACKFUN1A)(char*, void* pArg);
bool BIOPRINT_SENSOR_API dllFun1(CALLBACKFUN1 pCallbackFun1, void* pArg);
调用方式为
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CallbackFunc1([MarshalAs(UnmanagedType.LPWStr)] StringBuilder strName, IntPtr pArg);

四、Python调用c++动态库dll的方法

import ctypes

if __name__ == '__main__':
    # print(123)
    obj = ctypes.CDLL('CoordinateFibonacciDll.dll')
    obj.RunFibonacci()

需注意:结果的数据类型不太一致,需要对应指定的类型

可以在动态链接库的接口处获取值,之后转换为对应的类型

五、C++ 创建DLL动态链接库  新建其他C++项目调用

注:与上方 二 的区别

新建的C++项目需要拷贝 xxx.h、xxx.lib和xxx.dll文件

具体步骤(使用VS2022版本):

——创建DLL项目

  1. 新建项目  选择:空项目 c++  勾选将解决方案和项目放在同一个目录中
  2. 右键右侧项目名称,添加 类,填入类名称如myDLL
  3. myDLL.h  class __declspec(dllexport)myDLL{public: static void showTest}  myDLL.cpp  void myDLL::showTest(){...}
  4. 项目--属性--配置属性的常规--配置类型--改为动态库.dll
  5. 生成--生成解决方案 或 重新生成解决方案
  6. 需要提供给其他项目的文件  项目中的myDLL.h  x64/Debug/项目名.dll  项目名.lib

——创建c++项目调用生成的dll

  1. 新建项目
  2. 项目中新建 类
  3. 右键项目 在文件资源管理器中打开文件夹  新建include文件夹(将myDLL.h拷贝此处) 和 lib文件夹(将项目名.lib拷贝此处)
  4. 项目--属性--VC++目录  包含目录-添加新建的include文件夹目录 ; 库目录-添加新建的lib文件夹目录
  5. 链接器--输入--附加依赖项  添加:项目名.lib
  6. F5运行,报错:无法打开 项目名.dll,需要将项目名.dll 添加到x64/Debug中

——总结

项目输出DLL,需要在类前面加关键字 __declspec(exportdll),并将项目属性设置为dll;
其他项目调用,需要dll、lib、.h 三种文件

  • 16
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,可以使用动态链接库DLL)的方式实现代码模块的重用。下面是一个简单的例子,演示如何在 Windows 系统中创建一个简单的动态链接库: 首先,创建一个新的 C++ 项目,并在该项目中添加一个新的源文件。 将以下代码粘贴到新的源文件中: ``` #include <iostream> #include "example.h" using namespace std; __declspec(dllexport) int add(int a, int b) { return a + b; } __declspec(dllexport) int subtract(int a, int b) { return a - b; } ``` 其中,`__declspec(dllexport)` 用于指定该函数是动态链接库中可导出的函数。 然后,创建一个名为 `example.h` 的头文件,并在其中声明 `add()` 和 `subtract()` 函数: ``` #pragma once __declspec(dllexport) int add(int a, int b); __declspec(dllexport) int subtract(int a, int b); ``` 保存并编译代码,然后生成 DLL 文件。在 Visual Studio 中,可以通过选择“生成”菜单中的“生成解决方案”选项来生成 DLL 文件。 最后,在另一个项目中使用DLL 文件。在该项目中,需要包含 `example.h` 头文件,并链接到 DLL 库。这可以通过在 Visual Studio 中的“属性” -> “链接器” -> “输入”选项卡中设置来完成。 现在,可以在另一个源文件中使用 `add()` 和 `subtract()` 函数: ``` #include <iostream> #include "example.h" using namespace std; int main() { int a = 10, b = 5; cout << "a + b = " << add(a, b) << endl; cout << "a - b = " << subtract(a, b) << endl; return 0; } ``` 编译并运行该代码,将会输出以下内容: ``` a + b = 15 a - b = 5 ``` 这就是使用 C++ 创建动态链接库的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值