How to manage a buffer between C and C #

Problem

I have a C # script that calls C functions through System.Runtime.Interop. I managed to call the C functions, but I have a buffer management problem between C and C #.

In my situation, C is a producer of data (data), and C # is a consumer.

My problem is that when I read data in C #, sometimes I get the correct value, but sometimes I get NULL.

This problem has already been resolved. I am inserting my wrong approach and my correct approach is here to share with you.

Background

C # code is a script unit (part of Mono development), and C code is in Xcode, which means I cannot use the .Net framework in my C code.

Wrong approach (gives me NULL from time to time)

Here is my C code (write to the buffer and read from the buffer):

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() #:

[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);

, , NULL Marshal.PtrToStringAuto.

( !)

, -

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;
    }
}

#:

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


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

#:

        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);
        }

+3
2

. "" C/++ , PtrToStringAuto. , . , #, ( C/++ -).

c/++ CoTaskMemAlloc #, Marshal.FreeCoTaskMem.

+3

# SysAllocString ++:

// for the C# code to poll and read from C
void ReadFromBuffer(BSTR* pstrRet)
{
    while (mutex>0);
    mutex++;

    *pstrRet=SysAllocString(InteropBF)

    strcpy(InteropBF, "");
    mutex--;
}

//calling from c# 
string news ="";
ReadFromBuffer(out news);
if(news != "")
{
    print (news);
}


# xcode. :

// for the C# code to poll and read from C
unsigned int ReadFromBuffer(char* pstrRet, unsigned int cbSize)
{
    unsigned int uiRet;

    while (mutex>0);
    mutex++;

    uiRet=strlen(InteropBF);

    if (uiRet > 0 && cbSize > uiRet) 
    {    
        strcpy(pstrRet, InteropBF);
    }
    else //error
    {        
        uiRet=0;
    }

    strcpy(InteropBF, "");
    mutex--;

    return uiRet;
}


//calling from c# 
[DllImport ("CCNxPlugin")]
public static extern UInt32 ReadFromBuffer(IntPtr pData, UInt32 cbData);
.
.
. 

String news;
UInt32 cbData=INTEROP_BUFFER_SIZE; //you need to define this in c# ;)
Byte[]  abyData=new byte[cbData];

try
{     
    //kindly request GC gives us a data address & leaves this memory alone for a bit
    GCHandle oGCHData = GCHandle.Alloc(abyData, GCHandleType.Pinned);
    IntPtr pbyData = oGCHData.AddrOfPinnedObject();

    UInt32 cbCopied=ReadFromBuffer(pbyData, cbData);

    oGCHData.Free();

    if(cbCopied > 0)
    { 
        System.Text.Encoding enc = System.Text.Encoding.ASCII;
        news = enc.GetString(abyData,0,cbCopied);
    }
}
catch (Exception e)
{
    System.Diagnostics.Debug.WriteLine(e);
}


0
source

All Articles