?
Solved

First dll question - why is strcat not working

Posted on 2004-11-28
4
Medium Priority
?
286 Views
Last Modified: 2010-04-24
I have written a function in a dll project, which is not working.
__declspec(dllexport) char* SayHello(char* str)
{
      char* ret = strcat("Hello, ",str);
      return ret;
}

However this function works!!
__declspec(dllexport) char* SayHello(char* str)
{
      return str;
}

Can you tell me what is wrong here. Also, please tell what is the right way to work with strings here. I am basically a Java programmer, and I find it really easy to work with String object there. Is there a couterpart in Visual C++.NET also.
0
Comment
Question by:kumvjuec
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12690958
"Hello" is a constant string. It memory address doesn't have more room available to concatenate 'str', so, basically you are invading some memory area.

A correct implementation could be:

__declspec(dllexport) char* SayHello(char *buffer, char* str)
{
     strcpy(buffer, "Hello, ");
     return strcat(buffer, str);
}

Also you can implement a static buffer inside function:

__declspec(dllexport) char* SayHello(char* str)
{
    static buffer[80]; // Leave enough space for 'str'

     strcpy(buffer, "Hello, ");
     return strcat(buffer, str);
}
0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12690981
hi oliver,
can I also allocate space using new in the function. If yes, how should I go about deleting it?
Thanks,
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12691390
Yes, you can, something like:

__declspec(dllexport) char* SayHello(char* str)
{
    char *buffer=new char[80]; // Leave enough space for 'str'

     strcpy(buffer, "Hello, ");
     return strcat(buffer, str);
}

Just you must delete the buffer at the caller side. Something like this:

char *message = SayHello("John");
printf(message);
delete message;

0
 
LVL 3

Author Comment

by:kumvjuec
ID: 12693694
Thanks Jaime
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

862 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