Re: Retrieving text from CEdit box as char *?

From: Darren Beckley (darrenbeckley_at_nospamhotmail.com)
Date: 05/22/04


Date: Sat, 22 May 2004 10:45:43 +0100

Windows CE uses Unicode so strings coming from the OS such as window text
are stored using "wide" characters (2 bytes instead of 1).

If you really need a string of char, you need to convert the CString to
ASCII/ANSI using functions such as WideCharToMultiByte() or wcstombs(). You
may also like to look at MFC Tech Note TN059 which describes some useful
macros such as T2A() that can simplify your code if you do a lot of
conversions.

Alternatively, if you don't actually need the string in ASCII format, just
change char* text_input to LPCTSTR text_input. This gives you a const TCHAR
pointer to the CString buffer.

TCHAR is a generic character type that is either WCHAR or char depending on
whether you build for Unicode or not (look at the pre-processor flags in
your project, you will see something like UNICODE or _UNICODE defined in
eVC++ projects). Most C library functions have a generic function equivelent
that is controlled by this flag. e.g. _tprintf() becomes either wprintf() or
printf().

Hope that helps,
Darren

"Mike Hughes" <anonymous@discussions.microsoft.com> wrote in message
news:7401D145-3045-4F61-8CD0-85270ECA97FF@microsoft.com...
> Hi,
>
> I'm having some trouble with data conversions. I have a edit box that the
user enters information into and then I retrieve the data with either the
GetWindowText() or GetLine() functions. This leaves me with a CString that
I can't seem to do anything with.
>
> I tried the follow code to convert the CString to a char * but it ends up
with the char * only having the first element from the CString in it.
>
> char *text_input;
> CString temp_string;
>
> EditBox->GetWindowText(temp_string);
> text_input = (char *) temp_string.GetBuffer(sizeof(temp_string));
>
> .....
>
> temp_string.ReleaseBuffer();
>
> I also tried to do a straight strcpy of the temp_string into the
text_input (with space allocated for the text_input) but it causes a
compiler error (error C2664: 'strcpy' : cannot convert parameter 2 from
'class CString' to 'const char *').
>
> I'm using eVc++ 4.0 targetted at an ARMV4 device.
>
> Thanks,
>



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: ANSI oder nicht ANSI?
    ... Wenn dabei aber der String von Unicode auf Ansi gewandelt wird, damit die Funktion ablaufen kann, dann kann diese Umwandlung eine unerwartete Auswirkung haben. ... Die Basisfunktionen arbeiten mit 1 bzw. 2 Byte großen "Elementen", ohne sich darum zu kümmern, ob diese Elemente nun Zeichen, Teile von Zeichen oder ganz andere Informationen enthalten. ... Dann ist es auch nicht generell möglich, 1 Zeichen in einer Variablen vom Typ Char abzulegen, weil manche Zeichen eben länger sein können als nur ein Element vom Datentyp Char. ...
    (de.comp.lang.delphi.misc)
  • Re: CreateFile pathname
    ... >Instead of passing a char[] as parameter 1, ... Microsoft uses arrays of WCHAR to store UNICODE characters, ... or how to convert a string into the write format. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Searching for byte sequence
    ... you have to decide if you are ever going to support Unicode. ... You can use CString, but in that case, you should add ... for a string? ... would read a header and search it, then if nothing is found, skip the 10K ...
    (microsoft.public.vc.mfc)
  • Re: Conversion of byte array to cstring
    ... "feature" is that there is a CString constructor that will accept an 8-bit ... string and create an initialized CString, even if the CString is actually ... This ctor converts from ANSI to Unicode, so, even if you are ... // Point to next WCHAR in source array ...
    (microsoft.public.vc.mfc)

Loading