Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

  1. #1
    Join Date
    Mar 2011
    Posts
    5

    Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

    Hi All,

    I am trying to access Native C++ dll built in VS2010 from an C++/CLI or Managed C++ exe built in VS2008 .NET framework 3.5. I have included the header file of the Native C++ dll and included lib file for linker input, project compiled successfully. But when executing i get below exception on the line where it calls function of Native C++ dll.

    "
    An unhandled exception of type 'System.AccessViolationException' occurred in ExtrinsicModelWrapper.exe

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

    Below is the code in header file i included.


    Code:
    __declspec(dllexport) void mockStorage_calc(DataCollector & input);
    Below is the code in C++/CLI exe.

    Code:
    DataCollector * dc = new DataCollector(	);
    mockStorage_calc(*dc);//Code where exception occurs
    Please help. For my knowledge it seems the Native C++ dll built in VS2010/VS2008, will not have difference as it does not depends on dotnet framework. But i am a beginner.Please put your thoughts on this.

    Thanks
    Naresh

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

    Quote Originally Posted by __Naresh View Post
    Below is the code in header file i included.
    Code:
    __declspec(dllexport) void mockStorage_calc(DataCollector & input);
    Reference types are C++ types only, and are only guaranteed to work if you are calling that function from a C++ application created with the same compiler you created the DLL with.
    Please help. For my knowledge it seems the Native C++ dll built in VS2010/VS2008, will not have difference as it does not depends on dotnet framework.
    References are not guaranteed to have the same internals from compiler to compiler. The proper way to do what you want is to use pointer parameters, not references.

    Anyway, why don't you just debug the issue? Where does it have an access violation? Is it within the function itself, or before the function starts execution?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2011
    Posts
    5

    Re: Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

    thanks a lot for the reply Paul, I changed the input parameters to the method from reference parameters to pointer parameters. It did not work ,it failed with same error. Then we thought of trying using a simple data type than class. We tried to send a string using reference it failed with

    "External component has thrown an exception,System.Runtime.InteropServices.SEHException: External component has thrown an exception"

    Then as you suggested, we changed the input parameter to pointer input, . it worked for string. Now we are guessing, there is some issue with our DataCollectorClass. Any Thoughts from you will be really helpful. The class has many fields, if required i will post the whole code.

    Thanks Again
    Naresh

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

    Quote Originally Posted by __Naresh View Post
    Now we are guessing, there is some issue with our DataCollectorClass. Any Thoughts from you will be really helpful. The class has many fields, if required i will post the whole code.
    You compiled the DLL with one version of the compiler, but you are sending it an object (or pointer to an object) created with another version of the compiler. Do you see that this could be a problem?

    For each version of the compiler, the internals of the standard classes will be different. For example, internally a std::string in VC 2008 is not the same std::string as in VS 2010. If your DataCollector class contains members such as std::string, or some other types where the internals could have changed, then your DataCollectorClass will not work between versions.

    Again, if you pass C++ types between functions, you must use the same compiler and compiler options. If you pass a pointer to these types and the DLL will be manipulating this object then again, both DLL and application must be compiled with the same compiler (as well as use the same heap).

    The only types of parameters that are guaranteed to work, regardless of the version of the compiler, are the Windows types (LONG, LPLONG, LPCSTR, DWORD, pointers to these types, etc.). For example, I have a DLL that works for all versions of Windows from Windows 2000 up to Windows 7. The reason why it works is because all of the functions take simple parameters such as I described, not C++ references or C++ types.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2011
    Posts
    5

    Re: Exception when accessing VS2010 Native C++ DLL in VS2008 C++/CLI exe

    Hi Paul,

    Thanks a lot for replying. Your inputs helped us a lot. The issue is same as you have mentioned above. The DataCollector class has many data types which are part of STL and Boost libraries. They objects created in VC 8 Managed C++ wrapper are not compatible with dll built in VC10 compiler. Now we are thinking of upgrading our framework to VC 10 to avoid any more hassles. Thanks again for your time and inputs, it has saved us lot of time. Thanks to Code Guru .com for having such great members and forum.

    Regards
    Naresh

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center