Windows Programming basics _ 1 Hello, world

Source: Internet
Author: User
First Project 1) Open Visual Studio and create a project as follows
2) Select an empty project
3) creating a new file does not explain. You will certainly add a CPP file.
4) enter the following code:
#include <windows.h>#pragma comment(linker, "/entry:\"main\" /subsystem:\"windows\"")int main(){MessageBox(NULL, TEXT("Hello,World"),TEXT("NULL"),  MB_OK);}

5) press Ctrl + F5 to run the program. Is there a small window? Is this code familiar? Yes, this is the case. What you see is what is needed for development on Windows. The first line of code explanation is # include <windows. h> Any application on windows must reference some Windows functions or variables, and the declaration of these functions or variables is in windows. h, so we need to include it. This and everyone should include stdio. h. In the second line, we may see that Windows programs start with winmain. However, what do you see here is main? Some people suspect that I have written a mistake. In fact, I have not written a mistake. The so-called main function is a logical concept, that is, to tell the linker where I want to start executing this program, in C (++), the entry function is main and winmain in windows. There is nothing wrong with this function. Of course, we can also set the entry function by ourselves. Here # pragma
In comment (linker, "/entry: \" Main \ "/subsystem: \" WINDOWS \ ""),/entry actually sets the entry function, /subsystem is actually a sub-system (GUI or console). The GUI program does not have a black box. The console program is a black box that you usually see. The first hello, world is basically handed over to it, just as the first Internet access was basically handed over to IE. The third row and the third row are a function call. You must know that, right? This function calls MessageBox, which is an API in windows, which is defined in user32.dll. To help you write programs, I suggest you install a thing called msdn. If you do not know where to download it, you can download the idea from the following URL. Then enter MessageBox in it, and you will see the following description:
Obviously, this document tells you how to use and describe the parameters of MessageBox. I will not explain the specific translation, you know. For details about text, place the mouse over text and press F12. The following description is displayed, which is equivalent to the following definition:

#ifdef UNICODE#define TEXT(queto) L##queto#else#define TEXT(queto) queto#endif

It can be seen literally.
What is Unicode in C language? We can generally write strings in the format of "Hello, world", but not the text ("Hello, world") mentioned above, in Windows, to facilitate the transplantation of the operating system, two types of characters are used, one is multi-byte code and the other is Unicode. If l is added before an ASCII string, then it becomes a unicode string, such as l "ABC" can be just the URL below: http://baike.baidu.com/view/40801.htm
# What is the C language in Windows? # indicates a string connection, for example, "ABC" # "def", and you get "abcdef, many people do not know this estimate. This is especially useful in the implementation of MFC.
What is lptstr? By pressing F12 on lptstr, we can see the following equivalent definitions:

#ifdef UNICODE#define LPTSTRwchar_t*#else#define LPTSTRchar*#endif

Because lptstr indicates a string, as mentioned above, in UNICODE, the string is a unicode string, that is, const wchar_t []. In non-Unicode, the string is a const char *, const char []. The use of lptstr avoids the problem of source code compilation in different environments.
Is MessageBox a function? In fact, MessageBox is not a function. There are two related functions: messageboxa and messageboxw. Determine whether MessageBox is messageboxa or messageboxw based on whether Unicode strings are used, these two APIs are implemented in user32.dll at the same time. Of course, messageboxa will call messageboxw in implementation. You can see the following results through dumpbin-Exports user32.dll:
Dumpbin-exports can be used to see what functions are exported in a DLL. This tool is provided by Visual Studio SDK. After visual sudio is installed, this tool will be automatically installed, you do not need to download it separately.
As I am not so happy today, I will write the first chapter here, and I will try again later. In addition, I am planning to go home for a few weeks after the summer vacation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.