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(globalDevOut);
<<<<<<<<--------- The problem is probably there!!!
return true;
}
else
{
return false;
}
}
finally
{
GlobalUnlock(globalDevOut);
GlobalFree(devOut);
}
}
finally
{
GlobalUnlock(globalDevIn);
GlobalFree(devIn);
}
}
finally
{
ClosePrinter(hPrinter);
}
}
}
Mattias Sj?gren - 20 Oct 2004 09:26 GMT
Benoit,
> [DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA",
>SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true,
>CallingConvention=CallingConvention.StdCall)]
You shouldn't combine CharSet.Auto with explicitly referencing the
ANSI version of the API. The whole point of using CharSet.Auto is that
the runtime picks the best version of the API for you, so you don't
need the explicit entrypoint or the ExactSpelling setting.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Benoit Courchesne - 20 Oct 2004 16:42 GMT
Hi Mattias,
Thanks for the info,
I fixed that. I still have the same problem.
Any other idea ?
Thanks a lot, it's greatly appreciated.
Ben
> Benoit,
>
[quoted text clipped - 8 lines]
>
> Mattias