C# 呼叫dll 封送結構體 結構體陣列
阿新 • • 發佈:2019-01-06
一. 結構體的傳遞
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#程式碼
[DllImport("jnalib.dll", EntryPoint = "GetVersionPtr")]
public static extern bool GetVersionPtr(ref OSINFO info);
public static extern bool GetVersionRef(ref OSINFO info);
2. 方式二(傳入IntPtr(平臺通用指標))
cpp程式碼
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[] ptArr = new IntPtr[1];
ptArr[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)) * 2); //分配包含兩個元素的陣列
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)));
Marshal.Copy(ptArr, 0, pt, 1); //拷貝指標陣列
GetVersionArray(pt, 2); //呼叫
//還原成結構體陣列
for (int i = 0; i < 2; i++)
{
infos[i]=(OSINFO)Marshal.PtrToStructure((IntPtr)(pt.ToInt32()+i*Marshal.SizeOf(typeof(OSINFO))),typeof(OSINFO));
Console.WriteLine("OsVersion:{0} szVersion:{1}", infos[i].osVersion, infos[i].szVersion);
}
三. 複雜結構體的傳遞
1.
輸出引數,結構體作為指標傳出
cpp程式碼
typedef struct
{
char name[20];
int age;
double 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個元素的空間,比Marshal.copy方便多了
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)
原文連結:http://tcspecial.iteye.com/blog/1675309