12

我正在玩 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));
}

调试显示 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 驱动程序测试打印。代码项目文章在这里

4

2 回答 2

16

由于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));
}
于 2012-10-09T08:07:35.430 回答
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 个额外的字节,以使其工作。

于 2018-10-29T20:25:38.410 回答