您的位置:首页 > 游戏 > 手游 > C#调用C++/Rust动态链接库 传递或返回字符串参数时避坑

C#调用C++/Rust动态链接库 传递或返回字符串参数时避坑

2024/10/5 20:27:11 来源:https://blog.csdn.net/qq_41601836/article/details/139942320  浏览:    关键词:C#调用C++/Rust动态链接库 传递或返回字符串参数时避坑

1、C#向C++ dll 传入字符串时,参数直接用string,设置编码格式 CharSet.Unicode或者CharSet.Ansi。

C++ dll接收使用wchar_t* 或 char*。

2、C++ dll返回字符串,使用 wchar_t 或char*。

.net 4.0 C#可以直接使用string接收,很方便。

.net 4.0+ C# 用 IntPtr 接收,使用string接收调试不行。
————————————————

C# 的 IntPtr 类型

Dec 10, 2021

C# 中,IntPtr 是一个代表内存位置指针的类型。它被用来存储一个变量或一个对象在内存中的地址。IntPtr 是一个整数类型,但它的大小与平台有关。在 32 位系统中,IntPtr 的大小为 32 比特(4字节),在 64 位系统中,它的大小为 64 比特(8字节)。

IntPtr 通常在处理非托管代码或与其他使用指针的语言相互操作时使用。例如,如果你从动态链接库(DLL)中调用一个以指针为参数的函数,你可以使用IntPtr将一个变量的地址传递给该函数。C# 中主要用它调用 C++\C 封装的 DLL 库

下面主要介绍 IntPtr 的常见用法。

1 .int 类型与 IntPtr 类型之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace MyIntPtr
{class Program{static void Main(string[] args){int nValue1 = 10;int nValue2 = 20;//AllocHGlobal(int cb):通过使用指定的字节数,从进程的非托管内存中分配内存。IntPtr ptr1 = Marshal.AllocHGlobal(sizeof(int));IntPtr ptr2 = Marshal.AllocHGlobal(sizeof(int));//WriteInt32(IntPtr ptr, int val):将 32 位有符号整数值写入非托管内存。//int->IntPtrMarshal.WriteInt32(ptr1, nValue1);Marshal.WriteInt32(ptr2, nValue2);// ReadInt32(IntPtr ptr, int ofs):从非托管内存按给定的偏移量读取一个 32 位带符号整数//IntPtr->intint nVal1 = Marshal.ReadInt32(ptr1, 0);int nVal2 = Marshal.ReadInt32(ptr2, 0);//FreeHGlobal(IntPtr hglobal):释放以前从进程的非托管内存中分配的内存。Marshal.FreeHGlobal(ptr1);Marshal.FreeHGlobal(ptr2);}}
}

2.string 类型与 IntPtr 之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace MyIntPtr
{class Program{static void Main(string[] args){string str = "aa";IntPtr strPtr = Marshal.StringToHGlobalAnsi(str);string ss = Marshal.PtrToStringAnsi(strPtr);Marshal.FreeHGlobal(strPtr);  }}
}

3. 结构体与 IntPtr 之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace MyIntPtr
{class Program{public struct stuInfo{public string Name;public string Gender;public int Age;public int Height;}static void Main(string[] args){stuInfo stu = new stuInfo(){Name = "张三",Gender = "男",Age = 23,Height = 172,};//获取结构体占用空间的大小int nSize = Marshal.SizeOf(stu);//声明一个相同大小的内存空间IntPtr intPtr = Marshal.AllocHGlobal(nSize);//IntPtr->StructMarshal.StructureToPtr(stu, intPtr,true);//Struct->IntPtrstuInfo Info =(stuInfo)Marshal.PtrToStructure(intPtr, typeof(stuInfo));Console.ReadKey();}}
}

4. 调用 C++\C 封装的 DLL 库

Copy codeusing System;
using System.Runtime.InteropServices;namespace IntPtrExample
{class Program{// Import the DLL function that takes an IntPtr argument[DllImport("mylibrary.dll")]static extern void MyFunction(IntPtr ptr);static void Main(string[] args){// Create an integer variableint x = 10;// Allocate memory for the variable and get a pointer to itIntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(x));Marshal.StructureToPtr(x, ptr, false);// Call the DLL function and pass the pointer to the variableMyFunction(ptr);// Free the allocated memoryMarshal.FreeHGlobal(ptr);}}
}

在这个例子中,MyFunction() 函数从一个DLL中导入,并接受一个 IntPtr 参数。Main() 方法使用 Marshal.AllocHGlobal() 方法为一个整数变量分配内存,然后使用一个 IntPtr 变量将该变量的地址传递给 MyFunction()

注意,当使用 IntPtr 时,正确管理被分配的内存是很重要的。在上面的例子中,内存是用 Marshal.AllocHGlobal() 分配的,然后在不再需要时用 Marshal.FreeHGlobal() 释放。未能正确管理内存会导致内存泄漏或其他问题。

参考:
https://blog.csdn.net/tinghe17/article/details/114381046

https://dqdongg.com/c%23/2021/12/10/Csharp-Intptr.html

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com