'System.AccessViolationException' :Attempted to read or write protected memory. This is often an indication that other memory is corrupt. RRS feed

  • Question

  • Our application is calling a VC++ 6.0 (MFC Automation server) dll from a sample VB.Net (VS 2017 frame work 4.7.2), while calling a method from .Net it is throwing the below error message.

    The same functionality is working fine from VB6 application, it is happening only when calling from a .Net 4.7.2 executable.

    'System.AccessViolationException' in mscorlib.dll

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

    VB.Net code

           Dim obj As New MFCDLL.MFCclass

    Dim obj1 As object

           obj1 = obj.GetNextMethodRef()

    VC++ 6.0 MFCDLL (MFC/ATL automation server) code

    LPDISPATCH MFCClass::GetNextMethodRef


    //returns an IDispatch pointer to the Class Object




                  return ClassMethod1()->GetIDispatch(TRUE); “Access Violation Error is thrown here”


           Catch (ErrorClass* pe)




    Declaration of m_PointertoMFCClass2

    MFCClass2* m_PointertoMFCClass2;

    MFCClass2* MFCClass:: ClassMethod1()


    //returns a pointer to the portfolio object




                  return m_PointertoMFCClass2;


           Catch (ErrorClass * pe)




    Whereas other methods and functions of this DLL which are not using GetIDispatch methods are working fine.

    Please let us know if any limitation is there of using VC ++ 6 (MFC) dll with .Net. Also, do let us know resolution of this issue.

    Thursday, December 12, 2019 3:00 PM

All replies

  • Did you check if the code that is responsible for initialisation of m_PointertoMFCClass2 was executed?

    Thursday, December 12, 2019 6:03 PM
  • Yes Viorel, it is getting executed when mfcclass is initialised. Where this is set to 0
    Thursday, December 12, 2019 6:13 PM
  • Hi Viorel,

    This issue got resolved, you are correct the m_PointertoMFCClass2 was not getting initialized.

    After Initializing the m_PointertoMFCClass2, the issue got resolved.

    However, after our program running for around 30-40 mins, we are get the access violation exception at the same place. Please let us know how can we debug this issue.

    Friday, December 13, 2019 9:56 AM
  • Hi Chandrashekhar C, 

    Thanks for your feedback.

    For your question, you want to debug access violation exception.

    I find some related references about the question, and you can refer to them.

    1. Troubleshooting Exceptions: System.AccessViolationException
    2. Debugging System.AccessViolationException - DllImport hell

    Hope them could be helpful.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, December 16, 2019 9:07 AM
  • Thanks Xingyu. We will go through the links and try to implement the same to debug our code.
    Friday, January 3, 2020 11:28 AM