Re: String conversion/format

Tech-Archive recommends: Speed Up your PC by fixing your registry




"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


.



Relevant Pages

  • Re: String conversion/format
    ... display a char[] value in a label ... The problem is casting ClassName to LPTSTR. ... In .NET world you have just strings:) but in Win32/MFC/ATL there are ... I don't want to confuse you, but your code should be written using TCHAR, ...
    (microsoft.public.vc.language)
  • unicode define question
    ... I'm using TCHAR for all my strings since I understand that TCHAR is written in the include files to take care of a UNICODE define. ... {// its a number char // etc code} ...
    (microsoft.public.vc.mfc)
  • char * to tchar * conversion
    ... How can I convert different type of char to each other? ... code that give me strings in char * format but my program is using tchar ...
    (microsoft.public.vc.mfc)
  • String problem VS 2005
    ... How can I "convert" from a char to a string. ... the console the Classname and the text in the notepad window. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: I am a fresh Visual C++ .NET Developer. Convert char[1024] to System::String
    ... returns a char - which as you can see from the code below I am trying ... to pass to label1->Text - but this expects a String^ ... What data type does className() ... You are adding a character constant zero, ...
    (microsoft.public.dotnet.languages.vc)