A question about marshaling (String to CString)

  • Thread starter Thread starter mirek
  • Start date Start date
M

mirek

Hi,
I'm trying to import my old code to the .NET using managed wrappers.
I've read "Managed Extensions for C++ Migration Guide" document and was
trying to do what it stated in it. For example if my function takes
mfc's CString I was converting it this way:
char __pin* szUser =
static_cast<char*>(Marshal::StringToHGlobalAnsi(sUser).ToPointer());

and pass szUser to the function.

Today I've found that I can easily pass *String to the function that
takes CString and it works! I'm now little confused, could someone
explain it? When I have to marshal arguments, when i don't? I thought I
have to marshal all arguments.

The second question: what about marshaling and return values? I mean
marshaling value from the return statement in unmanaged function.

Regards,
mirek
 
Sorry, it seems that this doesn't work reliable. Sometimes it works
(proper string is passed to the function) sometimes doesn't :)
But I though it won't even compile.

Regards,
mirek
 
CString has a constructor that takes a String and therefore String to
CString conversions are done automatically for you. And String has
constructors that respectively take a __wchar_t* and a char*; and since
CString has an implicit cast to LPCTSTR, CString to String conversions are
almost always done automatically too.
 
Back
Top