1、C++结构体定义:
#pragma pack(1)
struct Person
{
#define Count_favoriteNumbers 6
int id;
float favoriteNumbers[Count_favoriteNumbers];
};
#pragma pack() // #pragma pack(1) end
C++ 导出函数:
#define DllExport extern "C" __declspec(dllexport)
DllExport void __stdcall InitPersonInfo_DLL(Person* p_Person)
{
p_Person->id = 6;
for (int i = 1; i <= Count_favoriteNumbers; i++)
{
p_Person->favoriteNumbers[i - 1] = 1.11 * i;
}
}
2、C# 结构体定义:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Person
{
public const int Count_favoriteNumbers = 6;
public int id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Count_favoriteNumbers, ArraySubType = UnmanagedType.U4)]
public float[] favoriteNumbers = new float[Count_favoriteNumbers];
}
C# 调用:
public partial class MainWindow : Window
{
[DllImport("MFCLibrary_ExportFunction.dll", EntryPoint = "InitPersonInfo_DLL")]
static extern unsafe void InitPersonInfo_DLL(byte* p_Person);
public unsafe void InitPersonInfo(ref Person person)
{
byte[] structBytes = StructToBytes(person);
fixed (byte* p_Person = &structBytes[0])
{
InitPersonInfo_DLL(p_Person);
person = (Person)BytesToStruct(structBytes, person.GetType());
}
public MainWindow()
{
InitializeComponent();
Person zhangSan = new Person();
InitPersonInfo(ref zhangSan);
}
#region // 结构体与 byte[] 互转
// Struct 转换为 byte[]
public static byte[] StructToBytes(object structure)
{
int size = Marshal.SizeOf(structure);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structure, buffer, false);
byte[] bytes = new byte[size];
Marshal.Copy(buffer, bytes, 0, size);
return bytes;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
// byte[] 转换为 Struct
public static object BytesToStruct(byte[] bytes, Type strcutType)
{
int size = Marshal.SizeOf(strcutType);
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(bytes, 0, buffer, size);
return Marshal.PtrToStructure(buffer, strcutType);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
#endregion
}
原文地址:https://www.cnblogs.com/dhqy/p/10192062.html