Protection Fault!

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
 
V

VJ

Did you try putting it under a general catch and try getting a exception.
maybe that will give you a better idea. Other option will be too see if this
machine speicfic

VJ
 
M

mehdi

Did you try putting it under a general catch and try getting a exception.
maybe that will give you a better idea. Other option will be too see if this
machine speicfic

VJ


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;

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

Does it have anything to do with memory allocation, pinning pointers
and the like?
 
V

VJ

It could be. I just googl'd and got a lot of information on the error code,
maybe you can try the same, see which situation applies to you

VJ

mehdi said:
Did you try putting it under a general catch and try getting a exception.
maybe that will give you a better idea. Other option will be too see if
this
machine speicfic

VJ


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;

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

Does it have anything to do with memory allocation, pinning pointers
and the like?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top