当前位置:编程学习 > C#/ASP.NET >>

C#调用 C的 dll 结构体问题!

最近,几天做了个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;

--------------------编程问答-------------------- 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;
    }
--------------------编程问答-------------------- C#调用定义
[DllImport(@"C:\WINDOWS\system32\WfFileIOCtl.dll", CharSet = CharSet.Unicode)]
internal static extern int wf_WriteFiles([In, Out] WF_ST_FILEINFO[] pIcfFileList,
                                               int uWfFileListCount); --------------------编程问答-------------------- 注意事项:
1.文字编码格式
2.[In, Out]的正确使用。
3.c中的[char]占一位,而C#中的[char]占二位。 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,