C#如何调用C++的dll(代码示范如何转换char*、unsigned long、unsigned long*、enum参数)

这个我试了很多次,都没成功。

最后我下载了别人的例子(工程)。

通过对比借鉴别人的写法,很快就成功了。

最主要是这里的写法格式问题:

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

namespace test_imp_dll
{
    class CPPDLL
    {
        public enum my_enum { A, B, C };
        const String str = @"D:Files	est	estdll.dll";
        [DllImport(str, EntryPoint = "add", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int add(int a, int b);static void Main(string[] args)
        {
            Console.WriteLine("Hello World!
");
            int ret;
            ret = CPPDLL.add(1, 2);
            Console.WriteLine(ret);
        }
    }
}

然后我遇到了个很奇怪的问题

如果我拿C#的ref ulong,对照C++的unsigned long*,就运行正常

但我用C#的ulong对照C++的unsigned long就会出错

我在网上找到下面这张C++与C#数据类型的对照表……上面明明写着ulong是对应unsigned long的啊!?

可见也是人云亦云误人子弟的玩意!

至于如何一劳永逸地解决C#参数类型与C++的对照问题,后面我会说。

参考(网上找到下面这张C++与C#数据类型的对照表,实际误人子弟!):

https://blog.csdn.net/f_957995490/article/details/108066822
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++:charINT8SBYTECHAR ---- c#:System.SByte
//c++:shortshort intINT16SHORT ---- c#:System.Int16
//c++:intlonglong intINT32LONG32BOOL , INT ---- c#:System.Int32
//c++:__int64INT64LONGLONG ---- c#:System.Int64
//c++:unsigned charUINT8UCHAR , BYTE ---- c#:System.Byte
//c++:unsigned shortUINT16USHORTWORDATOMWCHAR , __wchar_t ---- c#:System.UInt16
//c++:unsignedunsigned intUINT32ULONG32DWORD32ULONGDWORDUINT ---- c#:System.UInt32
//c++:unsigned __int64UINT64DWORDLONGULONGLONG ---- c#:System.UInt64
//c++:float, FLOAT ---- c#:System.Single
//c++:doublelong doubleDOUBLE ---- 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

上表就是在网上找的误人子弟的玩意

接着说,我是怎么彻底解决这个问题的

我用sizeof去求ulong的字节数,发现是8字节

然后用sizeof去求unsigned long的字节数,发现是4字节

C#使用ulong类型对应C++unsigned long类型出错,可能是因为字节长度不匹配,进而越界

而指针,之所以没问题,大概因为,不管什么类型的指针,指针长度本身就是四字节。在调用时候,至少参数长度是一致的

为了对应unsigned long四字节的长度,我选择了C#的System.Int32作为参数(四字节),就不会出错了

下面是我的代码,分别示范了C#对于带有enum类型参数、char*类型参数、unsigned long和unsigned long*类型参数的C++函数的调用:

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

namespace test_imp_dll
{
    class CPPDLL
    {
        public enum my_enum { A, B, C };
        const String str = @"D:Files	est	estdll.dll";
        [DllImport(str, EntryPoint = "add", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int add(int a, int b);

        [DllImport(str, EntryPoint = "get_str_len", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public static extern int get_str_len(byte[] str);

        [DllImport(str, EntryPoint = "test_enum", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static int test_enum(my_enum m);

        [DllImport(str, EntryPoint = "test_unsigned_long_and_unsigned_long_ptr", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static int test_unsigned_long_and_unsigned_long_ptr(System.Int32 tmp_ulong, ref System.Int32 tmp_ulong_ptr);

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!
");
            int ret;
            ret = CPPDLL.add(1, 2);
            Console.WriteLine("add返回值:" + ret);
            Console.WriteLine("
");
            string tt = "hahaha";
            byte[] ss = System.Text.Encoding.Default.GetBytes(tt);
            ret = CPPDLL.get_str_len(ss);
            Console.WriteLine("get_str_len返回值:" + ret);
            Console.WriteLine("
");
            Console.Write("test_enum返回值:"+CPPDLL.test_enum(my_enum.C));
            Console.WriteLine("
");
            Console.WriteLine("C#的System.Int32字节数为(与C++ unsigned long的长度对应):" + sizeof(System.Int32));
            Console.WriteLine("
");
            System.Int32 tmp2 = 2;
            test_unsigned_long_and_unsigned_long_ptr(2,ref tmp2);
            Console.WriteLine("test_unsigned_long_and_unsigned_long_ptr返回值:"+ tmp2);
            Console.WriteLine("
");
        }
    }
}

对应的C++库函数的代码:

#include "pch.h"
#include "test_dll.h"

int add(int a, int b)
{
    return a + b;
}

int get_str_len(char* str)
{
    return strlen(str);
}

int test_enum(my_enum m)
{
    return (int)m;
}

int test_unsigned_long_and_unsigned_long_ptr(unsigned long tmp_ulong, unsigned long* tmp_ulong_ptr)
{
    if (tmp_ulong == 1)
        *tmp_ulong_ptr = 1;
    else
        *tmp_ulong_ptr = 2;
    return 0;
}

我也将我的demo上传到了github:

https://github.com/SonnAdolf/test_csharp_imp_cplusplus_dll

问题的关键是,一定要让参数的数据类型长度对应……

原文地址:https://www.cnblogs.com/rixiang/p/14591429.html