using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { /// Functions and structures from prntvpt.h. public static partial class PrntvPt { /// /// /// [This function is not supported and might be disabled or deleted in future versions of Windows. PTOpenProviderEx provides /// equivalent functionality and should be used instead.] /// /// Opens an instance of a print ticket provider. /// /// The full name of a print queue. /// The latest version of the Print Schema that the caller supports. /// The version of the Print Schema requested by the caller. /// A pointer to a handle to the print ticket provider. /// The version of the Print Schema that the print ticket provider will use. /// /// If the method succeeds, it returns S_OK; otherwise, it returns an HRESULT error code. For more information about /// COM error codes, see Error Handling. /// /// Before calling this function, the calling thread must initialize COM by calling CoInitializeEx. // https://docs.microsoft.com/en-us/windows/win32/printdocs/bindptproviderthunk HRESULT BindPTProviderThunk( _In_ LPTSTR // pszPrinterName, _In_ INT maxVersion, _In_ INT prefVersion, _Out_ HPTPROVIDER *phProvider, _Out_ INT *usedVersion ); [DllImport(Lib.PrntvPt, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("", MSDNShortId = "815cc360-8dcd-4c58-a64d-5d77436a8623")] public static extern HRESULT BindPTProviderThunk(string pszPrinterName, int maxVersion, int prefVersion, out SafeHPTPROVIDER phProvider, out int usedVersion); /// /// [This function is not supported and might be disabled or deleted in future versions of Windows. PTGetPrintCapabilities provides equivalent functionality and should be used instead.] /// Retrieves the printer's capabilities formatted in compliance with the XML Print Schema. /// /// A handle to an open print ticket provider. This handle is returned by the BindPTProviderThunk function. /// The buffer that contains the print ticket data, expressed in XML as described in the Print Schema. /// The size, in bytes, of the buffer referenced by pPrintTicket. /// The address of the buffer that is allocated by this function and contains the valid print capabilities information, encoded as XML. This function calls CoTaskMemAlloc to allocate this buffer. When the buffer is no longer needed, the caller must free it by calling CoTaskMemFree. /// The size, in bytes, of the buffer referenced by ppbPrintCapabilities. /// A pointer to a string that specifies what, if anything, is invalid about pPrintTicket. If it is valid, this value is NULL. If pbstrErrorMessage is not NULL when the function returns, the caller must free the string with SysFreeString. /// If the method succeeds, it returns S_OK; otherwise, it returns an HRESULT error code. For more information about COM error codes, see Error Handling. // https://docs.microsoft.com/en-us/windows/win32/printdocs/getprintcapabilitiesthunk2 // HRESULT GetPrintCapabilitiesThunk2( _In_ HPTPROVIDER hProvider, _In_ BYTE *pPrintTicket, _In_ INT cbPrintTicket, _Out_ BYTE **ppbPrintCapabilities, _Out_ INT *pcbPrintCapabilitiesLength, _Out_opt_ BSTR *pbstrErrorMessage ); [DllImport(Lib.PrntvPt, SetLastError = false, ExactSpelling = true)] [PInvokeData("winspool.h", MSDNShortId = "15219c19-b64c-4c51-9357-15a797557693")] public static extern HRESULT GetPrintCapabilitiesThunk2(HPTPROVIDER hProvider, [In] IntPtr pPrintTicket, int cbPrintTicket, out IntPtr ppbPrintCapabilities, out int pcbPrintCapabilitiesLength, [MarshalAs(UnmanagedType.BStr)] out string pbstrErrorMessage); /// Closes a print ticket provider handle. /// A handle to the provider. This handle is returned by the PTOpenProvider or PTOpenProviderEx function. /// /// If the operation succeeds, the return value is S_OK, otherwise the HRESULT contains an error code. /// If hProvider was opened in a different thread, the HRESULT is E_INVALIDARG. /// For more information about COM error codes, see Error Handling. /// /// /// /// Note This is a blocking or synchronous function and might not return immediately. How quickly this function returns /// depends on run-time factors such as network status, print server configuration, and printer driver implementation—factors that /// are difficult to predict when writing an application. Calling this function from a thread that manages interaction with the user /// interface could make the application appear to be unresponsive. /// /// /// The hProvider parameter must be a handle that was opened in the same thread as the thread in which it is used for this function. /// /// A handle cannot be used after it is closed. /// // https://docs.microsoft.com/en-us/windows/win32/api/prntvpt/nf-prntvpt-ptcloseprovider HRESULT PTCloseProvider( HPTPROVIDER // hProvider ); [DllImport(Lib.PrntvPt, SetLastError = false, ExactSpelling = true)] [PInvokeData("prntvpt.h", MSDNShortId = "28e85b53-fd0c-4210-ae2b-794efaf65bd4")] public static extern HRESULT PTCloseProvider(HPTPROVIDER hProvider); /// /// /// [This function is not supported and might be disabled or deleted in future versions of Windows. PTCloseProvider provides /// equivalent functionality and should be used instead.] /// /// Closes a handle to a print ticket provider. /// /// /// A handle to an open print ticket provider. This handle is returned by the BindPTProviderThunk function. /// /// /// If the method succeeds, it returns S_OK; otherwise, it returns an HRESULT error code. For more information about /// COM error codes, see Error Handling. /// // https://docs.microsoft.com/en-us/windows/win32/printdocs/unbindptproviderthunk HRESULT UnbindPTProviderThunk( _In_ HPTPROVIDER // hProvider ); [DllImport(Lib.PrntvPt, SetLastError = false, ExactSpelling = true)] [PInvokeData("", MSDNShortId = "ce979c89-9f9d-4e89-b142-beed414caa3f")] public static extern HRESULT UnbindPTProviderThunk(HPTPROVIDER hProvider); /// Provides a handle to a print provider. [StructLayout(LayoutKind.Sequential)] public struct HPTPROVIDER : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public HPTPROVIDER(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static HPTPROVIDER NULL => new HPTPROVIDER(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(HPTPROVIDER h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator HPTPROVIDER(IntPtr h) => new HPTPROVIDER(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(HPTPROVIDER h1, HPTPROVIDER h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(HPTPROVIDER h1, HPTPROVIDER h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is HPTPROVIDER h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a for that is disposed using . public class SafeHPTPROVIDER : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeHPTPROVIDER(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeHPTPROVIDER() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator HPTPROVIDER(SafeHPTPROVIDER h) => h.handle; /// protected override bool InternalReleaseHandle() => PTCloseProvider(handle).Succeeded; } /* ConvertDevModeToPrintTicketThunk2 ConvertPrintTicketToDevModeThunk2 MergeAndValidatePrintTicketThunk2 PTConvertDevModeToPrintTicket PTConvertPrintTicketToDevMode PTGetPrintCapabilities PTGetPrintDeviceCapabilities PTGetPrintDeviceResources PTMergeAndValidatePrintTicket PTOpenProvider PTOpenProviderEx PTQuerySchemaVersionSupport PTReleaseMemory */ } }