can i create Dll with VC++ 2008 and use it in VC++

2019-02-28 21:45发布

问题:

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

回答1:

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.



回答2:

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.