In Windows programming, not every application requires a graphical user interface (GUI). In many cases, we can compile a console application so that the program is smaller and loaded faster, the transmission time is short, and the functions of the program are not sacrificed at all. This type of program is especially suitable for programs running in the background, such as compression, anti-virus, upload and download. If we do need to execute these programs in the GUI to complete some functions, such as disk formatting, we can create a new process in the GUI program and call these existing console applications, help complete these functions. However, it is disappointing that every time we load these console applications, graphics programs always generate an undesirable Console window during the loading process, making our graphic user interface look nondescribable, when users see this interface, especially the console applications written by others or provided by the operating system, they may doubt or even compromise the reliability of our products. Therefore, we must try our best to prevent this window from being displayed. At the same time, we also need to direct the program running results to a text file. The input part of the console program can be completed by the GUI. Just like the visual c+compiler Compiling Program, msdev.exe(gui clcl.exe (console program) is responsible for background compilation, then the compilation result is directed to a file, and the compilation result is output to a window in the foreground graphic interface, however, the user does not notice this process during compilation. c ++ provides multiple functions for application loading, such as _ spawnlp, ShellExecute, system, and _ exec, except system, these functions cannot implement output targeting of console programs. However, the disadvantage of system functions is that a console window appears. If the computer is configured with a full screen command prompt line mode, it will directly switch your GUI program to the full-screen Console window, which is obviously a very bad solution.
_ Spawnlp (_ p_wait, "netstat", "-e", "-s", "-n", "r", "A", "-P ", "ip", null );
: ShellExecute (null, null, "ping.exe", "168.192.0.1> 1.txt", null, sw_shownormal );
System ("format A:/q> null ");
_ Execlp ("expand.exe", "source. Cab", "-F: M *. dll", C:/winnt/sytem32 ", null );
You can call the CreateProcess function to successfully implement the console application output targeting. Through this function, we can create a process, hide the console window, and export the output results of the console window to a text file.
In Windows 2000, the CreateProcess function provides a flag named create_no_window, which successfully prevents console windows from appearing. However, this flag is not supported in Windows 98. To hide the console window in two environments, you can set the startinfo structure member and pass it to the CreateProcess function.
The following is the interface and code of the program implementation part:
- Void cdow.dlg: onbnclickedbutton1 ()
- {
- Updatedata ();
- Byte B1, B2, B3, B4;
- If (m_ipaddressctrl.getaddress (B1, B2, B3, b4) <4)
- {
- // The content of the obtained IP address cannot be blank
- M_ipaddressctrl.setfocus ();
- Return;
- }
- Char character line [max_path];
- Wsprintf (cmdline, "ping.exe % d. % d", B1, B2, B3, b4 );
- Security_attributes SA = {sizeof (SA), null, true };
- Security_attributes * PSA = NULL;
- DWORD dw1_mode = file_1__read | file_1__write;
- Osversioninfo osversion = {0 };
- Osversion. dwosversioninfosize = sizeof (osversion );
- If (getversionex (& osversion ))
- {
- If (osversion. dwplatformid = ver_platform_win32_nt)
- {
- PSA = & SA;
- Dw1_mode | = file_1__delete;
- }
- }
- // Set the sharing mode and security attributes based on the version
- Handle hconsoleredirect = createfile (
- "C: // netstatus.txt ",
- Generic_write,
- Dww.mode,
- PSA,
- Open_always,
- File_attribute_normal,
- Null );
- Assert (hconsoleredirect! = Invalid_handle_value );
- Startupinfo S = {sizeof (s )};
- S. dwflags = startf_useshowwindow | startf_usestdhandles;
- // Use the standard handle and display window
- S. hstdoutput = hconsoleredirect; // use the file as the standard output handle.
- S. wshowwindow = sw_hide; // hide the console window
- Process_information Pi = {0 };
- If (CreateProcess (null, cmdline, null, null, true, null, & S, & PI ;))
- {
- // Create a process, run the Ping program, and test whether the network is connected.
- Waitforsingleobject (PI. hprocess, infinite );
- // Wait for the process to complete
- Closehandle (PI. hprocess );
- Closehandle (PI. hthread );
- // Close process and main thread handle
- }
- Closehandle (hconsoleredirect );
- // Close the handle to the exported file in the console
- Cfile myfile ("C: // netstatus.txt", cfile: moderead );
- Assert (myfile. m_hfile! = NULL );
- Char * psznetstatus = new char [myfile. getlength () + 1];
- Zeromemory (psznetstatus, myfile. getlength () + 1 );
- Myfile. Read (psznetstatus, myfile. getlength ());
- Myfile. Close ();
- // Open the file and read it into a character buffer
- Deletefile ("C: // netstatus.txt ");
- // Delete a temporary file
- M_editnetstatus.setwindowtext (psznetstatus );
- // Write the console program output information to the editing box
- Delete psznetstatus;
- }
Reprinted from: http://148332727.blog.51cto.com/500316/112316