c#调用包含结构体嵌套的C++ dll函数

在痛苦了两天后,终于搞定了。主要是中文乱码问题,最后受C#调用C/C++DLL收取中文字符处理这篇文章的启发,才弄好。核心思想:c++中的char是一个字节,wchar 是两个字节,c#中的char是两个字节。
====================== C++代码==========================
里面出现的TCHAR为:

typedef wchar_t WCHAR;
typedef WCHAR TCHAR

用到的四个struct,其中struct2里存的有中文,需要返回到dll外部

struct struct1
{
	float h1;
	float h2;	
	float t;
	float t1;	
	float w1;			
	int  a;	
}
struct struct2
{
	TCHAR name[100];	
	TCHAR sex[100];	
}
struct StructRlt
{
	struct1 m_struct1;
	struct2 m_struct2;
}

struct structPrame
{
	int a;
	float b;	
}

c++函数声明:

extern "C" __declspec(dllexport) StructRlt* TestFunc(structPrame &m_Param, char *m_Data, int nCLen);

====================== C#代码==========================
结构体声明,需要注意的是在struct2里应该是200,在C++里定义的是100,wchar是两个字节,byte是一个字节

[StructLayout(LayoutKind.Sequential)]
public struct struct1
{
	public float h1;
	public float h2;	
	public float t;
	public float t1;	
	public float w1;			
	public int  a;	
}

[StructLayout(LayoutKind.Sequential)]
public struct struct2
{	
	[MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
	public byte[] name;	
	
	[MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
	public byte[] sex;	
}

[StructLayout(LayoutKind.Sequential)]
public struct StructRlt
{
	[MarshalAs(UnmanagedType.Struct)]
	public struct1 m_struct1;
	
	[MarshalAs(UnmanagedType.Struct)]
	public struct2 m_struct2;
}

[StructLayout(LayoutKind.Sequential)]
public struct structPrame
{
	public int a;
	public float b;	
}

函数声明:

[DllImport("dllName.dll", EntryPoint = "TestFunc", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr TestFunc(ref structPrame m_Param, [MarshalAs(UnmanagedType.LPStr)]string data, int len);

使用

structPrame parame = new structPrame();
string data = "hjakljdlsjalfds";
IntPtr res_ptr = TestFunc(ref parame, data, data.Length);
StructRlt res = (StructRlt)Marshal.PtrToStructure(res_ptr, typeof(StructRlt));

byte[] temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, res.m_struct2.name);
string name = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

temp = Encoding.Convert(Encoding.Unicode, Encoding.Unicode, res.m_struct2.sex);
string sex = Marshal.PtrToStringAuto(Marshal.UnsafeAddrOfPinnedArrayElement(temp, 0));

折腾了两天,哎,长时间不用C++都快忘完了。

你可能感兴趣的:(c#编程)