Click here to Skip to main content
13,053,276 members (60,439 online)
Rate this:
Please Sign up or sign in to vote.

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:
String* args=...;
char* str = (char*)(void*)Marshal::StringToHGlobalAnsi(args);
bool ret = wrapped->A(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*);

Posted 18-Oct-11 6:40am
Updated 18-Oct-11 14:55pm
SAKryukov 18-Oct-11 20:55pm
Fixed HTML, code formatting.
SAKryukov 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...
johny10151981 18-Oct-11 22:04pm
AA function returns character pointer, If unmanaged C++ support it then do it like this
char *retarray;

SAKryukov 19-Oct-11 1:31am
So, what's the problem?
johny10151981 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 19 Oct 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100