Re: Unicode question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



You are right about the null termination being a problem, I usually use the following when interfacing with Win32:

std::vector<char> text;
text.resize(length+1);
::SendMessageA(hwnd, WM_GETTEXT, static_cast<WPARAM>(length+1), reinterpret_cast<LPARAM>(&text[0]));
std::string string;
string.assign(&text[0], length);

I prefer to have as much of my code to be as portable as possible, hence my preference for std::string; I prefer the MVC way of doing things where you separate "model" and "gui" specific code, "gui" specefic e.g. MFC dialogs are where I employ CString, otherwise I use std::string as much as possible.

I prefer an explicit call to c_str() rather than relying on ugly conversion operators and it is up to the implementation as to whether c_str() returns a copy or not, in VC++ it does not return a copy I believe.

/Leigh


"Giovanni Dicanio" <giovanniDOTdicanio@xxxxxxxxxxxxxxxxx> wrote in message news:uZ9QWimfKHA.2184@xxxxxxxxxxxxxxxxxxxxxxx
"Leigh Johnston" <leigh@xxxxxxxxx> ha scritto nel messaggio news:OjZscOmfKHA.2188@xxxxxxxxxxxxxxxxxxxxxxx

2. Pass a pointer to internal character buffer to Windows API function.

Trivial to do with std::string.. resize(...) and &s[0]

Are std::string's always NUL-terminated?
My understanding is that calling c_str() always returns a NUL-terminated string, but I'm not sure about &s[0] (assuming 's' being an instance of std::[w]string).

Moreover, when calling Win32 APIs that have LPCTSTR parameters, we can pass a CString instance and implicit LPCTSTR conversion operator is called, instead with std::[w]string it is required an additional call to .c_str() method.
Moreover, does the .c_str() method return a *copy* of the string data? This would cause inefficiencies...

And CString is reference counted, so using this class could save useless deep-copies (optimizing both in space and time) that instead can happen with std::[w]string.


3. Trim a string from right and/or left.

Trivial to do with find/substr

Is it as trivial as CString::Trim/TrimeLeft/TrimRight?
I don't think so.


4. Tokenize a string.

Writing a tokenizer is trivial, there is also boost

CString does it out-of-the-box.
No need for custom code.


All the above mentioned tasks are quite easy with CString and hard with std::string.

Wrong.

I agree with Alex.
CString interface is more convenient than std::string, especially in contest of Win32 programming.

The benefit of STL's string class is portability of C++ code, but when you #include <windows.h> you've already left the kingdom of portable C++ code.

Giovanni


.



Relevant Pages

  • Re: peer code review/advice needed for noob programmer
    ... There are lots of easy ways to transition from char to CString; ... Yes, except I am having to program to Microsoft FS's API, and the string is ... if any reason to allocate a buffer here. ...
    (microsoft.public.vc.mfc)
  • Re: CSocket/CAsyncSocket sending and receiving
    ... Stop experimenting with CSocket; it is losing. ... CString constructor will do a MultiByteToWideChar to convert the string. ... sequence of Receives will receive N bytes. ...
    (microsoft.public.vc.mfc)
  • Re: CFileDialog drives me insane. Handle Problem ?
    ... The basic type you care about is CString. ... This is the equivalent to the Java 'String' ... There are rare cases in which you need a LPTSTR pointer, ... const wchar_t * - const pointer to Unicode characters. ...
    (microsoft.public.vc.mfc)
  • Re: FormatMessage LoadString from string table
    ... CString str2items ("here should be the first item %1, ...  So make this formatting string the first string ...  And if you need to have an array of 40 strings, well, ... Take a look at my FormatMessage example, or look at the numerous calls on FormatMessage in ...
    (microsoft.public.vc.mfc)
  • Re: CFileDialog drives me insane. Handle Problem ?
    ... The basic type you care about is CString. ... This is the equivalent to the Java 'String' ... There are rare cases in which you need a LPTSTR pointer, ... const wchar_t * - const pointer to Unicode characters. ...
    (microsoft.public.vc.mfc)