zl程序教程

您现在的位置是:首页 >  硬件

当前栏目

Unity 将托管内存封送到非托管内存

内存 Unity 托管 送到
2023-06-13 09:11:38 时间

C++代码通常被称为非托管代码,而我们在Unity中使用的C#代码会被编译成通用中间语言CIL,CIL/.Net代码通常被称为托管代码。那我们如何在Unity中调用由C++封装的dll动态库中接口时将托管内存封装到非托管内存?

Marshal类为此提供了方法集合,例如StringToHGlobalAnsi方法将ANSI字符从托管堆中的指定字符串复制到非托管堆中的缓冲区。

图例为C++代码封装的一个dll动态库,包含以下接口:

public static class Socket
{
    /// <summary>
    /// 初始化函数
    /// </summary>
    /// <param name="path">配置文件目录</param>
    /// <returns>成功返回0 否则返回错误代码</returns>
    [DllImport("HWSocket.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int HW_Initialize(IntPtr path);
    /// <summary>
    /// 发送数据函数
    /// </summary>
    /// <param name="buf">要发送数据的缓冲区地址</param>
    /// <param name="size">要发送的数据大小 最大为900</param>
    /// <returns>成功返回0 否则返回错误代码</returns>
    [DllImport("HWSocket.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int HW_Send(IntPtr buf, int size);
    /// <summary>
    /// 接收数据函数
    /// </summary>
    /// <param name="id">终端ID(字符串,表示接收哪一台终端的数据)</param>
    /// <param name="buf">接收数据缓冲区地址</param>
    /// <returns>读取到的数据大小 小等于0表示没有读取到数据</returns>
    [DllImport("HWSocket.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int HW_Read(IntPtr id, IntPtr buf);
    /// <summary>
    /// 关闭函数 
    /// </summary>
    /// <returns>成功返回0 否则返回错误代码</returns>
    [DllImport("HWSocket.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int HW_Close();
}

以Send函数的使用为例:

//申请内存
sendIntPtr = Marshal.AllocHGlobal(1024);
//开启发送消息线程
sendThread = new Thread(() =>
{
    while (true)
    {
        try
        {
            //将发送队列中消息依次取出发送
            if (sendQueue.Count > 0)
            {
                var msg = sendQueue.Dequeue();
                sendIntPtr = Marshal.StringToHGlobalAnsi(msg);
                Socket.HW_Send(sendIntPtr, msg.Length);
            }
        }
        catch (Exception e)
        {
            Debug.LogError($"Send Error : {e.Message}");
        }
        Thread.Sleep(0);
    }
});
sendThread.Start();