Solved

How to export char** in a class method in a DLL?

Posted on 2004-05-03
4
279 Views
Last Modified: 2012-08-14
I have a class exported as such

class  __declspec(dllexport) SigObject
{
public:
  void GetUsername(char **username)
  {
    *username = new TCHAR[10];
    strcpy(*username,"Hello");
  }
}

I include this class file and compile in a main program as such:
int main()
{
SigObject *myObject;
myObject = new SigObject();
char *username = NULL;
myObject->GetUsername(&username);
printf("\nUsername: %s\n",username);

delete[] username;
delete myObject;
}

which runs without error.  However, when I create the class file as a DLL and include the .lib file into the main program and call it, I get an exception at "delete[] username".  Can anyone help me to understand why?
0
Comment
Question by:pcssecure
4 Comments
 
LVL 3

Expert Comment

by:akalmani
ID: 10976361
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10976426
Generally, memory allocated in Dll should be released in the same Dll. Add function ReleaseUserName(char*):

public:
  void GetUsername(char **username)
  {
    *username = new TCHAR[10];
    strcpy(*username,"Hello");
  }
 
   void DeleteUserName(char* username)
   {
        delete[] username;
   }
}

Client code:

int main()
{
SigObject *myObject;
myObject = new SigObject();
char *username = NULL;
myObject->GetUsername(&username);
printf("\nUsername: %s\n",username);

myObject->DeleteUserName(username);
delete myObject;
}

Another way is using of clller-allocated buffer.


0
 
LVL 9

Expert Comment

by:Cayce
ID: 10977825
Everything posted above is ok, and the proper answer to your question.

However I would advise you on forgetting about C strings and using the better C++ strings:

class  __declspec(dllexport) SigObject
{
private:
  string username;
public:
  string GetUsername()
  {
    username = "hello, whatever";
    return username;
  }
}

int main()
{
  SigObject *myObject;
  myObject = new SigObject();
  string username;
  username = myObject->GetUsername();
  printf("\nUsername: %s\n",username.c_str());
  delete myObject;
}
0
 
LVL 2

Accepted Solution

by:
vhss earned 500 total points
ID: 10985253
The problem is trying to free memory that was in private heap, which works when compiled without the DLL as compared to freeing memory in default process heap when compiled with DLL.

Apparently, your class does the allocation of memory, not the caller and you are passing the memory out.  To do this, you should use CoTaskMemAlloc/CoTaskMemFree instead.

Refer to:
http://archives.neohapsis.com/archives/microsoft/various/dcom/2001-q1/0182.html

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 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