Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Printing / PrintDialog Problem - "Attempting to read or write protected memory."
 

Printing / PrintDialog Problem - "Attempting to read or write protected memory."

This problem is solved.

The Vista computer in question would print documents using other applications, but wouldn't print documents using my application. Therefore I didn't think there was a problem with the print drivers. Microsoft suggested that the problem might be print drivers and a friend with a lot of PC experience suggested that the problem might be print drivers also.

If anyone else is getting the error in question on aPrintDialog, tell them to reinstall their print driverseven if they can print with other applications.

I reinstalled the print drivers and the problem is no longer a problem.

I would like to thank the person who responded to myproblem post.

G. Hackbart

I have a strange issue with a Visual Studio .net 2008 distributable windows forms application.

The printing does not work on my Clients computers who have a Vista OS. All of my clients get the following error when they try to print any form from a Vista operating system.

The PrintDialog comes up, the Client Clicks on OK to print and the following error is generated. The following exception is caught and the program gets no farther in the code. The program errors off in the PrintDialog.

Programmed error �An Error occurred printing the report�/font>

System Generated error �“Attempting to read or write protected memory. This is often an indication that other memory is corrupt�

Note - I am using the following code to print windows forms. I get the above error on any Vista computer except mine.The above application prints and runs ok on my computer which has a Vista OS. The application also prints and runs ok on Windows XP operating systems.

I have Visual Studio 2008 installed, Visual Studio 2003 installed and VB 6.0 installed on my vista computer. I have been printing VB forms with code similar to this for about 8 years.


I'm not sure why the program doesn't get past the PrintDialog on my clientvista computers.Are myvisual Studio security setting screwed up?

Any help would be greatly appreciated!!!

The Print code follows.

SubDo_Print()
Try
DimdlgAsNewPrintDialog
dlg.Document=pd
DimresultAsDialogResult=dlg.ShowDialog()
If(result=System.Windows.Forms.DialogResult.OK)Then
DimpsDlgAsNewPageSetupDialog
psDlg.PageSettings=NewPageSettings
Ifresult=DialogResult.OKThen
pd.Print()
EndIf
EndIf
CatchexAsException
MessageBox.Show("Anerroroccurredprintingthereport-"+ex.Message)
EndTry
EndSub
PrivateSubpd_PrintPage(ByValsenderAsObject,ByValevAs
System.Drawing.Printing.PrintPageEventArgs)Handlespd.PrintPage
CaptureForm()
imgBuffer.RotateFlip(RotateFlipType.Rotate90FlipXY)
ev.Graphics.DrawImage(imgBuffer,0,0)
EndSub
PrivateSubCaptureForm()
DimhResAsLong
DimhdcAsIntPtr
DimgraphicAsGraphics=Me.CreateGraphics
imgBuffer=NewBitmap(1000,1000,graphic)
DimimgImageAsGraphics=Drawing.Graphics.FromImage(imgBuffer)
imgImage.FillRectangle(NewSolidBrush(Me.BackColor),0,0,imgBuffer.Width,imgBuffer.Height)
hdc=imgImage.GetHdc()
frmpincome.DefInstance.AutoScrollPosition=NewPoint(0,0)
hRes=SendMessage(frmpincome.DefInstance.Handle,NewIntPtr(WM_Print),hdc,NewIntPtr(PRF_CHILDREN+PRF_CLIENT+PRF_OWNED))
imgImage.ReleaseHdc(hdc)
EndSub

GHackbart  Sunday, January 18, 2009 10:26 PM
Hi GHackbart,

I'm trying your code to print the form, the problem doesn't appear in my Vista Operating System either. I'm thinking the problem may be caused byInteroperability. I've seen many people meeting this kind of exceptions when they are using Interoperability. I know that you are trying to capture the Form and do some transformation. Lastly, print it in the device. Why not write this function in managed code?


Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Tuesday, January 20, 2009 3:02 AM
Hi GHackbart,

Thanks for sharing your experience. Surely, other people who meet this kind of problems will benifit from your answers.

Since you edit and put your anwer in the original post, but not in a new post. I can't mark your post as the answer. So can you reply this thread and begin with the answer in the new post? then I can mark this thread as answered. Besides, separating the question and the answer will also help the customer read this thread.

Best regards,
Bruce Zhou

Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Wednesday, January 21, 2009 2:33 AM

You can use google to search for other answers

Custom Search

More Threads

• VB.NET and MySQL
• CustomForm problem
• Global Assembly Cache (GAC)
• How to allow resizing in one direction only?
• Button Click Event in C++
• FindControl.
• How to automatically resize a listView control in SmallIcon view, so that all items are visible
• Using Custom ProfessionalColor Table with toolstriprenderer.
• custom control for what
• Time slider Control