Unmanaged C++ DLL Calling
-
I have an unmanaged C++ DLL which has the following structures, typedef struct {char str[200]; } MYARRAY1; typedef struct {MYARRAY1 myarray1struct;} MYARRAY2; typedef struct {MYARRAY2 myarray1struct[5];} MYARRAY3; To return MYARRAY2, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray2( MYARRAY2 *pStruct) To return MYARRAY3, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray3( MYARRAY3 *pStruct) In C#, I declared the following structures, [StructLayout(LayoutKind.Sequential)] public struct MyArray1 { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)] public String str; } [StructLayout(LayoutKind.Sequential)] public struct MyArray2 { public MyArray1 tempArray1; } [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; } I do not have any problem using DLLImport call to C++ DLL to get data in MyArray2, however when I try to get data in MyArray3 with the following code, all data in testArray3 are empty. MyArray3 testArray3 = new MyArray3(); testArray3.tempArray2 = new MyArray2[ 5 ]; CallTestArray3(ref testArray3); Any help is appreciated!!!
-
I have an unmanaged C++ DLL which has the following structures, typedef struct {char str[200]; } MYARRAY1; typedef struct {MYARRAY1 myarray1struct;} MYARRAY2; typedef struct {MYARRAY2 myarray1struct[5];} MYARRAY3; To return MYARRAY2, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray2( MYARRAY2 *pStruct) To return MYARRAY3, I used the following entry function, extern "C" __declspec(dllexport) void CallTestArray3( MYARRAY3 *pStruct) In C#, I declared the following structures, [StructLayout(LayoutKind.Sequential)] public struct MyArray1 { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=200)] public String str; } [StructLayout(LayoutKind.Sequential)] public struct MyArray2 { public MyArray1 tempArray1; } [StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; } I do not have any problem using DLLImport call to C++ DLL to get data in MyArray2, however when I try to get data in MyArray3 with the following code, all data in testArray3 are empty. MyArray3 testArray3 = new MyArray3(); testArray3.tempArray2 = new MyArray2[ 5 ]; CallTestArray3(ref testArray3); Any help is appreciated!!!
-
vitowong wrote:
[StructLayout(LayoutKind.Sequential)] public struct MyArray3 { public MyArray2[] tempArray2; }
Underlined should be also marshaled.
MyArray2 is based on MyArray1, and MyArray1 has been marshaled. Is it need for MyArray2[] be marshaled? I tried to marshal as AsAny, but it doesn't work. What do I be marshal as? Thanks.