c# - 如何管理 C 和 C# 之间的缓冲区

标签 c# xcode memory-management interop unity3d

问题

我有一个 C# 脚本,它通过 System.Runtime.Interop 调用 C 函数。我设法调用了 C 函数,但在管理 C 和 C# 之间的缓冲区时遇到了问题。

在我的情况下,C 是(数据)生产者,C# 是消费者。

我的问题是,当我在 C# 中读取数据时,有时会得到正确的值,但有时会得到 NULL。

这个问题已经解决了。我把我错误的做法和正确的做法贴在这里与大家分享。

背景

C# 代码是一个统一脚本(Mono 开发的一部分),而 C 代码在 Xcode 中,这意味着我不能在我的 C 代码中使用 .Net 框架函数。

错误的方法(不时给我 NULL)

这是我的 C 代码(写入缓冲区和从缓冲区读取):

static char InteropBF[INTEROP_BUFFER_SIZE];
static int mutex = 0;
// for the c code to put its message in buffer
void PutToBuffer(char* name, char* content)
{
    while (mutex>0);
    mutex++;
    strcat(InteropBF, name);
    strcat(InteropBF, ",");
    strcat(InteropBF, content);
    strcat(InteropBF, ",");
    printf("Interop Buffer: %s\n", InteropBF);
    mutex--;
}


// for the C# code to poll and read from C
void* ReadFromBuffer(void* temp)
{
    while (mutex>0);
    mutex++;
    strcpy(temp, InteropBF);
    // memcpy(temp, InteropBF, INTEROP_BUFFER_SIZE);
    strcpy(InteropBF, "");
    mutex--;
    return temp;
}

我将函数 ReadFromBuffer() 公开给 C#:

[DllImport ("CCNxPlugin")]
public static extern IntPtr ReadFromBuffer(IntPtr temp);

然后,我这样调用函数:

        IntPtr temp = Marshal.AllocCoTaskMem(8912);
        CCN.ReadFromBuffer(temp);
        string news = Marshal.PtrToStringAuto(temp);
        if(news != "")
        {
            print (news);
        }
        Marshal.FreeCoTaskMem(temp);

使用这段代码,我有时会得到正确的缓冲区内容,但更常见的是,我从 Marshal.PtrToStringAuto 函数中得到 NULL。

正确的方法(非常感谢大家!)

我想粘贴我在此处找到的工作代码和引用资料 --

C 函数:

struct bufnode
{
    char* name;
    char* content;
    struct bufnode *next;
};

struct bufnode* bufhead = NULL;
struct bufnode* buftail = NULL;
// for the c code to put its message in buffer
void PutToBuffer(char* name, char* content)
{
    struct bufnode *temp = malloc(sizeof(struct bufnode));
    temp->name = malloc(256);
    temp->content = malloc(256);
    strcpy(temp->name,name);
    strcpy(temp->content,content);
    temp->next = NULL;

    if (bufhead == NULL && buftail == NULL) {
        bufhead = temp;
        buftail = temp;
    }
    else if(bufhead != NULL && buftail != NULL){
        buftail->next = temp;
        buftail = temp;
    }
    else {
        printf("Put to buffer error.\n");
    }    
}

// for the C# code to poll and read from C
struct bufnode* ReadFromBuffer()
{
    if (bufhead != NULL && buftail != NULL) {
        // temp->name = bufhead->name;
        // temp->content = bufhead->content;
        // temp->next = NULL;
        struct bufnode* temp = bufhead;
        if (bufhead == buftail) {
            bufhead = NULL;
            buftail = NULL;
        }
        else {
            bufhead = bufhead->next;
        }

        return temp;
    }
    else if(bufhead == NULL && buftail == NULL)
    {
        return NULL;
    }
    else {
        return NULL;
    }
}

C# 包装器:

[StructLayout (LayoutKind.Sequential)]
public struct bufnode 
{
    public string name;
        public string content;
        public IntPtr next;
}


[DllImport ("CCNxPlugin")]
public static extern IntPtr ReadFromBuffer();

在 C# 中调用函数:

        CCN.bufnode BufNode;
        BufNode.name = "";
        BufNode.content = "";
        BufNode.next = IntPtr.Zero;

        IntPtr temp = CCN.ReadFromBuffer();
        if(temp != IntPtr.Zero)
        {
            BufNode = (CCN.bufnode)Marshal.PtrToStructure(temp, typeof(CCN.bufnode));
            print(BufNode.name);
            print(BufNode.content);
            Marshal.FreeCoTaskMem(temp);
        }

总结

  1. char[] 似乎不是 C 和 C# 之间的良好缓冲区(至少在我使用 Unity-Mono 和 Xcode 的情况下)。我的建议是在结构中组织数据并将结构作为参数或作为返回值传递给 C#。我找到了关于传递类和结构的很好的文档,但我还没有找到任何关于单独传递 char 数组的文档。所以我想最好将 char[] 包装在结构或类中。
  2. C 结构可以编码为 C# 类或 C# 结构。将包装 class 作为参数传递给非托管函数将起作用。将包装 struct 作为参数传递给非托管函数也可以。从非托管函数返回指向 struct 的指针是可以的。从非托管函数返回指向 的指针是的。 (找到了一个很棒的教程:http://www.mono-project.com/Interop_with_Native_Libraries#Summary)

最佳答案

您正在传回一个堆栈变量。该变量可以在从方法返回时在 C/C++ 中“收集”或释放——这将在它到达 PtrToStringAuto 时随机导致内存错误。这可以解释为什么它有时 为空。您需要分配传递回 C# 代码的内存,并且该代码需要释放内存(或者 C/C++ 需要以某种方式执行此操作)。

您可以在 c/c++ 中使用 CoTaskMemAlloc 执行此操作,在 C# 中使用 Marshal.FreeCoTaskMem 释放它。

关于c# - 如何管理 C 和 C# 之间的缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389178/

相关文章:

c# - 获得 Guid.NewGuid () 副本的机会有多大?

c# - 用一些颜色或标签注释代码

ios - 从 Sandbox 中的 DAE 模型加载 SCNScene 对象

ios - SideMenu 左侧未使用屏幕的整个高度

c++ - 用c++释放内存的最好方法是什么

c - 获取包含动态分配内存的结构的大小

c - 关于一个小型内存分配程序的问题(41行)

c# - 从原始 ECG 信号中提取心率的算法

c# - IQueryable 与 IEnumerable : is IQueryable always better and faster?

xcode - 如何在 Xcode 4 中堆叠编辑器