1

我试图让getJob()从 64 位的 printerspooler api 工作。

我使用以下定义(正如其他人在 SO 上使用的那样)

[DllImport("winspool.drv", EntryPoint = "GetJob", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern bool GetJob([In] IntPtr hPrinter, [In] Int32 dwJobId, [In] Int32 Level, [Out] IntPtr lpJob, [In] Int32 cbBuf, ref IntPtr lpbSizeNeeded);

但这仅在我不调试并且我不在 64 位时才有效。

在 64 位中,我得到了 windows 错误:参数不正确。

我该如何解决这个问题?

我尝试将 Int32 更改为 IntPtr(64 位中 4=>8 个字节)和反向 IntPtr => Int32。无济于事。。

文档在这里:https ://docs.microsoft.com/en-us/windows/desktop/printdocs/getjob :

BOOL GetJob(
  _In_  HANDLE  hPrinter,
  _In_  DWORD   JobId,
  _In_  DWORD   Level,
  _Out_ LPBYTE  pJob,
  _In_  DWORD   cbBuf,
  _Out_ LPDWORD pcbNeeded
);

我还尝试将 ref 更改为 out 并将 ref/out 放入 lpJob 参数,但即使这样似乎也不起作用。

接下来我可以尝试什么?

编辑

似乎有效的是:

[DllImport("winspool.drv", EntryPoint = "GetJob", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern bool GetJob([In] IntPtr hPrinter, [In] Int32 dwJobId, [In] Int32 Level, [Out] byte[] lpJob, [In] Int32 cbBuf, ref Int32 lpbSizeNeeded);

并按如下方式使用它:

public JOB_INFO_1(IntPtr hPrinter, Int32 dwJobId)
{
  var BytesWritten = new Int32();
  var ptBuf = new byte[0];


  if (PrinterMonitorComponent.ComponentTraceSwitch.TraceVerbose)
    Console.WriteLine("JOB_INFO_1 new(" + hPrinter.ToString() + "," + dwJobId.ToString() + ")");


  // \\ Get the required buffer size
  if (!UnsafeNativeMethods.GetJob(hPrinter, dwJobId, 1, ptBuf, 0, ref BytesWritten))
  {
    if (BytesWritten == 0)
    {
      var ex = new Win32Exception();
      if (PrinterMonitorComponent.ComponentTraceSwitch.TraceError)
        Console.WriteLine("{0} GetJob for JOB_INFO_1 failed on handle: {1} for job: {2} - {3}", this.GetType().ToString(), hPrinter, dwJobId, ex.Message);
      throw ex;
    }
  }

  // \\ Allocate a buffer the right size
  if (BytesWritten > 0)
    ptBuf = new byte[BytesWritten]; // Marshal.AllocHGlobal(BytesWritten);

  //Console.WriteLine($"Buffer {BytesWritten} x"); // uncommenting this code somehow breaks it again -.-
  // \\ Populate the JOB_INFO_1 structure
  if (!UnsafeNativeMethods.GetJob(hPrinter, dwJobId, 1, ptBuf, BytesWritten, ref BytesWritten))
  {
    if (PrinterMonitorComponent.ComponentTraceSwitch.TraceError)
      Console.WriteLine("GetJob for JOB_INFO_1 failed on handle: " + hPrinter.ToString() + " for job: " + dwJobId, this.GetType().ToString());
    throw new Win32Exception();
  }
  else
  {
    GCHandle handle = GCHandle.Alloc(ptBuf, GCHandleType.Pinned);
    Marshal.PtrToStructure(handle.AddrOfPinnedObject(), this);
    handle.Free();
    //Marshal.PtrToStructure(ptBuf, this);

  }

  // \\ Free the allocated memory
  //Marshal.FreeHGlobal(ptBuf);
}

编辑2

似乎不起作用,有时它似乎起作用,但单元测试没有注意到 csproj 文件中的变化,所以我最终仍在测试 32 位。

将以下行添加到 csproj 使其工作(它以 32 位运行,将其置于 64 位,它失败)

<PlatformTarget>x86</PlatformTarget>
4

1 回答 1

2

这似乎主要是一个时间问题......

我正在使用空打印机进行测试,不知何故,它在 32 位上大部分工作,但比去 64 位一定要慢一些,这就是它会失败的原因,调试也是如此。

该作业已经被假脱机,因此请求作业信息不再可用。

要解决此问题,您可以设置:保留打印的文档

在此处输入图像描述

于 2018-11-02T16:13:32.420 回答