Console Application targeted output

Source: Internet
Author: User

 

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:

  1. Void cdow.dlg: onbnclickedbutton1 ()
  2. {
  3. Updatedata ();
  4. Byte B1, B2, B3, B4;
  5. If (m_ipaddressctrl.getaddress (B1, B2, B3, b4) <4)
  6. {
  7. // The content of the obtained IP address cannot be blank
  8. M_ipaddressctrl.setfocus ();
  9. Return;
  10. }
  11. Char character line [max_path];
  12. Wsprintf (cmdline, "ping.exe % d. % d", B1, B2, B3, b4 );
  13. Security_attributes SA = {sizeof (SA), null, true };
  14. Security_attributes * PSA = NULL;
  15. DWORD dw1_mode = file_1__read | file_1__write;
  16. Osversioninfo osversion = {0 };
  17. Osversion. dwosversioninfosize = sizeof (osversion );
  18. If (getversionex (& osversion ))
  19. {
  20. If (osversion. dwplatformid = ver_platform_win32_nt)
  21. {
  22. PSA = & SA;
  23. Dw1_mode | = file_1__delete;
  24. }
  25. }
  26. // Set the sharing mode and security attributes based on the version
  27. Handle hconsoleredirect = createfile (
  28. "C: // netstatus.txt ",
  29. Generic_write,
  30. Dww.mode,
  31. PSA,
  32. Open_always,
  33. File_attribute_normal,
  34. Null );
  35. Assert (hconsoleredirect! = Invalid_handle_value );
  36. Startupinfo S = {sizeof (s )};
  37. S. dwflags = startf_useshowwindow | startf_usestdhandles;
  38. // Use the standard handle and display window
  39. S. hstdoutput = hconsoleredirect; // use the file as the standard output handle.
  40. S. wshowwindow = sw_hide; // hide the console window
  41. Process_information Pi = {0 };
  42. If (CreateProcess (null, cmdline, null, null, true, null, & S, & PI ;))
  43. {
  44. // Create a process, run the Ping program, and test whether the network is connected.
  45. Waitforsingleobject (PI. hprocess, infinite );
  46. // Wait for the process to complete
  47. Closehandle (PI. hprocess );
  48. Closehandle (PI. hthread );
  49. // Close process and main thread handle
  50. }
  51. Closehandle (hconsoleredirect );
  52. // Close the handle to the exported file in the console
  53. Cfile myfile ("C: // netstatus.txt", cfile: moderead );
  54. Assert (myfile. m_hfile! = NULL );
  55. Char * psznetstatus = new char [myfile. getlength () + 1];
  56. Zeromemory (psznetstatus, myfile. getlength () + 1 );
  57. Myfile. Read (psznetstatus, myfile. getlength ());
  58. Myfile. Close ();
  59. // Open the file and read it into a character buffer
  60. Deletefile ("C: // netstatus.txt ");
  61. // Delete a temporary file
  62. M_editnetstatus.setwindowtext (psznetstatus );
  63. // Write the console program output information to the editing box
  64. Delete psznetstatus;
  65. }

 

Reprinted from: http://148332727.blog.51cto.com/500316/112316

 

 

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.