C#调用DLL,实在找不到问题出在哪里了
在网上看了许多关于C#调用C/C++编写的DLL文件的帖子,但是都没有能解决我的问题,有点抓狂了......
1. 目前的程序是在别人的程序的基础上进行的“修修补补”,该程序是使用DLL文件进行的二次开发。
2. 可能出问题的地方:
(2.1)C#调用的代码是:
[DllImport("MyFunction.dll",EntryPoint="GetPortInfo")]public static extern UInt32 GetPortInfo(UInt32 Device, Byte Num, IntPtr pPortInfo);UInt32 GetPortInfo( UInt32 hDevice, unsigned char num, PortInfo& pPortInfo );
[StructLayout(LayoutKind.Sequential,Pack=1)]public struct port{ [MarshalAs(UnmanagedType.ByValArray,SizeConst=6)] public Byte[] mac; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] ip; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] mask; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] gage; [MarshalAs(UnmanagedType.U4)] public Uint32 ArpReply; [MarshalAs(UnmanagedType.U4)] public Uint32 IcmpReply; [MarshalAs(UnmangeddType.U1)] public Byte IgmpEnable; [MarshalAs(UnmangeddType.U1)] public Byte MultiPauseEnable; [MarshalAs(UnmangeddType.U1)] public Byte DirectPauseEnable; [MarshalAs(UnmanagedType.ByValArray,SizeConst=6)] public Byte[] UserDefAddr;}struct PortInfo{ unsigned char mac[6]; unsigned char ip[4]; unsigned char mask[6]; unsigned char gate[6]; ArpReplayEnum ArpReply; IcmpReplyEnum IcmpReply; unsigned char IgmpEnable; unsigned char MultiPauseEnable; unsigned char DirectPauseEnable; unsigned char UserDefAddr[6];}IntPtr pPortInfo = Marshal.AllocHGlobal(40);
[StructLayout(LayoutKind.Sequential)]public struct port{ [MarshalAs(UnmanagedType.ByValArray,SizeConst=6)] public Byte[] mac; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] ip; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] mask; [MarshalAs(UnmanagedType.ByValArray,SizeConst=4)] public Byte[] gage; public UInt16 pad1; [MarshalAs(UnmanagedType.U4)] public Uint32 ArpReply; [MarshalAs(UnmanagedType.U4)] public Uint32 IcmpReply; [MarshalAs(UnmangeddType.U1)] public Byte IgmpEnable; [MarshalAs(UnmangeddType.U1)] public Byte MultiPauseEnable; [MarshalAs(UnmangeddType.U1)] public Byte DirectPauseEnable; [MarshalAs(UnmanagedType.ByValArray,SizeConst=6)] public Byte UserDefAddr; public UInt16 pad2; public Byte pad2;}
IntPtr pPortInfo = Marshal.AllocHGlobal(40);GetPortInfo( UInt32 hDevice, unsigned char num, PortInfo& pPortInfo );Marshal.FreeHGlobal(pPortInfo );