473,486 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need Help with DocumentProperties

Hi,

I'm trying to build my custom Printer Dialog box.
The following code is supposed to Pop up the printer properties form and
edit the PrinterSettings.

My problem is that I'm able to show the Printer Properties form but I'm
unable to write back the settings to my PrinterSettings
It either crash with a null reference exception or my application freeze
completly.

I just can't seem to figure out what i'm doing ?

Any help would be greatly appreciated.

TIA

Ben

#region "Data structure"
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_DEFAULTS
{
public int pDatatype;
public int pDevMode;
public int DesiredAccess;
}
#endregion

public class CustomPrinterSettings
{
#region "Private Variables"
private int lastError;
private int intError;
#endregion

#region "Win API Def"
[DllImport("kernel32.dll", EntryPoint="GetLastError",
SetLastError=false, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern Int32 GetLastError();

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr GlobalLock ( IntPtr hMem);

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr GlobalUnlock ( IntPtr hMem);

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
private static extern int GlobalFree ( IntPtr hMem);

[DllImport("winspool.Drv", EntryPoint="ClosePrinter",
SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA",
SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern int DocumentProperties (IntPtr hwnd, IntPtr
hPrinter, [MarshalAs(UnmanagedType.LPStr)] string pDeviceNameg, IntPtr
pDevModeOutput, ref IntPtr pDevModeInput, int fMode);

[DllImport("winspool.Drv", EntryPoint="OpenPrinterA",
SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
private static extern bool
OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr
hPrinter, ref PRINTER_DEFAULTS pd);
#endregion

#region "Constants"
private const int DM_OUT_BUFFER = 2;
private const int DM_IN_PROMPT = 4;
#endregion

public bool DisplayPrinterSettings(string printerName)
{
PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();
IntPtr Temp = new IntPtr();
IntPtr hPrinter = new System.IntPtr() ;

int nRet = Convert.ToInt32(OpenPrinter(printerName, out hPrinter,
ref PrinterValues));
try
{
if (nRet == 0)
{
lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(Marshal.GetLastWin32Error());
}

IntPtr devIn = PrintEngine.PrinterSettings.GetHdevmode();
IntPtr globalDevIn = GlobalLock(devIn);
try
{
IntPtr devOut =
PrintEngine.PrinterSettings.GetHdevmode();
IntPtr globalDevOut = GlobalLock(devOut);
try
{
int propSetRet = DocumentProperties(IntPtr.Zero,
hPrinter, printerName, globalDevOut, ref globalDevIn, DM_IN_PROMPT |
DM_OUT_BUFFER);

if (propSetRet == 1)
{
PrintEngine.PrinterSettings.SetHdevmode(globalDevO ut);
<<<<<<<<--------- The problem is probably there!!!
return true;
}
else
{
return false;
}
}
finally
{
GlobalUnlock(globalDevOut);
GlobalFree(devOut);
}
}
finally
{
GlobalUnlock(globalDevIn);
GlobalFree(devIn);
}
}
finally
{
ClosePrinter(hPrinter);
}
}
}

Nov 16 '05 #1
0 2560

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2393
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
7
4359
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
0
295
by: sparton | last post by:
Good day!! Is there a way to retrieve values / changes from documentproperties? documentproperties is the screen is shown when you press 'properties' on the 'Print' window. This can be derived from...
0
1024
by: sparton | last post by:
good day!! is there a way to retrieve values from documentproperties in winspool.drv?? sample values are the number of pages, and orientation.. many thanks!!!
2
4759
by: Chris Bingham | last post by:
Hi everyone, Does anyone know how to fix the paper orientation depending on the paper size used when printing with the WebBrowser control please? What I've got is a HTML file, containing 1 big...
6
2110
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
0
3917
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
1185
by: Calum | last post by:
I have created a custom print dialog and its fully working expcept for the printer preferences. The code below displays the printer preferences dialog for the selected printer fine, the devmode...
4
2545
by: wellscrambled | last post by:
Folks, Probably don't have the right forum here, but this is all a bit new to me. I have a web site that sends me a daily email with an excel spreadsheet attachment that contains some customer...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.