日期:2014-05-18 浏览次数:21058 次
<StructLayout(LayoutKind.Sequential)> _ Private Class PrinterNotifyInfoData Public wType As Int16 Public wField As Int16 Public dwReserved As Int32 Public dwId As Int32 Public cbBuff As Int32 Public pBuff As Int32 End Class
typedef struct _PRINTER_NOTIFY_INFO_DATA { WORD Type; WORD Field; DWORD Reserved; DWORD Id; union { DWORD adwData[2]; struct { DWORD cbBuf; LPVOID pBuf; } Data; } NotifyData; } PRINTER_NOTIFY_INFO_DATA, *PPRINTER_NOTIFY_INFO_DATA;
------解决方案--------------------
Codeplex上的一个项目也有过同样的问题,解决方案来自 不存在的网站
代码如下:
[StructLayout(LayoutKind.Sequential)] public struct PRINTER_NOTIFY_INFO_DATA_DATA { public uint cbBuf; public IntPtr pBuf; } [StructLayout(LayoutKind.Explicit)] public struct PRINTER_NOTIFY_INFO_DATA_UNION { [FieldOffset(0)] private uint adwData0; [FieldOffset(4)] private uint adwData1; [FieldOffset(0)] public PRINTER_NOTIFY_INFO_DATA_DATA Data; public uint[] adwData { get { return new uint[] { this.adwData0, this.adwData1 }; } } } [StructLayout(LayoutKind.Sequential)] public struct PRINTER_NOTIFY_INFO_DATA { public ushort Type; public ushort Field; public uint Reserved; public uint Id; public PRINTER_NOTIFY_INFO_DATA_UNION NotifyData; }
------解决方案--------------------
64位下,adwData0和cbBuf应当具有相同值,adwData1的值不能由Data取到,因为pBuf之前有4字节被空置以对齐。
------解决方案--------------------
PRINTER_NOTIFY_INFO的代码也是有的,出处同上
[StructLayout(LayoutKind.Sequential)] public struct PRINTER_NOTIFY_INFO { public uint Version; public uint Flags; public uint Count; [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] public PRINTER_NOTIFY_INFO_DATA[] aData; }