In MEC++ I could just call a function that was expecting a String __gc* even
though I had a native char *. The char * automatically got marshaled to a
String __gc*.
In the May CTP of VS 2005 I just get a compile-time error: cannot convert
parameter 1 from 'char *' to 'System::String ^'
What would be the normal C++/CLI way to handle this? And can I do it that
way in the May CTP?
I thought it might be the marshal_as template but I couldn't really find it
or enough information about it.
Bern McCarty
Bentley Systems, Inc.
Steve McLellan - 22 Jun 2004 18:12 GMT
> In MEC++ I could just call a function that was expecting a String __gc* even
> though I had a native char *. The char * automatically got marshaled to a
[quoted text clipped - 5 lines]
> What would be the normal C++/CLI way to handle this? And can I do it that
> way in the May CTP?
I use:
System::String * DotNetStringFromSTLString( const std::string &string_ )
{
return Marshal::PtrToStringAnsi((int*) string_.c_str());
}
which works, but for all I know is leaking like a swimming pool full of
porcupines.
Nishant S - 22 Jun 2004 19:30 GMT
Hi Steve
There is no leak, since PtrToStringAnsi creates a new String object and
copies the source string into it. The managed String object will get GC'd
eventually.

Signature
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
> > In MEC++ I could just call a function that was expecting a String __gc*
> even
[quoted text clipped - 16 lines]
> which works, but for all I know is leaking like a swimming pool full of
> porcupines.
Nishant S - 22 Jun 2004 19:27 GMT
Do this :-
ref class R
{
public:
void Test(String^ s)
{
}
};
void _tmain()
{
R^ r = gcnew R();
char* s = "hello world";
r->Test(gcnew String(s));
}

Signature
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
> In MEC++ I could just call a function that was expecting a String __gc* even
> though I had a native char *. The char * automatically got marshaled to a
[quoted text clipped - 11 lines]
> Bern McCarty
> Bentley Systems, Inc.