Solved

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

Posted on 2004-05-03
4
270 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now