Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4024728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:45:27+00:00 2026-05-20T10:45:27+00:00

I am getting a System.AccessViolationException on a call to ResetDC when passing my Devmode.

  • 0

I am getting a System.AccessViolationException on a call to ResetDC when passing my Devmode. I am doing the following..

// create and marshall the devmode to a byte array.

DEVMODE myDevmode = new DEVMODE {dmSize = (short) Marshal.SizeOf(typeof (DEVMODE))};

myDevmode.dmFields = DM.Orientation;
myDevmode.dmOrientation = DMORIENT_LANDSCAPE;

IntPtr ptDM = Marshal.AllocHGlobal(myDevmode.dmSize);
Marshal.StructureToPtr(myDevmode, ptDM, false);

byte[] byteDM = new byte[myDevmode.dmSize];
Marshal.Copy(ptDM, byteDM, 0, myDevmode.dmSize);

I then call across a com interface (existing architecture not possible to change) the method:

SetDevMode(ref byteDM[0], myDevmode.dmSize)

on the other side of the com interface and back in .net code the other side the following is run:

    public void SetDevMode(ref byte pDevMode, int nDevModeSize)
    {
        _pageSetupDevMode = new byte[nDevModeSize];

        unsafe
        {
            fixed (byte* p = &pDevMode)
            {
                Marshal.Copy((IntPtr)p, _pageSetupDevMode, 0, nDevModeSize);
            }
        }
    }

and at a later point a call to ResetDC is made:

Win32.ResetDC(_hRefDc, ref _pageSetupDevMode[0]);

at which point I get the exception.

An unhandled exception of type ‘System.AccessViolationException’ occurred
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

This occurrs in gdi32.dll

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:45:27+00:00Added an answer on May 20, 2026 at 10:45 am

    As mentioned in the comment above, this was not an issue with the memory handling but rather the result of passing an all but empty devmode into ResetDC – I had seen this done in a sample online but it seems to ‘upset’ gdi while printing.

    I am now getting a valid devmode from the printer as the basis of the above code and this works fine. Apologies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When designing a new system or getting your head around someone else's code, what
My organization is getting ready to implement a new system, which is a asp.net
I'm getting the following error when I try to Serialize an HttpWebRequest Type 'System.Net.KnownHttpVerb'
After Getting a System.Reflection.PropertInfo array for a class- Does anyone know how or if
I'm getting a System.ArgumentException Value was invalid when calling RSACryptoServiceProvider.SignData with the following code:
I keep getting System.OutOfMemory exception in my WinForms App which is thrown while doing
Hi Anyone know of a simple system of getting xml formatted inside an editor.
I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException:
I'm getting ready to implement a source control system (subversion) but I'm facing some
I'm trying to use System.DirectoryServices in a web site project and I'm getting this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.