Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than winspool, prefix the name with the module name and a period.
<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
User-Defined Types:
None.
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
For the values for the fMode parameter have a look at the fModes enum.
Notes for x64 sytems
Doesn't work on a x64 system. (i.e. this declaration is wrong for x64, I'm just not quite sure why.). MH:I tested the code extensivly and I think the param "IntPtr pDevModeInput" must not have the "ref" flag. If it has the initial state of the dialog is wrong. EP: Yup, you're right! Thanks!!! I've been trying to figure out why it doesn't work on some drivers for a few weeks now! "ref" removed.
- - - - - - - - -
I had similar issues under x64 so i invstigated a bit and found the solution!
"DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, pDevMode, pDevMode, 0);" returns -1 and error code 183 under x64 however i dnt knw y!
in order to get it working use "DocumentProperties(this.Handle, IntPtr.Zero, printerSettings.PrinterName, IntPtr.Zero, pDevMode, 0);"
Tips & Tricks:
Please add some!
Sample Code:
Obtain a pointer to the DEVMODE struct (either printerHandle or printerName can be NULL | IntPtr.ZERO):
public IntPtr GetDevMode(IntPtr printerHandle, string printerName)
{
int sizeNeeded = DocumentProperties(IntPtr.Zero, printerHandle, printerName, IntPtr.Zero, IntPtr.Zero, fModes.DM_SIZEOF);
int sizeNeeded = NativeMethods.DocumentProperties(IntPtr.Zero, printerHandle, printerName, IntPtr.Zero, IntPtr.Zero, NativeMethods.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;
int result = DocumentProperties(IntPtr.Zero, printerHandle, printerName, pdevmode, IntPtr.Zero, fModes.DM_OUT_BUFFER);
int result = NativeMethods.DocumentProperties(IntPtr.Zero, printerHandle, printerName, pdevmode, IntPtr.Zero, NativeMethods.fModes.DM_OUT_BUFFER);
if (result < 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
I had to fixed Matthias solution, because it doesn't work well for me. Exception is no more occurs, but when some setting is changed in printer properties it isn't used by printer. I've changed second call of DocumentProperties method. The third-from-last paramter MUST be in this case devModeData.
I believe the issue(s) discussed re:x64 is actually an odd idiosyncracy under Windows Server 2008 R2 x64, but is not a x86/x64 problem per se. At least testing this under Win7 x64, I have no issues. However, under W2K8 x64, the OutBuffer and InBuffer must NOT be the same (in Win7 x64 this works OK). ALSO, the call to set doc properties with fMode = 14 (Prompt | In | Out) and OutBuffer = IntPtr.Zero CANNOT possibly correct, as in this case there is no place to put the resulting Devmode. Good code shouldn't use "magic numbers" anyway. The following values from WinSpool.h should be used instead of "14" or "0" for the last parameter in this call:
/* mode selections for the device mode function */
public const uint DM_UPDATE = 1;
public const uint DM_COPY = 2;
public const uint DM_PROMPT = 4;
public const uint DM_MODIFY = 8;
public const uint DM_IN_BUFFER = DM_MODIFY;
public const uint DM_IN_PROMPT = DM_PROMPT;
public const uint DM_OUT_BUFFER = DM_COPY;
public const uint DM_OUT_DEFAULT = DM_UPDATE;
EDIT 13.09.2013: For the values for the fMode parameter have a look at the fModes enum.
Edit 10APR2013:
On a Win8 x64 system, I encountered the issue when I was trying to target x86 compiling. The call to DocumentProperties would fail and return -1 for sizeNeeded.
If I target x64 or AnyCPU, everything is working fine. However, because of other constraints, I had to target x86 and couldn't make it work without using the IntPtr.Zero fix. Now it works in every configuration.
Agreed that the use of a IntPrt.Zero where a recipient data structure is expected looks wrong, but it should be ok in this case: that first call to DocumentProperties does not attempt to save any data to the DevMode structure, it just returns the size of memory necessary to hold all the devModeData held by the printer driver, nothing else.
Defines the values for the fMode-Parameter of the [DocumentProperties] method.
8/28/2014 8:25:22 PM - -193.109.238.134
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Contains information about the initialization and environment of a printer or a display device.
2/1/2023 1:31:58 PM - shtolliver-47.227.243.115
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Brings up a printer properties page
11/6/2015 11:53:45 AM - Anisan-179.178.48.53
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Brings up a printer properties page
11/6/2015 11:53:45 AM - Anisan-179.178.48.53
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Defines the values for the fMode-Parameter of the [DocumentProperties] method.
8/28/2014 8:25:22 PM - -193.109.238.134
Brings up a printer properties page
11/6/2015 11:53:45 AM - Anisan-179.178.48.53
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Brings up a printer properties page
11/6/2015 11:53:45 AM - Anisan-179.178.48.53
Contains information about the initialization and environment of a printer or a display device.
2/1/2023 1:31:58 PM - shtolliver-47.227.243.115
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).