MFC calls DOS window to output data or string

Create a new MFC window program, double-click the OK button on the interface.

First introduce the functions to be used: The

AllocConsole() function will allocate a new console to the calling process.
Prototype:
BOOL WINAPI AllocConsole(void);
Parameters: None
Return Value: If the function succeeds, the return value is non-zero; if the function fails, the return value is zero.
Note: A process can only have one console associated with it, so this function will fail if the calling process already has a console.


After FreeConsole()

calls the AllocConsole() function to associate the DOS window, you need to use this function to release the associated DOS window. If you directly press the × key on the window program to close the program, an error will be reported. So you can write this function to the callback function of the "Cancel" button.

WriteConsole()

Function prototype:

BOOLWINAPIWriteConsole(
__in HANDLEhConsoleOutput,
__in const VOID *lpBuffer,
__in DWORDnNumberOfCharsToWrite,
__out LPDWORDlpNumberOfCharsWritten,
__reserved LPVOIDlpReserved
); 

Parameters:

hConsoleOutput
console screen buffer handle. The handle must have GENERIC_WRITE access.

lpBuffer
contains a pointer to the buffer into which characters are to be written to the console screen buffer.
If the buffer is allocated from the process's 64kb heap, the maximum size of the buffer will depend on heap usage.

nNumberOfCharsToWrite
The number of characters to write. If the total size of the specified number of characters exceeds the available heap, the function fails with ERROR_NOT_ENOUGH_MEMORY.

lpNumberOfCharsWritten
Pointer to a variable that receives the number of characters actually written.

lpReserved
reserved; must be NULL.


Then there is the function to get the width of the string:

strlen() and wcslen() are functions defined by standard C++, which get the length of the ASCII string and the wide string respectively.

The new version of VS generally defaults to the Unicode wide character set, so the wcslen() function is generally used to calculate the string width. If it is a pure English string, you can also use the function sizeof() provided by C to calculate the string width.

--------------------------------------------

1. First call AllocConsole( );function.

void CDOS_MDlg::OnBnClickedOk()
{
	// TODO: Add control notification handler code here
	// CDialogEx :: OnOK ();


	CString terribly;
	AllocConsole();//Associate a console


	HANDLE hdlWrite = GetStdHandle(STD_OUTPUT_HANDLE); //Get the handle of the console


	terribly=L"Spring brother pure man!"; //The string to be output

	WriteConsole(hdlWrite, terribly, wcslen(terribly), NULL, NULL); //output string to DOS window

}

2. After the call, the DOS window needs to be released, and the FreeConsole() function is called in the callback function of the cancel key to release resources

void CDOS_MDlg::OnBnClickedCancel()
{
	// TODO: Add control notification handler code here

	FreeConsole();//Release the associated console, otherwise an error will be reported

	CDialogEx :: OnCancel ();
}


3. Sometimes it is easy to click the × button in the upper right corner to close the program when the operation is fast, causing the program to fail, so it is best to reload the OnSysCommand message and add the command to release the console in the callback function.

void ××× :: OnSysCommand (UINT nID, LPARAM lParam)
{
	// TODO: add message handler code and/or call defaults here


	FreeConsole();//Release the associated console, otherwise an error will be reported


	CDialogEx::OnSysCommand(nID, lParam);
}

MFC calls the DOS window output information as a supplement to the debugger, because sometimes there is no way to use the debug window to view the output information.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325932762&siteId=291194637