M
mehdi
Hi folks,
Consider the following code:
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int OpenPrinter(string pPrinterName, out
IntPtr phPrinter, IntPtr pDefault);
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int DocumentProperties(IntPtr hwnd,
IntPtr hPrinter, String pDeviceName, IntPtr pDevModeOutput, ref IntPtr
pDevModeInput, int fMode);
const int DM_UPDATE = 1;
const int DM_COPY = 2;
const int DM_PROMPT = 4;
const int DM_MODIFY = 8;
const int DM_IN_BUFFER = DM_MODIFY;
const int DM_IN_PROMPT = DM_PROMPT;
const int DM_OUT_BUFFER = DM_COPY;
const int DM_OUT_DEFAULT = DM_UPDATE;
private void test()
{
PrinterSettings settings = new PrinterSettings();
IntPtr hPrinter = IntPtr.Zero;
IntPtr dm = IntPtr.Zero;
try
{
if (OpenPrinter(PrinterSettings.InstalledPrinters[0], out
hPrinter, IntPtr.Zero) == 0)
return;
IntPtr input = IntPtr.Zero;
Int32 cb = DocumentProperties(this.Handle, hPrinter,
settings.PrinterName, IntPtr.Zero, ref input, 0);
if (cb <= 0)
return;
dm = Marshal.AllocHGlobal(cb);
if (dm == IntPtr.Zero)
return;
int ret = DocumentProperties(this.Handle, hPrinter,
settings.PrinterName, dm, ref dm, DM_IN_BUFFER | DM_OUT_BUFFER |
DM_IN_PROMPT);
}
finally
{
if (dm != IntPtr.Zero)
Marshal.FreeHGlobal(dm);
if (hPrinter != IntPtr.Zero)
ClosePrinter(hPrinter);
}
}
As soon as the last call to the DocumentProperties gets executed, the
program crashes and the following error message is displayed:
"Function address 0x... caused a protection fault. (Exception code:
0xc0000005). Some or all property page(s) may not be displayed."
I've got no idea what is wrong with the above code, since the same
code just works fine under CPP.
Any help would be highly appreciated,
TIA,
Mehdi
Consider the following code:
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int OpenPrinter(string pPrinterName, out
IntPtr phPrinter, IntPtr pDefault);
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Unicode,
ExactSpelling = false, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
private static extern int DocumentProperties(IntPtr hwnd,
IntPtr hPrinter, String pDeviceName, IntPtr pDevModeOutput, ref IntPtr
pDevModeInput, int fMode);
const int DM_UPDATE = 1;
const int DM_COPY = 2;
const int DM_PROMPT = 4;
const int DM_MODIFY = 8;
const int DM_IN_BUFFER = DM_MODIFY;
const int DM_IN_PROMPT = DM_PROMPT;
const int DM_OUT_BUFFER = DM_COPY;
const int DM_OUT_DEFAULT = DM_UPDATE;
private void test()
{
PrinterSettings settings = new PrinterSettings();
IntPtr hPrinter = IntPtr.Zero;
IntPtr dm = IntPtr.Zero;
try
{
if (OpenPrinter(PrinterSettings.InstalledPrinters[0], out
hPrinter, IntPtr.Zero) == 0)
return;
IntPtr input = IntPtr.Zero;
Int32 cb = DocumentProperties(this.Handle, hPrinter,
settings.PrinterName, IntPtr.Zero, ref input, 0);
if (cb <= 0)
return;
dm = Marshal.AllocHGlobal(cb);
if (dm == IntPtr.Zero)
return;
int ret = DocumentProperties(this.Handle, hPrinter,
settings.PrinterName, dm, ref dm, DM_IN_BUFFER | DM_OUT_BUFFER |
DM_IN_PROMPT);
}
finally
{
if (dm != IntPtr.Zero)
Marshal.FreeHGlobal(dm);
if (hPrinter != IntPtr.Zero)
ClosePrinter(hPrinter);
}
}
As soon as the last call to the DocumentProperties gets executed, the
program crashes and the following error message is displayed:
"Function address 0x... caused a protection fault. (Exception code:
0xc0000005). Some or all property page(s) may not be displayed."
I've got no idea what is wrong with the above code, since the same
code just works fine under CPP.
Any help would be highly appreciated,
TIA,
Mehdi