public bool IsPdfScribePrinterInstalled() { bool pdfScribeInstalled = false; PRINTER_DEFAULTS scribeDefaults = new PRINTER_DEFAULTS(); scribeDefaults.DesiredAccess = 0x00008; // Use access scribeDefaults.pDatatype = null; scribeDefaults.pDevMode = IntPtr.Zero; IntPtr scribeHandle = IntPtr.Zero; if (NativeMethods.OpenPrinter(PRINTERNAME, ref scribeHandle, scribeDefaults) != 0) { pdfScribeInstalled = true; } else { int errorCode = Marshal.GetLastWin32Error(); if (errorCode == 0x5) { pdfScribeInstalled = true; // Printer is installed, but user } // has no privileges to use it } return(pdfScribeInstalled); }
/// <summary> /// /// </summary> /// <param name="portName"></param> /// <param name="xcvDataOperation"></param> /// <returns></returns> /// <remarks>I can't remember the name/link of the developer who wrote this code originally, /// so I can't provide a link or credit.</remarks> private int DoXcvDataPortOperation(string portName, string portMonitor, string xcvDataOperation) { int win32ErrorCode; PRINTER_DEFAULTS def = new PRINTER_DEFAULTS(); def.pDatatype = null; def.pDevMode = IntPtr.Zero; def.DesiredAccess = 1; //Server Access Administer IntPtr hPrinter = IntPtr.Zero; if (NativeMethods.OpenPrinter(",XcvMonitor " + portMonitor, ref hPrinter, def) != 0) { if (!portName.EndsWith("\0")) { portName += "\0"; // Must be a null terminated string } // Must get the size in bytes. Rememeber .NET strings are formed by 2-byte characters uint size = (uint)(portName.Length * 2); // Alloc memory in HGlobal to set the portName IntPtr portPtr = Marshal.AllocHGlobal((int)size); Marshal.Copy(portName.ToCharArray(), 0, portPtr, portName.Length); uint needed; // Not that needed in fact... uint xcvResult; // Will receive de result here NativeMethods.XcvData(hPrinter, xcvDataOperation, portPtr, size, IntPtr.Zero, 0, out needed, out xcvResult); NativeMethods.ClosePrinter(hPrinter); Marshal.FreeHGlobal(portPtr); win32ErrorCode = (int)xcvResult; } else { win32ErrorCode = Marshal.GetLastWin32Error(); } return(win32ErrorCode); }
private bool DeletePdfScribePrinter() { bool printerDeleted = false; PRINTER_DEFAULTS scribeDefaults = new PRINTER_DEFAULTS(); scribeDefaults.DesiredAccess = 0x000F000C; // All access scribeDefaults.pDatatype = null; scribeDefaults.pDevMode = IntPtr.Zero; IntPtr scribeHandle = IntPtr.Zero; try { if (NativeMethods.OpenPrinter(PRINTERNAME, ref scribeHandle, scribeDefaults) != 0) { if (NativeMethods.DeletePrinter(scribeHandle)) { printerDeleted = true; } } else { logEventSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, "Could not delete PDF Scribe virtual printer. " + String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString())); } } finally { if (scribeHandle != IntPtr.Zero) { NativeMethods.ClosePrinter(scribeHandle); } } return(printerDeleted); }
internal static extern int OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault);