Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey there,

I hope someone out there is alot smarter than I am :) .
I have this strange problem when accessing a static library written in VS2005 from a VS2003 MFC-project. Code is in native C++.

I use static binding in my VS2003-project.

I have no trouble accessing the lib, and executing the methods, but when I pass an std::string by ref (or an std::vector) in VS2003, it seems that the memory-location in VS2005 is shifted by 4 bytes, resulting in a <removed ptr=""> and a crash.

I call a method "Foo(std::string& arg_szError);"
In VS2003, the address of my string is 0x0013F4C0, but when I debug Foo in VS2005, the address of my string suddenly became 0x0013F4C4, with the known result.

The funny thing is that it actually worked for a little while, but I had to move some projects in my solution to different directories and since then, I get this very annoying problem.

I think something (most probably me :( ) messed up the code-generation and/or linking options.
Here are some options which, I think, could be useful.

VS2003:
General options: application (exe), use MFC in shared dll, not using ATL, MBCS
C/C++: Multi-threaded Debug DLL, default struct member alignment, __cdelc
Linker: not ignoring default libraries

VS2005:
General options: static library, use standard windows libraries, dynamic link to ATL, MBCS
C/C++: Multi-threaded Debug, default struct member alignment, __cdelc

The main reason for using 2 different IDE's is that I'm using a template-library to do some matrix-calculations which only ships for VS2005 or higher, and my main-app comes with 3rd party libs written for VS2003 (I tried to upgrade, but it failed due to some MFC-references in the libs, which of course don't belong there).

Thanks in advance for trying to help me out of this!
Posted

Over time I have learned not to mix static libraries from different compilers. The reason is that now you have two versions of CRT running in same process which is recipe for disaster. Best solution is to create a DLL for the VS2005 project and then use it in VS2003 project.

-Saurabh
 
Share this answer
 
Hi Saurabh.Garg,

I actually started it that way, created a dll, and used it in my VS2003-project by dynamically linking it. It worked for some time, but after re-ordering my solution, it got messed up. Some colleagues adviced me to use static linking.
I found a little error in my posted question, being that the address of the std::vector I pass by reference is the same in both VS2003 and VS2005, and not shifted by 4 bytes as happens when I pass a std::string. But I can't read the elements of the vector. It always appears to be empty.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900