gpt4 book ai didi

c# - 将 char*[] 从 c++ dll 结构传递到 c#

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:23:58 25 4
gpt4 key购买 nike

我正在尝试通过 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
}

下面是我的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");
}

我能够从 C++ dll 导入名字和姓氏。

从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;
};

对于我的完整解决方案,请检查 Foo.cpp 和 Program.cs:https://gist.github.com/3779066

关于c# - 将 char*[] 从 c++ dll 结构传递到 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12561534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com