Issue with PrintWatchQueue

Jul 23, 2009 at 7:59 PM

Hello!

I have been trying to use the PrintWatchQueue component, but I have been running into an odd issue... When adding any printer using the .AddPrinter method I receive the following error:

Attempted to read or write protected memory. this is often an indication that other memory is corrupt.

I was using the latest version of the component (2.0.7.0) on a WindowsXP 64 box, Visual Studio 2005. When I tried to open the source code, I discovered it was written in VS 2008. Thinking that it might be a problem with the target CPU, I loaded the source code in a Server 2008 (32 bit)/VS 2008 virtual machine that I have. I checked - the target CPU was set to "Any CPU", so that wasn't the problem...

I recompiled anyhow, and copied the new dll back to my application on the 64 bit machine. Ran the program - encountered the same error. I then switched gears, and moved my app to the virtual machine. Converted the project to VS2008, compiled and ran it - BINGO! No error! Interesting - is this a 32 bit vs. 64 bit Framework issue? On to further investigation...

I needed to debug your source code to identify the location of the error, but the only source I had was VS 2008. So, I dug around a bit on MerrionComputing.com and on the CodePlex PUMA site. I found that the PrintQueueWatch code in the PUMA source was VS 2005. I recompiled the code, replaced the dll in my app, and was able to establish debugging. Here is what I found:

When I call the .AddPrinter method, the code executes successfully through creating a New PrinterInformation class when the DesiredAccess is set to 131080, but when it tries to create the class with DesiredAccess set to PrinterAccessRights.PRINTER_ALL_ACCESS (983052) the code throws the protected memory error.

So there is the source of the error, but my knowledge of the Printer world is much too limited for me to make any sense of it. Do you have any ideas?

Thanks,

Eric

Coordinator
Jul 24, 2009 at 7:56 AM

It does indeed look like a 32/64 bit issue which is a problem as I don't have a 64 bit rig.

The most likely cause would be the use of an Int32 where an Integer (32 or 64 depending on framework) should be used.

e.g.

 

<font size="2" color="#0000ff"><font size="2" color="#0000ff">

#Region

</font></font><font size="2" color="#0000ff">

 

</font>

"PRINTER_DEFAULTS STRUCTURE"

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

<font size="2">

// <MarshalAs(UnmanagedType.U4)>

</font>

 

Friend Class PRINTER_DEFAULTS

    Public lpDataType As Integer

    Public lpDevMode As Integer

    Public DesiredAccess As PrinterAccessRights

Coordinator
Jul 24, 2009 at 2:21 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 24, 2009 at 3:53 PM

Hello again!

Here is a great article by Scott Hanselman (Microsoft) that explains what the issue is with Inptr variables:

http://www.hanselman.com/blog/BackToBasics32bitAnd64bitConfusionAroundX86AndX64AndTheNETFrameworkAndCLR.aspx

I will play around with the source code this AM to see if I can get things working...

Eric

Coordinator
Jul 27, 2009 at 1:34 PM

You need to get rid of the MarshalAs(U4) as it does not fit in that on Win64.

Not sure what other changes would be needed...?