Address: http://blog.csdn.net/huiyouyongdeyu2011/article/details/6556168
Allocconsole function allocates a new console for the main scheduling process.
Syntax C ++: bool winapi allocconsole (void); parameter: no return value: If the function succeeds, the return value is a non-zero value; if the function fails, the return value is a zero value. Note: A process can only be associated with one console. Therefore, this function will fail when the main process already has a console. A process can use the freeconsole function to release the associated console. Then, it can call this function to create a new console or use the attachconsole function to associate it with another console. If a sub-process is created, the sub-process inherits the newly created console. This function initializes standard input, output, and error handle for the new console. A standard input handle is a console input buffer handle. A standard output handle and a standard error handle are console screen buffering handles. To obtain these handles, you can use the getstdhandle function. This function is mainly used for GUI applications. Program Create a console window. There is no console for GUI application initialization, while console applications are initialized on the console. Requirement: Minimum supported client Windows 2000 Professional Minimum supported server Windows 2000 Server header: wincon. H (include windows. h) Library: kernel32.lib DLL kernel32.dll
Bytes ----------------------------------------------------------------------------------------
Although the Win32 era is a graphical interface era, the command line mode, such as batch processing, is also required in occasionally programs. At this time, it seems that it is not so professional to create a graphical interface.
. However, the customer still needs to be in a normal State (for the command line mode, I think the user is not normal, for example, the brain enters the water .) The graphical interface is used. Therefore, we cannot open a console project.
MFC exe program.
OK. Of course, through the condition control, the command line will close the call of the dialog box InterfaceCode, And then use
Cout <"Hello world! "<Endl; to greet the friendly world and insist that the greeting should be displayed in the dark window of CMD.
It is frustrating to say that the facts have been a pain point for me. The cmd window is still dark, and the system ignores my friendliness.
Grandpa Deng said: Reform and Opening are good! Maybe I also need a reform.
Under the guidance of Mr. Zhang, I found a group of Apis: console functions! It is this group of APIs that ultimately allow my friendliness to be expressed locally.
I. Create a console, allocconsole
Directly use allocconsole (); immediately, if this function is called for the first time in the process, an empty cmd window will pop up. Note that only one console can be created for a process, and false is returned for multiple calls. In addition, this window is an independent Console window.
Msdn explanation: A process can be associated with only one console, so
Allocconsole function fails if the calling process already has
Console. Other sections: If the calling process creates a child process, the child
Inherits the new console.
II. Show Hello world, writeconsole
With the console, we also need to get its handle before it can be displayed on it. The method is getstdhandle, which obtains the CMD window handle obtained by allocconsole. If you do not call allocconsole, the standard input/output window handle is obtained.
The getstdhandle function returns a handle for the standard input, standard output, or standard error device.
Handle hdlwrite = getstdhandle (std_output_handle); // you can also use std_error_handle here
Tchar C [] = {"Hello world! "};
Writeconsole (hdlwrite, C, sizeof (C), null, null );
Use writeconsole directly after obtaining the console handle
The function is displayed on the screen. This function has five parameters: the first is the console handle, the second is the write content address, the third parameter is the expected write length, and the fourth parameter is actually written.
Length. It can be null, but it is not recommended. If null is used, boundchecker will prompt incorrect usage here. The fifth parameter is retained and must be null.
In msdn, writefile can also be used to output handle to the console.
3. Get user input, readconsole
The light display is not enough. You still need to operate it. Of course, it is a keyboard input.
We have already created a console, and the input also needs to use this console. However, if the above output handle is used, we will find that the program will skip this sentence below.
Readconsole (hdlwrite, C, 2, null, null );
Here, we also need a std_input_handle; as follows:
Tchar buffer [100]; // cache Enabled
Memset (buffer, 0,100 );
DWORD dwcount = 0; // number of inputs
Handle hdlread = getstdhandle (std_input_handle );
Readconsole (hdlread, buffer, 100, & dwcount, null );
This function is similar to writeconsole, but note that the fourth parameter must be specified. Otherwise, the function cannot be read.
As mentioned in msdn, to obtain input information outside the keyboard, such as mouse information, you can only use the readconsoleinput function:
If the input buffer contains input events other than Keyboard Events
(Such as mouse events or window-resizing events), they are discarded.
Those events can only be read by using the readconsoleinput function.
4. Close Console
Remember to use closehandle to release the handle when using it, for example:
Closehandle (hdlread );
Closehandle (hdlwrite );
If you want to close the console, you can use freeconsole (). Note that if you directly click the close button in the CMD window, the entire application will exit!
5. Other functions
This set of functions also provides other interesting functions, such as modifying the background color and foreground color of the console. For more functions, see msdn.
allocconsole ();
setconsoletitle ("lonefox love China "); // modify the title of the console
handle hconsole = createconsolescreenbuffer (
generic_read | generic_write, // permission
file_assist_read | file_assist_write, // console sharing method
null, // security setting, null default
lele_textmode_buffer, // unique value
null // reserved
); // create the console's screen content cache. A process can have multiple screenbuffer
setconsoleactivescreenbuffer (hconsole); // display this Buffer content
setconsoletextattribute (hconsole,
foreground_red | background_green);
tchar C [] = {"Hello world! /N "};< br> writeconsole (hconsole, C, sizeof (C), null, null); // displayed on the screen
closehandle (hconsole );
freeconsole ();
Postscript: This article only takes notes and is not a textbook. If you have any questions, please leave a message for discussion!
--------------------------------------------------------------------------------
The simple method is:
If (allocconsole ())
{
Freopen ("conout $", "W", stdout );
Printf ("allocconsole [OK] \ n ");
}
You can redirect the standard output to facilitate GUI program debugging.
--------------------------------------------------------------------------------
This article is complete.