Passing values from unmanaged code to managed code in C++/CLI  
Author Message
Ludmila Koganer





PostPosted: Visual C++ Language, Passing values from unmanaged code to managed code in C++/CLI Top

Hi,

I would appreciate if someone could provide pointers / example on passing char* value from a unmanaged C++ code to a managed string object .

I am able to pass a string object to char* from managed to unmanged using MarshallToHGlobalANSI but I am looking for the reverse.

Thanks,

Ludmila



Visual C++4  
 
 
Mike Danes





PostPosted: Visual C++ Language, Passing values from unmanaged code to managed code in C++/CLI Top

The String class has a constructor that takes a char * parameter:

char *foo = "Hello";

Console::WriteLine(gcnew String(foo));


 
 
Brian Kramer





PostPosted: Visual C++ Language, Passing values from unmanaged code to managed code in C++/CLI Top

To be obnoxiously pedantic:

const char* foo = "Hello";

is a better choice.  It prevents any writes to foo, which would lead to a memory exception, within the enclosing function.  (The System::String constructor however won't write to foo because the argument isn't marked as an out parameter.)