c# c++混编参数传递
C++ Code
typedef struct _result_array
{
int num;
int nums;
char *path;
string name[100];
}*result_array;
int _stdcall test01(char *guest_img_path, result_array p)
{
string path1 = guest_img_path;
p->num = 1;
p->nums = 2;
p->path = guest_img_path;
return 0;
}
C# code
namespace CSharpCallDLL
{
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode, Pack = 1)]
public struct result_array
{
public int num;
public int nums;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 50)]
public string path;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 50)]
public string[] name;
}
class Program
{
[DllImport(@"C:\Users\meng\Desktop\csharp\fasterrcnn\x64\Release\CreateDLL.dll", EntryPoint = "test01", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern int test01(string guest_img_path, ref result_array p);
public static void Main(string[] args)
{
string guest_imgpath = "C:\\Users\\file";
result_array p = new result_array();
p.num = 0;
p.nums = 0;
p.path = " ";
string guest_imgpath = "C:\\Users\\meng\\Desktop\\file";
int r = test01(guest_imgpath,ref p);
Console.ReadKey();
}
}
}
结构体成员可以从c#中传递到c++DLL,在DLL中修改结构体成员的值后,在C#中结构体p的成员整形p.num和p.nums的值改变,而字符串p.path则没变或乱码,求大神赐教!