none
Unmanaged VC++ DLL in VS2010 RRS feed

  • Question

  • Hi,

    I have a C# desktop application that talks to a C++ DLL through a COM Interop.
    I am currentlly modifying the C++ DLL which makes use of MFC. The code was initially written in VC++ 6.0. I have upgraded the same to VS2010.
    The exported class contains many std::string private variables. Whenever I assign a CString value to these variables, I get the exception -
    "An unhandled exception of type 'System.AccessViolationException' occurred in XXX.exe
    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    I tried passing in wide-char (CStringW to std::wstring), unchecked/checked 'Suppress JIT optimization on module load (Managed only) but the error keeps coming unchanged.

    The error is very inconsistent. It occurs sometimes on the first assignment, sometimes somewhere in the middle & sometimes in the end.

    How am I supposed to resolve this? Please help me. I need to resolve this ASAP. 

    Thanks..


    • Moved by Shawn Zhao Tuesday, January 6, 2015 1:56 AM
    Monday, January 5, 2015 12:43 PM

Answers

  • You can't create the class on the stack if it will be used later after your function returns since it will automatically clean them up and free the memory.  The other code probably expects that the string will still be around later.  It's generally a code idea to instantiate your class objects on the heap anyway when sending them to external code.

    WinSDK Support Team Blog: http://blogs.msdn.com/b/winsdk/

    Friday, January 9, 2015 3:30 PM

All replies

  • Why are you assigning a CString object to a std::string object?  Why aren't you using a std::string object instead?  If you have a CString object from your other code, you can always just make a std::string object with std::string* stdString = new std::string((LPCSTR)CStringString).

    WinSDK Support Team Blog: http://blogs.msdn.com/b/winsdk/

    Monday, January 5, 2015 3:47 PM
  • Thanks for the info..

    I changed the string variables to string pointers in the header file and 'newed' them within the cpp. Now when I assign values to them, they do not throw the exception.

    Why is it that when variables are created on the stack, the exception gets triggered? (I did duly convert the CString object to std::string).

    Is it necessary to create all the class members on the heap? Is this behavior seen because I am calling this unmanaged code from a managed environment?

    Tuesday, January 6, 2015 10:12 AM
  • You can't create the class on the stack if it will be used later after your function returns since it will automatically clean them up and free the memory.  The other code probably expects that the string will still be around later.  It's generally a code idea to instantiate your class objects on the heap anyway when sending them to external code.

    WinSDK Support Team Blog: http://blogs.msdn.com/b/winsdk/

    Friday, January 9, 2015 3:30 PM