Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Problem with calling vc6.0 dll methods from the visual studio .net (ver 7.1)

Hi,

Please help me to fix the following problem.

I have a MFC based application compiled with visual studio.net (version 7.1), uses a dll compiled with vc++ 6.0 9implicity linked with .lib)

Iam getting a access violation error, when i invoke one of the dll method which takes a const string reference.

the method declaration in the dll(6.0);

 virtual void setWorkingDirectory(const std::string& dir_path);


the caller application side(7.1) :

      string working_dir = "C:\\temp";
      factory->setWorkingDirectory(working_dir);

the application crashes (memory damaged) when the string is trying to free the memory, when its exisitng from the dll method.

Any idea?

Hari

      
0
pphari
Asked:
pphari
  • 2
2 Solutions
 
rcarlanCommented:
Exchanging MFC, STL, ATL (and any other class library) objects between two different library versions should not be attempted unless you can confirm binary compatibility. Also, you should not allocate memory in one library version and release it another.


Just in case the above sounds too abstract, here is some food for thought.

Lets say you export this class from a library:

class CMyInt
{
      public:
            CMyInt(int n) : m_nValue(n) {}
            
            const CMyInt& operator=(int n)
            {
                  m_nValue = n;
                  return *this;
            }

            operator int() const {return m_nValue;}

            const char* ToString() const
            {
                  static char sText[20];
                  sprintf(sText, "%d", m_nValue);
                  return sText;
            }
            
      private:
            int m_nValue;
};


You then implement a DLL (lets call it MyDLL) which exports this function:

extern "C" __declspec(dllexport) void DoSomething(const CMyInt& myInt)
{
      int n = myInt;
      const char* s = myInt.ToString();
      
      // ... do something with n and s
}

Obviously, MyDLL links to your library.

You can now implement an application (MyApp) which links statically to your library and dynamically to MyDLL and does something like this:

#include "MyInt.h"      // declaration for CMyInt
#include "MyDLL.h"      // exports for MyDLL

int main()
{
      CMyInt myInt = 50;
      DoSomething(myInt);
      return 0;
}

Everything's fine so far.

Now, you decide that the first implementation of CMyInt is not really as good as it could be. Using a static buffer to convert the int to text is obviously not the best of solutions, which may have become apparent after somebody wrote something like this:

CMyInt myInt = 1;
const char* s1 = myInt.ToString();
myInt = 2;
const char* s2 = myInt.ToString();
std::cout << "1=" << s1 << " and 2=" << s2;

Also, because you discover through profiling that CMyInt instances are not changed very often (i.e. initialised by the constructor and then left alone) but the ToString method is called all over the place, you decide to improve your class like so:

class CMyInt
{
      public:
            CMyInt(int n) : m_nValue(n)
            {
                  sprintf(m_sValue, "%d", m_nValue);
            }
            
            const CMyInt& operator=(int n)
            {
                  m_nValue = n;
                  sprintf(m_sValue, "%d", m_nValue);
                  return *this;
            }

            operator int() const {return m_nValue;}

            const char* ToString() const
            {
                  return m_sValue;
            }
            
      private:
            char m_sValue[20];
            int m_nValue;
};

You now compile MyApp but do not compile MyDLL (i.e. link MyApp with the new library and the old DLL). There will be no compilation or linker errors.
What do you think will happen when you call DoSomething?


This is more or less what you are trying to do.
Memory allocations and deallocations further complicate matters, but for similar reasons: different data structures, memory blocks, allocation and deallocation algorithms in the two different versions of the same library.

Radu
0
 
AlexFMCommented:
Replace const std::string& with const char*
0
 
rcarlanCommented:
Hari,

Did you manage to call your VC6 DLL from VC7.1?

Radu
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now