最近,几天做了个C# 调用结构体的工作,研究了好几天,终于把调试出来了。现把经验与大家分享。希望对你有帮助!C的 函数原型int WINAPI wf_WriteFiles (WF_ST_FILEINFO * pWfFileList,
        unsigned int uWfFileListCount);
参数说明
   pWfFileList 是一个结构体指针
   uWfFileListCount 结构体的个数结构体定义 WF_ST_FILEINFO
typedef struct {
WCHAR szInPath[MAX_PATH+10];
WCHAR szOutPath[MAX_PATH+10];
unsigned char byFileHashCode[32];
WF_RESULT nResult;
} WF_ST_FILEINFO, *WF_ST_FILEINFO_PTR;

解决方案 »

  1.   

    C#结构体定义如下
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct WF_ST_FILEINFO
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 270)]
            public String szInPath;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 270)]
            public String szOutPath;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public byte[] byFileHashCode;
            public int nResult;
        }
      

  2.   

    C#调用定义
    [DllImport(@"C:\WINDOWS\system32\WfFileIOCtl.dll", CharSet = CharSet.Unicode)]
    internal static extern int wf_WriteFiles([In, Out] WF_ST_FILEINFO[] pIcfFileList,
                                                   int uWfFileListCount);
      

  3.   

    注意事项:
    1.文字编码格式
    2.[In, Out]的正确使用。
    3.c中的[char]占一位,而C#中的[char]占二位。