Marshaling结构使用从托管代码到非托管代码的字符串和数组

时间:2011-04-20 22:30:45

标签: c# c++ marshalling unmanaged managed

我有一个简单的C#数据结构,包含一个字符串,一个int和一个int的向量:

class MyManagedClass
{
    public string m_Str;
    int m_Int;
    int[] m_IntArray;
}

C ++中的等价物是:

struct myUnmanagedStruct
{
    char* m_Str;
    UINT m_Int;
    UINT* m_IntArray;
}

我有一个非托管函数,它创建了一个myUnmanagedStruct结构数组。编写托管包装器的正确编组数据的最佳方法是什么,以便在非托管端创建的内容正确地传递回托管端? (即我想从MyUnmanagedStructs数组中创建一个MyManagedClass对象数组)

注意:
a)在非托管结构中创建一个字符串 b)在非托管结构中创建一个int向量

到目前为止,我最好的尝试是:

在管理方面:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)]
    public class MyManagedClass
    {
        public MyManagedClass()
        {
            m_IntArray = new int[4];
        }

        public String m_Str;
        public int m_Int;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] m_IntArray;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct UnsafeLCEArray
    {
        public int m_Int;
        public IntPtr m_CharBuf;
        public IntPtr m_IntBuf;
    }

    public unsafe class LibWrap
    {
        // Declares managed prototypes for the unmanaged function.
        [DllImport("mydll.dll", EntryPoint = "GetUnmanagedStructs")]
        public static extern voidGetUnmanagedStructs(out int size, out IntPtr outArray);
    }

在非管理方面:

typedef struct _MyUnmanagedStruct
{
   char* m_Str;
   UINT m_Int;
   UINT* m_IntArray;
} MyUnmanagedStruct;

typedef struct _UNSAFELCEARRAY
{
    char* strBuf;
    UINT intBuf; 
    UINT* intArrayBuf;
} UNSAFELCEARRAY;

extern "C" __declspec(dllexport) void GetUnmanagedStructs( int* pSize, UNSAFELCEARRAY** ppStruct )
{
    const int cArraySize = 5;
    *pSize = cArraySize;
    int numBytes = cArraySize * sizeof( MyUnmanagedStruct);
    *ppStruct = (UNSAFELCEARRAY*)CoTaskMemAlloc(numBytes);

    UNSAFELCEARRAY* pCurStruct = *ppStruct;
    char* typenamebuffer;
    char* numBuffer;
    int var = 999;
    for( int i = 0; i < cArraySize; i++, pCurStruct++ )
    {
       pCurStruct->intBuf = i+1;

       typenamebuffer = (char*)CoTaskMemAlloc( 8 );
       memcpy_s(typenamebuffer,    8, "bufABCD", 8);
       pCurStruct->strBuf = typenamebuffer;

       numBuffer = (char*)CoTaskMemAlloc( 16 );
       ++var;
       memcpy_s(numBuffer,    4, &var, 4);
       ++var;
       memcpy_s(numBuffer+4,  4, &var, 4);
       ++var;
       memcpy_s(numBuffer+8,  4, &var, 4);
       ++var;
       memcpy_s(numBuffer+12, 4, &var, 4);
       pCurStruct->intArrayBuf = (UINT*)numBuffer;
    }
 }

如果我从托管和非托管对象中删除int的向量,但是使用上面的代码返回时未初始化的内容是一切正常。我使用下面的函数从MyUnmanagedStructs生成MyManagedClasses

        int size;
        IntPtr outArray;
        LibWrap.GetUnmanagedStructs(out size, out outArray);

        manArray = new MyManagedClass[size];
        IntPtr current = outArray;
        for (int i = 0; i < size; i++)
        {
            manArray[i] = new MyManagedClass();
            Marshal.PtrToStructure(current, manArray[i]);

            Marshal.DestroyStructure(current, typeof(sb_LCE));
            int numBytes = Marshal.SizeOf(manArray[i]);
            current = (IntPtr)((long)current + numBytes);
        }
        Marshal.FreeCoTaskMem(outArray);

原谅冗长的解释以及未管理的支柱充满虚拟值的事实。这只是为了说明。

0 个答案:

没有答案