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;//返回结构体
}