szAppName

Pages: 123
Hi, could you give me some information on the following code:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)

{
static TCHAR szAppName[] = TEXT("Skeleton");

....more code....

}

And then later when defining our window attributes its got:

wndclass.lpszClassName = szAppName;

I have a rough idea of whats going on here, but id like to hear what exactly is happening from someone who knows what im doing here, in detail please, including what the "static" keyword does. thanks.

Last edited on
 
static TCHAR szAppName[] = TEXT("Skeleton");



In this case, static doesn't really do anything (it limits the scope of a function or variable to the current compile, which doesn't matter here cause it's a local variable anyways. Also, it would prevent reinitialization of the variable, which doesn't matter either because you only assign a value to it once).

TCHAR is just a microsoft definition for char.

The TEXT() macro does, well, nothing much either. In ASCII mode, it resolves to just the string, and in Unicode mode it resolves to L to indicate the string is using wide chars.

the last line, wndclass.lpszClassName = szAppName; pretty much just sets the name of the class (lpsz just means "long pointer to zero terminated string), which windows uses to identify it.
Thanks, do you know of any website which has a tutorial which talks you through this, because i dont quite understand some parts still, :)
Hm, I don't know, I learned with a book. It helped me to realize that most of those cryptic capsed WINDOWS stuff was actually just macros that were there for compatibility reasons und usually just resolve to standard datatypes, or in many cases even nothing.
Last edited on
TCHAR is just a microsoft definition for char.


Not really. It's a char or a wchar_t depending on whether or not it's a unicode build.

To be Unicode friendly, you should use TCHAR or wchar_t.
If you consider this to be beginner stuff can't wait till you consider yourself advanced :).
You'll learn on this site that the whole "beginner", "Windows" or "General C++ Programming" doesn't really effect where people post their questions :p.

Also Win32 API programming isn't too difficult if you dance around using COM objects, there's just more ground work to put down before you get rolling and typedef gets pretty much abused.
Thanks, i have a few questions :)

1. So does the keyword static make a function/variable not callable outside the file it is defined in, which means that if i #included it into another file i would not be able to use the static functions/variables. Correct?

2. "TCHAR is just a microsoft definition for char". By this do you mean TCHAR is just a typedef for char?

3. The TCHAR definition is wrote like this: "typedef WCHAR TCHAR, *PTCHAR;", does this mean that TCHAR is a typedef of WCHAR?

4. The definition of WCHAR is: "typedef wchar_t WCHAR;", does this mean WCHAR is a typedef of wchar_t?

5. So what is wchar_t because it shows up as a keyword in blue and not a type?
Last edited on
Oh, I might have worded this incorrectly:

TCHAR is a macro of microsoft that resolves to either wchar_t or char, depending whether or not the UNICODE macro is defined (I said it resolves to char, because in most cases -the ones you probably care about right now- it does). In the header files, it will probably look a little something like this:
1
2
3
4
5
6
7
8
9
10
11
typedef char CHAR, *PSTR, *LPSTR;
typedef wchar_t WCHAR, *PWSTR, *LPWSTR;
#ifdef UNICODE
     typedef WCHAR TCHAR;
     typedef PWSTR PTSTR;
     typedef LPWSTR LPTSTR;
#else
     typedef CHAR TCHAR;
     typedef PSTR PTSTR;
     typedef LPSTR LPTSTR;
#endif //UNICODE 

-- there is a bunch of other macros and typedefs like that, but basically they're all just there so you have to worry less about whether you are dealing with Unicode or ASCII/ANSI characters.

as to wchar_t - it's a C/C++ char type for wide characters. How wide "wide" is however is compiler dependent.
Last edited on
So does the keyword static make a function/variable not callable outside the file it is defined in, which means that if i #included it into another file i would not be able to use the static functions/variables.
static can be used in two ways. When used on class members, it mean there's only one of those members per class which is shared for all instances. For example, if you wanted to indicate a maximum size for your container you could define a static method max_size(), as all instances of your container would respect this maximum.

A static variable or function outside of a class is only known within that compiled module. If you had two different files that used two distinct function GetName in their respective files, these can be declared static to avoid a name clash.

"TCHAR is just a microsoft definition for char". By this do you mean TCHAR is just a typedef for char?
Not quite. TCHAR is a Windows definition for a char. A char may be encoded as ASCII or Unicode-16.

So what is wchar_t because it shows up as a keyword in blue and not a type?
wchar_t is a "wide" (16 bit) character.

You really need to stop what you're doing and read about the C++ Language and how to use it with the Platform SDK. Your questions are only touching on the issues and you can't possibly understand what's going on from what you've asked.
Last edited on
Thanks, i guess i am asking questions which i do not need to know, for now anyway...

To make things simpler for me, could i just put:

wndclass.lpszClassName = ClsName;

without putting "static TCHAR szAppName[] = TEXT("Skeleton");"
It depends where ClsName is declared.
I don't know what clsName in this context is, but if you were sure that your program is never going to deal with wide characters, writing char* appName = "Skeleton"; instead of static TCHAR szAppName[] = TEXT("Skeleton"); isn't going to change anything.

Besides: Can anyone enlighten me what the static there would do? I can't imagine it doing anything in normal circumstances, but maybe the pros here can think of a situation where it would make a difference.
Ok, i asked that question because if you look here: http://www.functionx.com/win32/Lesson01c.htm

And scroll down you'll see a complete example how a window is made, and you'll also see that he doesnt use:

"char* appName = "Skeleton";"

or

"static TCHAR szAppName[] = TEXT"

He just goes ahead and gives ClsName to wndclass.lpszClassName =
Last edited on
Yeah, that's fine, it's global.
Last edited on
Im having another go at understanding "static TCHAR szAppName[] = TEXT("Skeleton");"

I know what the static keyword does.

TCHAR can be either char or WCHAR based on the platform. WCHAR is always a 16-bit Unicode character, wchar_t. So its best to use TCHAR to support both, right?

szAppName is just the name of this variable?

We use [] after the variable name because it is a char with more than one character (an array), right?

Could you explain to me why we used = TEXT("Skeleton") instead of = "skeleton"?

Thanks.



closed account (z05DSL3A)
Could you explain to me why we used = TEXT("Skeleton") instead of = "skeleton"?

TEXT() is another macro to make sure that the string literal type matches the the type that TCHAR ends up being.
Thanks, also, why have they made it so we have to store the window class name in a variable?
Instead of passing the name directly?
Pages: 123