wrapping unmanaged class convert MFC CString

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I have dealt with converting managed C++ String* to PCSTR using the Marshal
class, but now I need to get managed C++ String* converted to MFC CString.
I can't seem to figure out how this is performed?

Is it possible? Any help would be greatly appreciated, thank you.
 
Hmm, so that is what I ended up doing but was sure there was a prettier way
;) Thanks!

char buffer[512];

char* pCheckoutby = (char*)(void*)Marshal::StringToHGlobalAnsi(checkoutby);

strcpy(buffer, pCheckoutby);



CString str(buffer);
 
sklett said:
Hmm, so that is what I ended up doing but was sure there was a prettier
way
;) Thanks!

char buffer[512];

char* pCheckoutby =
(char*)(void*)Marshal::StringToHGlobalAnsi(checkoutby);

strcpy(buffer, pCheckoutby);



CString str(buffer);

What's wrong with

char* psz = ((char*)Marshal::StringToHGlobalAnsi(checkoutby);
CString str(psz);
Marshal::FreeCoTaskMem(psz); // don't leak!

?

-cd
 
Nothing, I was over complicating the whole thing, it's actually pretty clean
I guess.
I'm flip-floppin'


Carl Daniel said:
sklett said:
Hmm, so that is what I ended up doing but was sure there was a prettier
way
;) Thanks!

char buffer[512];

char* pCheckoutby =
(char*)(void*)Marshal::StringToHGlobalAnsi(checkoutby);

strcpy(buffer, pCheckoutby);



CString str(buffer);

What's wrong with

char* psz = ((char*)Marshal::StringToHGlobalAnsi(checkoutby);
CString str(psz);
Marshal::FreeCoTaskMem(psz); // don't leak!

?

-cd
 
Back
Top