Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Unmanaged Managed
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:
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 18-Oct-11 7:40am
luke2141
Edited 18-Oct-11 15:55pm
v2
Comments
SAKryukov at 18-Oct-11 20:55pm
   
Fixed HTML, code formatting.
--SA
SAKryukov at 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
johny10151981 at 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);
SAKryukov at 19-Oct-11 1:31am
   
So, what's the problem?
--SA
johny10151981 at 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
good
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?
  Permalink  
Comments
Stefan_Lang at 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
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 19 Oct 2011
Copyright © CodeProject, 1999-2014
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