wrapping unmanaged class convert MFC CString

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.
 
S

sklett

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);
 
C

Carl Daniel [VC++ MVP]

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
 
S

sklett

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top