First dll question - why is strcat not working

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.
LVL 3
kumvjuecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
"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
kumvjuecAuthor Commented:
hi oliver,
can I also allocate space using new in the function. If yes, how should I go about deleting it?
Thanks,
0
Jaime OlivaresSoftware ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kumvjuecAuthor Commented:
Thanks Jaime
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.