简体   繁体   English

将char * []从c ++ dll Struct传递给c#

[英]passing char*[] from c++ dll Struct to c#

I'm trying to pass the following structure through c++ DLL to c#: 我试图通过c ++ DLL将以下结构传递给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: 下面是我的c#代码:

[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 am able to import first and last name from c++ dll. 我可以从c ++ dll导入名字和姓氏。

What should I do to import char pointer array form c++? 如何从c ++导入char指针数组?

[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 Foo.cpp and Program.cs: https://gist.github.com/3779066 对于我的完整解决方案,请查看Foo.cpp和Program.cs: https ://gist.github.com/3779066

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM