C Sharp/The .NET Framework/Marshalling

.NET Framework當前支持調用非受管的函數,使用非受管的數據,該過程叫做marshalling。這常用於調用Windows API函數或資料結構,也可用於定製的庫。

關鍵字ref用於傳送地址:

[DllImport("DllFilePath")] extern static int FunctionName(ref byte param1[1], ref byte param2) 

DLL需傳出char *類型時,應該用C#的StringBuilder類型。

例1: GetSystemTimes 編輯

一個簡單例子是C#調用Windows API函數GetSystemTimes。聲明為:

BOOL WINAPI GetSystemTimes(
  __out_opt  LPFILETIME lpIdleTime,
  __out_opt  LPFILETIME lpKernelTime,
  __out_opt  LPFILETIME lpUserTime
);

LPFILETIME是指向FILETIME結構的指針,額可以簡化為64比特的整數。C#通過long類型支持64比特整數。因此可以聲明這個函數為:

using System;
using System.Runtime.InteropServices;

public class Program
{
    [DllImport("kernel32.dll")]
    static extern bool GetSystemTimes(out long idleTime, out long kernelTime, out long userTime);
    
    public static void Main()
    {
        long idleTime, kernelTime, userTime;
        
        GetSystemTimes(out idleTime, out kernelTime, out userTime);
        Console.WriteLine("Your CPU(s) have been idle for: " + (new TimeSpan(idleTime)).ToString());
        Console.ReadKey();
    }
}

注意在參數中使用關鍵字out與ref,自動使它成為指向未受管數據的指針。

例2:GetProcessIoCounters 編輯

傳遞指向結構的指針,可以使用關鍵字out或ref:

using System;
using System.Runtime.InteropServices;

public class Program
{
    struct IO_COUNTERS
    {
        public ulong ReadOperationCount;
        public ulong WriteOperationCount;
        public ulong OtherOperationCount;
        public ulong ReadTransferCount;
        public ulong WriteTransferCount;
        public ulong OtherTransferCount;
    }

    [DllImport("kernel32.dll")]
    static extern bool GetProcessIoCounters(IntPtr ProcessHandle, out IO_COUNTERS IoCounters);

    public static void Main()
    {
        IO_COUNTERS counters;

        GetProcessIoCounters(System.Diagnostics.Process.GetCurrentProcess().Handle, out counters);
        Console.WriteLine("This process has read " + counters.ReadTransferCount.ToString("N0") + 
            " bytes of data.");
        Console.ReadKey();
    }
}

參數類型的映射關係 編輯

性質 C#參數 C語言參數 注釋
輸入參數 string char*
輸出參數 StringBuilder char* 先用StringBuilder buf = new StringBuilder(_size);準備空間
輸出參數 ref int int* 以int指針為例
傳遞結構體指針 ref stuctName stuctName* 實參調用時前綴ref
IntPtr 指針類型
byte unsigned char
System.Int16 short
System.UInt16 unsigned short
System.Char char
System.String char*
System.String wchart_t*
[MarshalAs(UnmanagedType.LPTStr)] string LPTSTR
out 變量名 結構體 **變量名 C#中提前申明一個結構體實例化後的變量名
ref 結構體 變量名 結構體 &變量名
System.SByte char
delegate static extern int FunCallBack(string str); 回調函數
public delegate double fun_type1(double); typedef double (*fun_type1)(double);

結構體的映射關係 編輯

例如: C的結構體:

typedef struct
{
    unsigned char DeviceIdentify[30];//Greenpow Usb IC card reader
    unsigned short res; 
}FindDeviceAck_Struct;

對應於:

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, 
                  CharSet = System.Runtime.InteropServices.CharSet.Ansi,Pack =1)]
    public struct FindDeviceAck_Struct
    {
        /// unsigned char[30]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray,
            SizeConst = 30)]
        public byte[] DeviceIdentify;
        /// unsigned short
        public ushort res;
    }
//另一个例子:
[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
}

C#的結構體與C語言結構體的互轉 編輯

例如在TCP通信時,需要把C#的結構體變為Byte數組傳給TCP模塊。

//定义结构体:
using System.Runtime.InteropServices;//命名空间

[StructLayoutAttribute(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]
struct TestStruct
{
    public int c;//字符串,SizeConst为字符串的最大长度
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string str;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public int[] test;//int数组,SizeConst表示数组的个数,在转换成byte数组前必须先初始化数组,再使用,初始化/的数组长度必须和SizeConst一致,例test = new int[6];
}
//结构体转byte数组:  
public static byte[] StructToBytes(object structObj)
{
    int size = Marshal.SizeOf(structObj);//得到结构体的大小
    byte[] bytes = new byte[size];//创建byte数组
    IntPtr structPtr = Marshal.AllocHGlobal(size);//分配结构体大小的内存空间
    Marshal.StructureToPtr(structObj, structPtr, false);//将结构体拷到分配好的内存空间
    Marshal.Copy(structPtr, bytes, 0, size);//从内存空间拷到byte数组
    Marshal.FreeHGlobal(structPtr);//释放内存空间
    return bytes;//返回byte数组
}
//byte数组转结构体: 
public static object BytesToStuct(byte[] bytes,Type type)
{
    int size = Marshal.SizeOf(type);//得到结构体的大小
    if ( size > bytes.Length )//byte数组长度小于结构体的大小
    {
        return null;//返回空
    }
    IntPtr structPtr = Marshal.AllocHGlobal(size);//分配结构体大小的内存空间
    Marshal.Copy(bytes,0,structPtr,size);//将byte数组拷到分配好的内存空间
    object obj = Marshal.PtrToStructure(structPtr, type);//将内存空间转换为目标结构体
    Marshal.FreeHGlobal(structPtr);//释放内存空间
    return obj;//返回结构体
}