How to Use the console for an MFC Application

Source: Internet
Author: User
Tags readfile

In addition to creating and opening files, the createfile function can also create console Buffering in the Win32 environment. The Return Value of the createfile function is the Console Buffer handle. View plaincopy to clipboardprint?
Handle console_write = createfile ("conout $", // buffer file on the system console
Generic_read | generic_write, // to read/write the Console Buffer file, which must be opened in read/write mode
File_cmd_read | file_cmd_write, // if multiple threads want to share the output console, set the sharing flag
Null, // file security attribute, null here
Open_existing, // both the output console and the input console must use the open_existing flag.
Null, // ignored Parameter
Null); // ignored Parameter
Handle console_write = createfile ("conout $", // buffer file on the system console
Generic_read | generic_write, // to read/write the Console Buffer file, which must be opened in read/write mode
File_cmd_read | file_cmd_write, // if multiple threads want to share the output console, set the sharing flag
Null, // file security attribute, null here
Open_existing, // both the output console and the input console must use the open_existing flag.
Null, // ignored Parameter
Null); // ignored Parameter

Now we have an available output console. You can use a similar method to get an input console:

View plaincopy to clipboardprint?
Handle console_read = createfile ("conin $", // note that the buffer file in the input console is changed to "conin $"
Generic_read | generic_write, // The following is the same as the output Console
File_pai_read | file_pai_write,
Null,
Open_existing,
Null,
Null );
Handle console_read = createfile ("conin $", // note that the buffer file in the input console is changed to "conin $"
Generic_read | generic_write, // The following is the same as the output Console
File_pai_read | file_pai_write,
Null,
Open_existing,
Null,
Null );

If you already haveProgramYou can use the getstdhandle () function to quickly obtain the console handle. The unique parameter of the getstdhandle function is a DWORD flag. The possible values include:

Std_output_handle // gets the output console handle
Std_input_handle // gets the handle of the input console
Std_error_handle // gets the handle of the error output Console

Now, you can use the input/output console to read and write disk files. You can use the write/readfile function. A better way is to use the write/readconsole function. The explanation given by Microsoft is that the "Write/readfile" function does not support Unicode.

View plaincopy to clipboardprint?
Char * buffer = {"Hello console world! /N "};
Writeconsole (getstdhandle (std_output_handle), // obtain the output console handle
Buffer, // cache of the content to be output
Strlen (buffer), // cache Length
Null, null
);
Char * buffer = {"Hello console world! /N "};
Writeconsole (getstdhandle (std_output_handle), // obtain the output console handle
Buffer, // cache of the content to be output
Strlen (buffer), // cache Length
Null, null
);

The cout and printf functions cannot be used in MFC, which means the formatting of output data is not that convenient. The solution is to use the format function of cstring and then getbuffer. Similarly, the input can only receive strings from the input buffer, and you need to separate the data.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shwneo/archive/2009/05/10/4165388.aspx

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.