Click here to Skip to main content
15,896,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have two DLLs DLL1 and DLL2
DLL1 calls DLL2 function

C++
char* Execute(char*,char*,...)      [DLL2 function]

char * retVal = obj->Execute("Test","","");



In DLL2 function Parameters passed correctly and evaluated in DLL2 correctly.
but when DLL2 Execute function return value to caller function like :

C++
char* retVal = obj->Execute("Test","","");

retVal is getting garbage values.

I have already tried with following alternatives:

string retVal and also changing function return type string& Execute(char*,char*,...)

Every time I got garbage value.
DLL1 is built in Release mode and DLL2 in debug mode.[can not change the mode].

Let me know if any one knows any help.
Posted
Updated 2-Nov-12 2:49am
v2
Comments
Timberbird 2-Nov-12 9:00am    
Do you have access to Execute() implementation? If yes, please show code which creates and assigns result

Hi,

It seems you are returning a string stored in the stack.

You can allocate the string with new/malloc and return the pointer but you have to remember to free that pointer inside the dll that created it.

Unless you add a FreeString api to your dll, I recommend to use Windows api's like SysAllocString or GlobalAlloc for this special cases.

Regards,
Mauro H. Leggieri
 
Share this answer
 
Another thing you could do is to pass a reference to the Execute function like this:

C++
void Execute(char **result, int resultLength, char *, char *, ...);


You would need to allocate memory for the result before the call and then free it yourself when done from within DLL1.

If you don't know the length of the result before the call to Execute, you could do the same thing Microsoft does with their libraries, and pass in NULL to Execute the first time. Execute would fill in the resultLength for you and then you could allocate your memory accordingly.

I do this a lot myself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900