我正在使用codeproject中的一个项目,它基本上监视计算机上的打印活动。但是,对于64位配置,它无法正常工作。代码的下面部分是问题。无论何时打印完成,都会调用此代码。
PRINTER_NOTIFY_INFO info = (PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(pNotifyInfo, typeof(PRINTER_NOTIFY_INFO));
int pData = (int)pNotifyInfo + Marshal.SizeOf(typeof(PRINTER_NOTIFY_INFO));
PRINTER_NOTIFY_INFO_DATA[] data = new PRINTER_NOTIFY_INFO_DATA[info.Count];
for (uint i = 0; i < info.Count; i++)
{
data[i] = (PRINTER_NOTIFY_INFO_DATA)Marshal.PtrToStructure((IntPtr)pData, typeof(PRINTER_NOTIFY_INFO_DATA));
pData += Marshal.SizeOf(typeof(PRINTER_NOTIFY_INFO_DATA));
}
调试显示数据[i] .field值始终为0.但在32位中它可以正常工作。我认为没有正确定义PRINTER_NOTIFY_INFO_DATA。目前我正在使用以下代码。任何人都可以解决这个问题,以便在64位上正常工作吗?
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_NOTIFY_INFO
{
public uint Version;
public uint Flags;
public uint Count;
}
[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 };
}
}
}
// Structure borrowed from http://lifeandtimesofadeveloper.blogspot.com/2007/10/unmanaged-structures-padding-and-c-part_18.html.
[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;
}
我正在使用MS XPS驱动程序测试打印。代码项目文章是Here
答案 0 :(得分:15)
由于Data Alignment,它无法正常用于64位配置。
所以我建议您按如下方式更改PRINTER_NOTIFY_INFO:
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_NOTIFY_INFO
{
public uint Version;
public uint Flags;
public uint Count;
public PRINTER_NOTIFY_INFO_DATA_UNION aData;
}
然后使用Marshal.OffsetOf
代替Marshal.SizeOf:
PRINTER_NOTIFY_INFO info = (PRINTER_NOTIFY_INFO)Marshal.PtrToStructure(pNotifyInfo, typeof(PRINTER_NOTIFY_INFO));
long pData = (long)pNotifyInfo + (long)Marshal.OffsetOf(typeof(PRINTER_NOTIFY_INFO), "aData");
PRINTER_NOTIFY_INFO_DATA[] data = new PRINTER_NOTIFY_INFO_DATA[info.Count];
for (uint i = 0; i < info.Count; i++)
{
data[i] = (PRINTER_NOTIFY_INFO_DATA)Marshal.PtrToStructure((IntPtr)pData, typeof(PRINTER_NOTIFY_INFO_DATA));
pData += (long)Marshal.SizeOf(typeof(PRINTER_NOTIFY_INFO_DATA));
}
答案 1 :(得分:0)
另一种解决方案是在写入后添加4个字节:PRINTER_NOTIFY_INFO填充以进行数据对齐(32int仅是64位的一半)+ 4个字节使之成为64位。
还要注意:PRINTER_NOTIFY_INFO_DATA_DATA => IntPtr pBuf;是inptr,因此在Windows 64位中为64位。这使得整个类的数据价值为128位。 (所以也不要忘记在此处添加额外的4个字节。
保留或Id似乎也有4个额外的字节,因此我最终在最后每个info_data添加8个额外的字节,以使其正常工作。