Re: String conversion/format
- From: "Giovanni Dicanio" <giovanni.dicanio@xxxxxxxxxx>
- Date: Sat, 15 Sep 2007 12:09:53 +0200
"SQACPP" <lsdisciples@xxxxxxxxxxx> ha scritto nel messaggio
news:1189845809.508982.117880@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks! It work now to display the handle.... Now i'm trying to
display a char[] value in a label (or a TextBox)
HWND hNotepad;
hNotepad = FindWindow(L"Notepad", NULL);
static char ClassName[51];
GetClassName(hNotepad,(LPTSTR) ClassName, 50 );
Hi,
the above code is not correct.
The problem is casting ClassName to LPTSTR.
LPTSTR is "TCHAR *", i.e. a string of TCHARs.
ClassName is instead a "char *" (a string of char's), i.e. a LPSTR (no "T").
Another string type in Windows programming is "wchar_t *", i.e. LPWSTR ("W"
in place of "T").
In .NET world you have just strings :) but in Win32/MFC/ATL there are
*several* string types.
I don't want to confuse you, but your code should be written using TCHAR,
i.e.:
<code>
// Make enough room for class name
static const int ClassNameCount = 200;
// Buffer for class name
// (Note TCHAR and not char)
static TCHAR ClassName[ ClassNameCount ];
// Get the class name
::GetClassName(hNotepad, ClassName, ClassNameCount );
</code>
or, IMHO, better, using explicit Unicode strings (i.e. wchar_t strings, like
strings prefixed by the L, like your L"Notepad"):
<code>
// Make enough room for class name
static const int ClassNameCount = 200;
// Buffer for class name
// We use Unicode (UTF-16) --> wchar_t
static wchar_t ClassName[ ClassNameCount ];
// Get the class name
// (use the explicit Unicode - W in tail - version)
::GetClassNameW(hNotepad, ClassName, ClassNameCount );
</code>
Now you should have the correct Notepad and not just "N".
The issue about char/wchar_t/TCHAR is that:
- char can be used to store ANSI strings (or Unicode UTF-8 strings)
- wchar_t can be used to store Unicode UTF-16 strings (i.e. the native
Windows and C#/.NET string format)
- TCHAR is expanded as char or wchar_t, basing on some C preprocessor flags
(like _UNICODE and UNICODE)
If you have a char* string, you cannot convert it to TCHAR* just with a type
cast (like LPCTSTR).
You need to use some conversion technique, like proper CString constructor,
or ATL conversion helper like CA2CT, etc.
But you can just use TCHAR instead of char, and you don't need explicit
conversion in this context.
Or better IMHO, you can use explicit Unicode UTF-16 strings, i.e. use
wchar_t.
So, why did "N" appeared and not "NOTEPAD"?
I answered a similar question here
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2058281&SiteID=1
you might find my post (it should be the 5th) in that thread to be a little
bit interesting.
Giovanni
.
- Follow-Ups:
- Re: String conversion/format
- From: SQACPP
- Re: String conversion/format
- References:
- Re: String conversion/format
- From: SQACPP
- Re: String conversion/format
- From: Kim Gräsman
- Re: String conversion/format
- From: SQACPP
- Re: String conversion/format
- Prev by Date: Re: String conversion/format
- Next by Date: Re: String conversion/format
- Previous by thread: Re: String conversion/format
- Next by thread: Re: String conversion/format
- Index(es):
Relevant Pages
|