DocumentProperties

C# Signature:

[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

[DllImport("winspool.drv", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int DocumentProperties(IntPtr hWnd, IntPtr hPrinter, string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, fModes fMode);

VB Signature:

<DllImport("winspool.Drv", EntryPoint:="DocumentPropertiesW", SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Private Shared Function DocumentProperties(ByVal hwnd As IntPtr, ByVal hPrinter As IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByVal pDeviceName As String, ByVal pDevModeOutput As IntPtr, ByVal pDevModeInput As IntPtr, ByVal fMode As Integer) As Integer
    End Function

Sample Code:

public IntPtr GetDevMode(IntPtr printerHandle, string printerName)
{
   int sizeNeeded = DocumentProperties(IntPtr.Zero, printerHandle, printerName, IntPtr.Zero, IntPtr.Zero, fModes.DM_SIZEOF);
   if (sizeNeeded < 0)
   {
     throw new Win32Exception(Marshal.GetLastWin32Error());
   }

   // not really required, but see example: http://support.microsoft.com/kb/828638/en-us
   sizeNeeded += 100;

   IntPtr pdevmode = Marshal.AllocHGlobal(sizeNeeded);

   int result = DocumentProperties(IntPtr.Zero, printerHandle, printerName, pdevmode, IntPtr.Zero, fModes.DM_OUT_BUFFER);
   if (result < 0)
   {
     throw new Win32Exception(Marshal.GetLastWin32Error());
   }

   return pdevmode;
}

Sample Code:

Sample Code:

Sample Code:

Sample Code:

Sample Code:

Sample Code:

Última actualización