Question

I'm building an MFC C++ application for one of my uni projects, and unfortunately MFC only allows you to use the SetWindowTextW() method, with a parameter of LPCTSTR? not a string or CString, and for the life of me I cannot find out how to convert my string to an LPCSTR value.

My code is:

pBorrowedBookCEdit1->SetWindowTextW(/* INSERT LPCTSTR HERE */);

The String comes from a completely separate object.

If there is another way of passing text to a CEdit object in MFC then please let me know =)

Any help would be greatly appreciated.

Cheers.

Was it helpful?

Solution

Assuming that you have a std::string on hand, which is vaguely indicated by your question, and assuming further that you're building a Unicode app, also vaguely indicated, then you can probably do

CString mfc_string( s.c_str() );
// Use mfc_string here.

where s is the std::string.

The CString constructors are documented in MSDN Library.

It's often a good idea to consult the documentation.


Background: LPCTSTR is a macro that expands to either char const* or wchar_t const* depending on whether the symbol UNICODE was defined when <windows.h> was included. It's a compatibility thing, for writing code that might work, with some drastically reduced functionality and reliability, in Windows 9x. Unless you're targeting Windows 9x, forget it, don't use the T macro stuff.

The T macros became obsolete already in the year 2000, when Microsoft introduced the Layer for Unicode which lets Unicode apps work in Windows 9x (with only slightly reduced functionality).

That they're still used for non-legacy code in 2014, long after the demise of the target platform, is, IMHO, a testimony to the extreme power and self-sustainability of conformity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top