[C++]cstring to lpstr

Started by
4 comments, last by nobodynews 16 years, 9 months ago
Hi, I want to copy a CString to the Clipboard, apparently I need to convert the CString to LPSTR. How do I do this?
Advertisement
After roughly 8 seconds on Google:
Using CString
How to convert from CString to LPSTR?

Impressive how these must have been posted within the last minutes, since you couldn't find them when you tried to find an answer, because of course you tried a simple [google] search before posting here. [wink]
Ofcourse I search before I post... I tried google, searched for like 30min
the method in the 2nd link:
[code
LPSTR OriginChar= theCString.GetBuffer(theCString.GetLength());


doesn't work: error C2440: 'initializing' : cannot convert from 'wchar_t *' to 'LPSTR'

And the first link... can't find any usefull information...
umm, if your using wchar_t's try setting this option in visual studio:

right click on your project and go to the project properties, then in the general section you should see a box for character set, change that to not set.

It will make your program not use wide characters, so you cant have your app in japanses or other funky languages, but it should work.

Alternativly, you need to find the wide character version of the function that needs a LPSTR.

BTW, LPSTR is just fancy for (char *). LPWSTR is what you have with your wchar_t's.

Hope that helps.

Hi, thanks for the reply.

I tried what you suggested. The error doesn't come up, but there is another error:

1>mt.exe : general error c101008a: Failed to save the updated manifest to the file ".\Debug\statmodv1.exe.embed.manifest". The parameter is incorrect.

Maybe there is something wrong with my code...
LPSTR lpCmdLine = temp.GetBuffer(0); //temp is the string that I want to copy to clipboard	  HGLOBAL      temp_Handle ;      char*         temp_ptr ;  OpenClipboard();         temp_Handle =  GlobalAlloc (GMEM_MOVEABLE + GMEM_DDESHARE,                                  strlen(lpCmdLine) + 1 );  temp_ptr = (char*)GlobalLock(temp_Handle);  memcpy (temp_ptr, lpCmdLine, strlen(lpCmdLine)+1);  GlobalUnlock(temp_Handle);  EmptyClipboard();  SetClipboardData(CF_TEXT, temp_Handle);  CloseClipboard();

I googled that error code and found this link. I think the solution was to just clean your solution (Build->Clean Solution). Hope that helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement