?
Solved

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

Posted on 2005-03-29
6
Medium Priority
?
273 Views
Last Modified: 2013-11-25
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
Comment
Question by:pphari
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
rcarlan earned 189 total points
ID: 13657617
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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 186 total points
ID: 13659048
Replace const std::string& with const char*
0
 
LVL 9

Expert Comment

by:rcarlan
ID: 13735185
Hari,

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

Radu
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question