c调⽤c语⾔结构体数组,C#调⽤CC++动态库封送结构体,结构
体数组(数组)
C#调⽤C/C++动态库 封送结构体,结构体数组
⼀. 结构体的传递
Cpp代码
#define JNAAPI extern "C" __declspec(dllexport) // C⽅式导出函数
typedef struct
{
int osVersion;
int majorVersion;
int minorVersion;
int buildNum;
int platFormId;
char szVersion[128];
}OSINFO;
// 1. 获取版本信息(传递结构体指针)
JNAAPI bool GetVersionPtr( OSINFO *info );
// 2.获取版本信息(传递结构体引⽤)
JNAAPI bool GetVersionRef(OSINFO &info);
C#代码
// OSINFO定义
[StructLayout(LayoutKind.Sequential)]
public struct OSINFO
{
public int osVersion;
public int majorVersion;
public int minorVersion;
public int buildNum;
public int platFormId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szVersion;
}
可以通过⼆种⽅式来调⽤:
1. ⽅式⼀(传⼊结构体引⽤),在C#中,结构体是以传值⽅式传递,类才是以传地址⽅式传递,加关键字ref即可. C端传递了两种不同类型的参数,都可以通过引⽤来解决.
C#代码
[DllImport("jnalib.dll", EntryPoint = "GetVersionPtr")]
public static extern bool GetVersionPtr(ref OSINFO info);
public static extern bool GetVersionRef(ref OSINFO info);
2. ⽅式⼆(传⼊IntPtr(平台通⽤指针))
C#代码
IntPtr pv = Marshal.AllocHGlobal(148); //结构体在使⽤时⼀定要分配空间(4*sizeof(int)+128)
Marshal.WriteInt32(pv,148); //向内存块⾥写⼊数值
if (GetVersionPtr(pv)) //直接以⾮托管内存块地址为参数
{
Console.WriteLine("--osVersion:{0}", Marshal.ReadInt32(pv, 0));
Console.WriteLine("--Major:{0}",Marshal.ReadInt32(pv, 4)); //移动4个字节
Console.WriteLine("--BuildNum: " + Marshal.ReadInt32(pv, 12));
Console.WriteLine("--szVersion: "+Marshal.PtrToStringAnsi((IntPtr)(pv.ToInt32()+20)));
}
Marshal.FreeHGlobal(pv); //处理完记得释放内存
⼆.结构体数组的传递
Cpp代码
// 传递结构体指针
JNAAPI bool GetVersionArray(OSINFO *info,int nLen);
调⽤代码:
C#代码
/**
* C#接⼝,对于包含数组类型,只能传递IntPtr
*/
[DllImport("jnalib.dll", EntryPoint = "GetVersionArray")]
public static extern bool GetVersionArray(IntPtr p, int nLen);
// 源⽬标参数
OSINFO[] infos = new OSINFO[2];
for (int i = 0; i < infos.Length; i++)
{
infos[i] = new OSINFO();
}
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)) * 2);
GetVersionArray(pt, 2); //调⽤
//还原成结构体数组
for (int i = 0; i < 2; i++)
{
IntPtr ptr = (IntPtr)((UInt32)pt + i * Marshal.SizeOf(typeof(OSINFO)));
infos[i]=(OSINFO)Marshal.PtrToStructure(ptr,typeof(OSINFO));
Console.WriteLine("OsVersion:{0} szVersion:{1}", infos[i].osVersion, infos[i].szVersion); }
Marshal.FreeHGlobal (pt);
三. 复杂结构体的传递
1. 输出参数,结构体作为指针传出
Cpp代码
typedef struct
{
char name[20];
int age;
typeof arraydouble scores[30];
}Student;
// Class中包含结构体数组类型
typedef struct
{
int number;
Student students[50];
}Class;
// 传⼊复杂结构体测试
JNAAPI int GetClass(Class *pClass,int len);
C#代码
// 接⼝定义
[DllImport("jnalib.dll", EntryPoint = "GetClass")]
public static extern int GetClass(IntPtr pv,int len);
// 结构体定义
// Student
[StructLayout(LayoutKind.Sequential)]
public struct Student
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
public string name;
public int age;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public double[] scores;
}
// Class
[StructLayout(LayoutKind.Sequential)]
public struct Class
{
public int number;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)] // 指定数组尺⼨
public Student[] students; // 结构体数组定义
}
// 调⽤复杂结构体测试
int size = Marshal.SizeOf(typeof(Class)) * 50;
IntPtr pBuff = Marshal.AllocHGlobal(size); // 直接分配50个元素的空间,⽐py⽅便多了GetClass(pBuff, 50);
Class[] pClass = new Class[50];
for (int i = 0; i < 50; i++)
{
IntPtr ptr = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
pClass[i] = (Class)Marshal.PtrToStructure(ptr, typeof(Class));
}
Marshal.FreeHGlobal(pBuff); // 释放内存
2. 输⼊参数, 给复杂结构体赋值后作为输⼊参数传⼊
对于⽐较⼤的结构体指针,⽆法直接应⽤结构体类型,转化成IntPtr类型, 此时需要将原⽣类型转化为指针,并给指针赋值调⽤⽅法:
Marshal.StructureToPtr(stu, ptr1, true)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论