1

I have made a DLL with VC++ 2008 and when i use it in console application VC++ 6.0, there is an exception:

(msvcr90.dll): 0xc0000005: Access Violation

Neysor
  • 3,893
  • 11
  • 34
  • 66
Ahmed Mostafa
  • 419
  • 6
  • 16
  • I have never tried but if i would guess then i think we cannot because there are many differences even in the way we write a c++ program in VS2008 and VS2006 for example in 2006 we have header files with .h extension but not in 2008. But the main question is WHY DO YOU WANT TO GO FROM 2008 TO 2006? – Rameshwar.S.Soni Apr 02 '12 at 16:18
  • 1
    Maybe, maybe not. If you are really, really careful when you design the interface, it is certainly possible. But in the general case - No, it will not work. – Bo Persson Apr 02 '12 at 16:22
  • 2
    You'll need to export a C-style interface from the 2008 project, exporting of C++ classes only work within one version of the compiler if I recall. – PeskyGnat Apr 02 '12 at 16:31
  • Guys, I don't think this question qualifies as "not a real question." A little overzealous with the closing, are we? – Mahmoud Al-Qudsi Apr 02 '12 at 16:43

2 Answers2

5

Access Violation in this case can mean so many things, and the msvcr90.dll reference can be very much misleading. If you pass invalid data to any of the MSVC standard library functions, the access violation will occur within msvcr90.dll and not in your code (when viewing the stack trace or looking at the exception info.

That said, there should not, in theory, be a problem using a VC9 DLL in VC++ 6, as the ABI has not changed and the PE format is the same. You might have problems if msvcrt9.dll is unsupported on your platform (for instance if you're running MSVC6 on Windows NT), but otherwise it means you need to review your code.

What I mean is: attach a debugger and look at what's happening beneath the scene!

One more note: when using different versions of the MSVC libraries dynamically, you MUST NOT allocate data in one library and free it in another as they are not guaranteed to be using the same heap and you can get memory corruption (and Access Violation errors) rather easily like this. This also means that if you're writing C++, you must not create an object and then pass it by return value to the calling app as that's what will happen beneath the scenes.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
3

If you want to build a DLL with Visual C++ version X and use it in Visual C++ version Y, you have some options:

  1. Build a DLL which exposes a pure C interface. You can use C++ inside the DLL, but the public interface must be pure C (so, for example, you can't throw exceptions crossing DLL boundaries).
  2. Build a COM DLL (possibly with the help of tools like ATL).
  3. Build a DLL using COM-like techniques, i.e. expose only abstract interfaces and factory functions from your DLL (this technique is explained in this article on CodeProject "HowTo: Export C++ classes from a DLL", in particular in the paragraph "C++ Mature Approach: Using an Abstract Interface").

It is also important to point out that the code which allocates memory and the code which frees memory must use the same allocator.