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);
}
}
}