Passing char * [] from C ++ dll Struct to C #

I am trying to pass the following structure through a C ++ DLL to C #:

struct name { char* myArray[3]; char firstname[100]; char lastname[100]; }; void Caller(struct name * demo) { strcpy(demo->firstname,"hello"); demo->myArray[0]="hello"; demo->myArray[1]="hello"; demo->myArray[2]="hello"; ping(demo); //call to c# function } 

Below is my C # code:

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct name { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string firstname; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string lastname; //what should i marshal here for char* myArray[3]; } ; static void Main(string[] args) { name myname = new name(); ping( ref myname); } public static void ping(int a,ref name myname) { Console.WriteLine(myname.firstname+"\n"); } 

I can import the first and last name from a C ++ dll.

What should I do to import a char form pointer form of a C ++ array?

+4
source share
1 answer
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct Name { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string FirstName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)] public string LastName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public string[] Array; }; 

for my complete solution check out Foo.cpp and Program.cs: https://gist.github.com/3779066

0
source

Source: https://habr.com/ru/post/1435871/


All Articles