How can I copy a array of strings into an unmanaged char double pointer?

↘锁芯ラ 提交于 2020-01-15 15:42:03

问题


I have a char** in a C struct which is allocated in the C code as a Nx128 matrix. In C# I have an array of strings and I want to copy this array to the char double pointer, without reallocating anything. I tried this:

public void StringArrayToPtr(IntPtr ptr, string[] array) 
{

    for (int i = 0; i < array.Length; i++)
    {
        char[] chars = (array[i] + '\0').ToCharArray();

        Marshal.Copy(chars, 0, IntPtr.Add(ptr, 128*i), chars.Length);
    }
}

But this doesn't work. Does somebody know how to perform such a copy ?

UPDATE:

Here is how my char** names is allocated in the C code for 3 items: names = (char **) MallocArray2D (3, 128, sizeof ( char ));

Here is the details of the MallocArray2D method:

void ** MallocArray2D (
     int  n1,
     int  n2,
     int  size_elem )
{
    void ** p2;
    void * p1;
    size_t i;

    p1 = (void *) malloc (size_elem * n1 * n2); 

    p2 = (void **) malloc (n1 * sizeof ( void * ));


    for ( i = 0 ; i < n1 ; i++ )
    {
        p2[i] = (char *) p1 + size_elem * n2 * i;
    }

    return p2;
}

This MallocArray2D method is called into a MallocImage which is exposed in my C# code.

Here is the MallocImage method interesting part in C code:

int MallocImage (
     IMAGE *  image,
     int           nxyz,
     int           nvar )
{
    //... Allocating others objects

    image->names = (char **) MPDSMallocArray2D ( nvar, 128, sizeof ( char ));

}

Now my C# exposed MallocImage method:

[DllImport(DLL_PATH, CallingConvention = CallingConvention.Cdecl)]
public static extern int MallocImage([In, Out]Image image, int nXYZ, int nVar);

// My Image class
[StructLayout(LayoutKind.Sequential)]
class Image {

    private IntPtr names;

    public string[] Names {

        set {ArrayOfStringToPtr(names, value);}

    }

    // Constructor
    public Image(int nVar, int nXYZ) {

        MallocImage(this, nXYZ, nVar);

    }

}


// Somewhere else in my code
image.Names = new string[] {"porosity", "duplicity", "facies"];

回答1:


A System.Char is a unicode 16 bits character [MSDN]. You probably work with ASCII string.

The pointer arithmetic seems wrong here, since you are working with a pointer who points to an array of three pointers you probably need to get the address of the string using : Marshal.ReadIntPtr(ptr, i * IntPtr.Size) thus the resulting code will be :

public static void StringArrayToPtr(IntPtr ptr, string[] array)
{
    for (int i = 0; i < array.Length; i++)
    {
        byte[] chars = System.Text.Encoding.ASCII.GetBytes(array[i] + '\0');
        Marshal.Copy(chars, 0, Marshal.ReadIntPtr(ptr, i * IntPtr.Size), chars.Length);
    }
}


来源:https://stackoverflow.com/questions/17091618/how-can-i-copy-a-array-of-strings-into-an-unmanaged-char-double-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!