In the first chapter, we understand a fact that the so-called API is not a mysterious thing. For programmers, its meaning is the last one in these three letters, Interface (interface), in the slogan is just A library, then in fact, we are learning how to use this library, just like we have learned STL, starting from the simplest application, this is what I think when you start learning a library. Thought should be established.
At the end of the first chapter, the author shows the first Windows program (even if I doubt that it is a Windows program), I have changed it for the description, the code is as follows:
#include
Int WinApi Winmain (Hinstance Hinstance,
Hinstance Hprevinstance,
PSTR SZCMDLINE,
Int icmdshow)
{
Char * Hello = "Hello, Windows 98!";
Char * title = "Hellomsg";
MessageBox (Null, Hello, Title, 0);
Return 0;
}
The author explained a detailed explanation of this program, but I still don't understand, the second and third parameters of Messagebox () are PCSTR. What type is this? According to the author's tips, I viewed this header file. It turns out that this type is const char *, but at the same time, this damn brain has an issue: here is almost TypedEf for each type, why So trouble? I have entered the second chapter with such a problem.
Oh, the original Windows pre-processes each character type in order to support the two character environments of ASCII and Unicode:
#ifdef unicode
Typedef wchar tchar, * ptchar;
Typed, PTCH, PTSTR, LPTSTR
Typedef lpcwstr lpctstr;
#ELSE
Typedef char tchar, * ptchar;
Typedef LPSTR LPTCH, PTCH, PTSTR, LPTSTR
Typedef lpcstr lpctstr;
#ndif}
For the sake of unified format, all types are changed to uppercase (why not don't know? Don't know, don't you go with me?), This little thing is to change:
#include
#include
Int WinApi Winmain (Hinstance Hinstance,
Hinstance Hprevinstance,
PTSTR SZCMDLINE,
Int icmdshow)
{
PTSTR Hello = "Hello, Windows 98!";
PTSTR title = "Hellomsg";
MessageBox (Null, Hello, Title, 0);
Return 0;
}
The above code is almost very good, there is nothing wrong, running is everything, but don't forget that in general, our character environment is ASCII, change it to Unicode, huh, the compiler did not give your face? The problem is assigned, as the Unicode said in C is supported by Wchar_T, and the Wchar_T string assignment mode is: wchar_t * p = l "Hello!";
Obviously in the above procedure, when PTSTR was preprocessed into WCHAR_T, the assignment method did not change, and the type did not match, fortunately, in tchar.h file, provide us with a macro function: Text x); (For the definition of this function, see the same book, thought and PTCSTR). So the code above should write this way:
#include
#include
Int WinApi _twinmain (Hinstance Hinstance,
Hinstance Hprevinstance,
PTSTR SZCMDLINE,
Int icmdshow)
{
PTSTR Hello = Text ("Hello, Windows 98!");
PTSTR Title = Text ("Hellomsg");
MessageBox (Null, Hello, Title, 0);
Return 0;
}
The careful people will find that in front of Winmain () also wrote _T, in fact, the same is the same as Text, in order to support two character environments.
But so far, we didn't see the meaning of this, and the big strength is almost a bit of self-satisfaction. Look at this chapter says so much (even the primitive people), find still valuable, not Nance (this sentence is definitely nonsense), Unicode is for internationalization, that is, the difference between him and ASCII is mainly supporting the text of non-English countries. We are all told that a Chinese character is equal to two English characters, is this in the Unicode character environment? I wrote something to test, the code is as follows:
#include
#include
#include
Using namespace std;
#ifdef unicode
Typedef wstring astring;
Typedef wostringstream ostringstream;
#ELSE
TYPEDEF STRING ASTRING;
Typedef ostringstream ostringstream;
#ENDIF
Int WinApi _twinmain (Hinstance Hinstance,
Hinstance Hprevinstance,
PTSTR LPCMDLINE,
INT ncmdshow)
{
PTSTR str = text ("Hello");
PTSTR Message = Text ("Hello");
INT A = Lstrlen (STR);
Ostringstream Out_STR;
OUT_STR << "this is" << a << endl;
Astring s = out_str.str ();
PCTSTR P = S.c_Str (); MessageBox (NULL, P, STR, MB_OK);
Return 0;
}
Output "THIS 4" in the ASCII environment. This is what we are familiar with, "this is 2" is output under Unicode, so we can see the changes brought by Unicode. Yes, he is exist for the internationalization of Windows, which makes our computer represent more information.
I seem to have heard you said, "Oh, you used C !", Yes, why not? The Windows API is an operating system library function. He is a library of C, then there is no reason to say that C , which is a super collection of C, cannot use him. And I don't have a good sense for that sprintf, huh, huh.