Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi,

I have a question regarding getting strings from unmanaged c++ code.

In my unmanaged code I have a following declaration:
__declspec(dllexport) bool A(const char*);


I call this function from my managed C++ in the following way:
C#
String* args=...;
char* str = (char*)(void*)Marshal::StringToHGlobalAnsi(args);
bool ret = wrapped->A(str);
Marshal::FreeHGlobal(str);


and everything works OK.

But how to get a string from this function if it looks like this using IJW technique?
__declspec(dllexport) char* AA(const char*);


Regards,
Luke
Posted
Updated 18-Oct-11 14:55pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Oct-11 20:55pm    
Fixed HTML, code formatting.
--SA
Sergey Alexandrovich Kryukov 18-Oct-11 20:57pm    
It does return string in the form of char*. What else do you need? Different string type? What's the problem? Or you want to return string from A? But it does not return string at all...
--SA
Mohibur Rashid 18-Oct-11 22:04pm    
AA function returns character pointer, If unmanaged C++ support it then do it like this
char *retarray;

retarray=aa(valuearray);
Sergey Alexandrovich Kryukov 19-Oct-11 1:31am    
So, what's the problem?
--SA
Mohibur Rashid 19-Oct-11 1:38am    
I didnt say its a problem, Its a suggestion, I cant answer directly cause I dont know Managed C++

1 solution

Thanks for your replies. Both below solutions work:
char* xx3 = wrapped->AAA();

String* ret = wrapped->AAA();

but what about memory management? Is it OK or causes memory leaks?
 
Share this answer
 
Comments
Stefan_Lang 19-Oct-11 9:52am    
It will indeed leak, unless the string pointed to is const or static (in which case it can't be released anyway).

You may not, however, just call delete (or free) on it if it is not. For one you normally don't know what method was used to allocate it in the library, and the implementation may change. More importantly, a librars heap may be an entirely different object as the heap of the main application, and therefore an attempt to release memory that was not allocated on the application heap might fail!

It is generally a bad idea to pass an object allocated on the heap from a library to extern. Instead you should always design external functions so that they require a buffer to be passed to them, which then can hold the result. Alternately you could return a string type, such as std::string. That would cause the library function to create a temporary result value in the address space of the lib, which is then copied to a newly created string object in the address space of the main app. After that the temporary gets properly released, while the copy lives happily in the main app until it runs out of scope.

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