WINDOWS C language network programming quick start

xiaoxiao2021-03-06  19

C language learning, the general way is, first learn C, then C , it is best to have assembly language and microcomputer principle base, and then Visual C . In this way, spend a lot of time and endurance for learners. In the school teaching, there is no time to learn the practical technology of Windows programming.

In fact, after the C language basis, there are some basic C classes of the C class, you can learn about Windows C directly.

First, walk near Windows C

Many languages ​​make a "Hello, World!" As the first entry program, the first program in the C language is this:

#include main () {printf ("Hello, World!");

If the main function writes the main function as a parameter, it should be:

#include main (int Arge, char * argv []) {printf ("Hello, World!");

The first program of Windows C and this program are consistent in form and principles, just two points:

1. The main function received by the main function is not just the number of strings in the command line and the first address of the string.

2. Many functions of the C language can continue to use in Windows C, but the functions such as the Printf () screen display cannot continue to use. Because Windows is a multi-task operating system, the screen is no longer unique to a certain application, the Windows C application is displayed, and the API function provided by Windows is required to open your own window.

Here is the simplest, display "Hello, World!":

#include APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {MessageBox (NULL,, "the first Windows C program", MB_OK | MB_ICONASTERISK "Hello, World!");}

The main function is four:

1) Hinstance: Handle of the current instance of the program is run; 2) HPRIVINSTANCE: The handle of the previous instance; 3) LPCMDLINE: Program command line pointer; 4) ncmdshow: An integer used to specify the window display mode.

The use of these parameters will be introduced in in-depth learning.

Show Hello, Word! String, we use a MessageBox function that displays a dialog on the screen, its prototype is:

INT MessageBox (HWND HWND, LPCTSTR LPTEXT, LPCTSTR LPCAPTION, Unit Utype)

The four parameters are:

1) HWND: The handle of the parent window; 2) LPText: The pointer to the string; 3) LPCAPTION: Dialog box header string pointer; 4) uType: The type of small icon on the dialog box.

Use this function to include Windows.h header files.

Debug it, how? A "first Windows C program" dialog is popped up, there is a line above: "Hello, World!".

The world is really beautiful! !

In-depth programming:

In the C language, the function is declared, if the return value type is not specified, the default value is Void, and the main function of this program has no return value. However, in Windows programming, we'd better develop a good habit, indicate the return value type of the function, because in C , the function return value type is not default. And we will use some of the concepts of C when Windows C programming, so that it is conducive to learning in depth. The program is like this:

#include int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {MessageBox (NULL,, "the first Windows C program", MB_OK | MB_ICONASTERISK "Hello, World!"); return }

Here, we declare the type of INT, and return a value of 0, such a function can be used in a complex function call.

In this section, we have some of the concepts, handles, and pointers of the handle, and a value in the internal index table in the operating system, which prevents the application directly from accessing the object. The internal structure reflects the superiority of Windows resource management. For example, after a window is found, there should be a memory block in memory. The memory fast address in this window tends to be dynamically adjusted by the operating system, but it will not change. However, through it can access this window, so when you use it, you can treat it as a pointer.

转载请注明原文地址:https://www.9cbs.com/read-42720.html

New Post(0)