我试图让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>