Я пишу программу на C # для изменения ориентации задания на печать.Вот как я это делаю
• Открыть принтер с помощью OpenPrinter ()
• Получить сведения о работе со структурой JOB_INFO_2 с помощью GetJob ()
• Изменить ориентацию в JOB_INFO_2структура
• Установить структуру JOB_INFO_2 с помощью SetJob ()
Но когда я вызываю метод SetJob (), он всегда завершается ошибкой с кодом возврата 122. Я открываю принтер с PRINTER_ALL_ACCESS.(Я также пытался с PRINTER_ACCESS_ADMINISTER, но не повезло).Принтер, который я использую, является локальным принтером.
Что я делаю не так?
Вот мой код
public class PrintJob
{
#region Native Method Imports
[DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, ref PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", EntryPoint = "GetJob", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern bool GetJob(IntPtr hPrinter, Int32 dwJobId, Int32 Level, IntPtr lpJob, Int32 cbBuf, ref Int32 lpbSizeNeeded);
[DllImport("winspool.drv", EntryPoint = "GetJob", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
public static extern bool GetJob(Int32 hPrinter, Int32 dwJobId, Int32 Level, IntPtr lpJob, Int32 cbBuf, ref Int32 lpbSizeNeeded);
[DllImport("winspool.drv", EntryPoint = "SetJobA")]
public static extern bool SetJob(IntPtr hPrinter, int JobId, int Level, IntPtr pJob, int Command_Renamed);
[StructLayout(LayoutKind.Sequential)]
internal struct PRINTER_DEFAULTS
{
public int pDatatype;
public int pDevMode;
public uint DesiredAccess;
}
private const int DM_ORIENTATION = 0x1;
#endregion
public void ChangePrintOrientation(string printerName, int jobId)
{
IntPtr _printerHandle = IntPtr.Zero;
PRINTER_DEFAULTS pDefaults = new PRINTER_DEFAULTS();
pDefaults.DesiredAccess = 0xf000c; // PRINTER_ALL_ACCESS;
pDefaults.pDatatype = 0;
pDefaults.pDevMode = 0;
//Open the printer
OpenPrinter(printerName, out _printerHandle, ref pDefaults);
if (_printerHandle == IntPtr.Zero)
{
throw new Exception("OpenPrinter() Failed with error code " + Marshal.GetLastWin32Error());
}
//Get the JOB details from GetJob()
JOB_INFO_2 jobInfo = GetJobInfo2(_printerHandle, jobId);
if(jobInfo == null)
{
throw new Exception("Could not get job details");
}
//Set the orientation
jobInfo.DeviceMode.dmFields = DM_ORIENTATION;
jobInfo.DeviceMode.dmOrientation = 2;//LANDSCAPE
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(jobInfo));
Marshal.StructureToPtr(jobInfo, ptr, false);
//Set the job
if(!SetJob(_printerHandle, jobId, 2, ptr, 0)) //Here i always get 122 from the function
{
throw new Exception("SetJob() Failed with error code " + Marshal.GetLastWin32Error());
}
//Finally close the printer
ClosePrinter(_printerHandle);
}
private JOB_INFO_2 GetJobInfo2(IntPtr _printerHandle, int jobId)
{
JOB_INFO_2 info = null;
Int32 BytesWritten = default(Int32);
IntPtr ptBuf = default(IntPtr);
//Get the required buffer size
if (!GetJob(_printerHandle, jobId, 2, ptBuf, 0, ref BytesWritten))
{
if (BytesWritten == 0)
{
throw new Exception("GetJob for JOB_INFO_2 failed on handle: " + _printerHandle.ToString() + " for job: " + jobId);
}
}
//Allocate a buffer the right size
if (BytesWritten > 0)
{
ptBuf = Marshal.AllocHGlobal(BytesWritten * 2);
}
if (!GetJob(_printerHandle, jobId, 2, ptBuf, BytesWritten, ref BytesWritten))
{
throw new Exception("GetJob for JOB_INFO_2 failed on handle: " + _printerHandle.ToString() + " for job: " + jobId);
}
else
{
info = new JOB_INFO_2();
Marshal.PtrToStructure(ptBuf, info);
//Fill the devmode structure
IntPtr ptrDevMode = new IntPtr(info.LPDeviceMode);
Marshal.PtrToStructure(ptrDevMode, info.dmOut);
}
//\\ Free the allocated memory
Marshal.FreeHGlobal(ptBuf);
return info;
}
}
Я взял структуру DEVMODE из здесь.