locked
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt" while printing. RRS feed

  • Question

  • I've been trying to resolve the problem for few days now.  I am a very beginner in C#, and I am playing around with Visual Studio 2008.  I try to print anything out from the windows form but every time I get the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."  This happens in debug mode, as well as fully compiled version. 

    The source is in System.Drawing

    Here is the stack trace:


    "   at System.Drawing.SafeNativeMethods.DeviceCapabilities(String pDevice, String pPort, Int16 fwCapabilities, IntPtr pOutput, IntPtr pDevMode)\r\n   at System.Drawing.Printing.PrinterSettings.FastDeviceCapabilities(Int16 capability, IntPtr pointerToBuffer, Int32 defaultValue, String printerName)\r\n   at System.Drawing.Printing.PrinterSettings.Get_PaperSizes()\r\n   at System.Drawing.Printing.PageSettings.PaperSizeFromMode(DEVMODE mode)\r\n   at System.Drawing.Printing.PageSettings.GetPaperSize(IntPtr modeHandle)\r\n   at System.Drawing.Printing.PageSettings.GetBounds(IntPtr modeHandle)\r\n   at System.Drawing.Printing.PrintController.CreatePrintPageEvent(PageSettings pageSettings)\r\n   at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)\r\n   at System.Drawing.Printing.PrintController.Print(PrintDocument document)\r\n   at System.Drawing.Printing.PrintDocument.Print()\r\n   at System.Windows.Forms.PrintPreviewControl.ComputePreview()\r\n   at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()\r\n   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)\r\n   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)\r\n   at System.Threading.ExecutionContext.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)\r\n   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.PrintPreviewControl.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)\r\n   at System.Windows.Forms.Form.ShowDialog()\r\n   at Inventory_Tracking_System.InventoryMenu.button5_Click(Object sender, EventArgs e) in C:\\Users\\nir\\Documents\\Visual Studio 2008\\Projects\\Project1\\Project1\\Inventory Tracking System\\Inventory Tracking System\\Form2.cs:line 130"


    and here is the code


            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {

                Graphics g = e.Graphics;
                String message = "dupa";
                Font messageFont = new Font("Arial",
                         24, System.Drawing.GraphicsUnit.Point);
                g.DrawString(message, messageFont, Brushes.Black, 100, 100);
            }


            private void button5_Click(object sender, EventArgs e)
            {
                printPreviewDialog1.ShowDialog();                                                         <---- This is line 130
            }

    The weirdest part about the problem is that the program actually works on two other machines I tested it with ( Windows XP, and Vista).  The machine I'm coding with crashes every time I click the print preview button, or anything that has to do with printing for that matter ( I tried everythink I could think of).  Any ideas or suggestions ?  Thank You
    Tuesday, April 1, 2008 11:27 PM

Answers

  • Reinstall the printer driver. Make sure it has the newest version.

    Wednesday, April 2, 2008 12:47 AM