Go VC calls external EXE program mode

Source: Internet
Author: User

This article transferred from: http://blog.sina.com.cn/s/blog_486285690100ljwu.html

currently know three ways: Winexec,shellexecute, CreateProcess, others have summed up very well. Methods (functions) for invoking other applications in VC Winexec,shellexecute, CreateProcess ", I reproduced the full text, in addition to add their own summary (bold part, in addition to the title). Three SDK functions: Winexec,shellexecute, CreateProcess can implement the requirements of calling other programs, WinExec is the simplest, ShellExecute is more flexible than winexec, CreateProcess is the most complex. WinExec two parameters, the previous specified path, followed by a specified display mode. ShellExecute can specify the working directory, and can also find the association of the file directly open without loading the application associated with the file, ShellExecute can also open the Web page, initiate the corresponding message association to send the message, and so on. CreateProcess altogether has 10 parameters, but most of them can be replaced with NULL, which can specify the security properties of the process, the inheritance information, the class priority, and so on. If we want to get enough information about the new process and control the details of the new process, we need to use the CreateProcess function to achieve these goals. Syntax for three SDK functions (WinExec, Shellexec, crateprocess): (a) WinExec This function is the simplest, only two parameters, the prototype is as follows: UINT winexec (LPCSTR lpcmdline, 
    //Command PathUINT Ucmdshow//display Mode), use the following method: WinExec ("Notepad.exe", sw_show);//Open NotepadWinExec ("D:\\Program Files\\test\\test.exe", sw_showmaximized);//Open Test.exe in a maximized mannerNote that if you use sw_showmaxmized to load a program that does not have a maximized button, such as Neterm,calc, there will be no normal form, but it has been added to the task list. This function can only open exe files. Required header file: Windows.h,winbase.h (the former is experimental, the latter is on MSDN), in addition, the order of the two can not change. Description on MSDN: [Url]http://Msdn.microsoft.com/en-us/library/ms687393[/url] (vs.85). aspx(ii) The ShellExecute prototype is as follows: HINSTANCE ShellExecute (HWND hwnd,//parent window handleLPCTSTR Lpoperation,//operation, open with "edit", "Explore", "open", "find", "print", "NULL"LPCTSTR Lpfile,//file name, preceded by an add pathLPCTSTR Lpparameters,//ParametersLPCTSTR Lpdirectory,//default FolderINT nShowCmd//display Mode); Use the following method: ShellExecute (NULL,"Open","C:\\Test.txt", Null,null,sw_shownormal);//Open C:\Test.txt FileShellExecute (NULL,"Open","[Url]http://www.google.com/[/url]", NULL, NULL, SW_SHOWNORMAL);//open Web page [Url]www.google.com[/url]ShellExecute (NULL,"Explore","d:\\c++", Null,null,sw_shownormal);//Open Directory d:\c++ShellExecute (NULL,"Print","C:\\Test.txt", Null,null, Sw_hide);//Print File C:\Test.txtShellExecute does not support directed output. This function can open any file and invoke the program registered by the system to open the file corresponding to the suffix name. Required header file: Windows.h,shellapi.h (the former I tried out, the latter is what MSDN said needed). In addition, the order of the two can not change. Description on MSDN: [Url]http://Msdn.microsoft.com/en-us/library/bb762153[/url] (vs.85). aspx(iii) The CreateProcess prototype is as follows: BOOL CreateProcess (LPCTSTR lpapplicationname,//Execution Program nameLPTSTR lpCommandLine,//parameter Row//The following two parameters describe the security properties of the processes and threads that are created, and if NULL, the default security attributes are usedLpsecurity_attributes Lpprocessattributes,//Process Security AttributesLpsecurity_attributes Lpthreadattributes,//Thread security AttributesBOOL bInheritHandles,//Inheritance FlagsDWORD dwCreationFlags,//Create a flagLPVOID Lpenvironment,//Environment VariablesLPCTSTR Lpcurrentdirectory,//run the initial directory of the processLpstartupinfo Lpstartupinfo,//used to set various properties when creating a child processLpprocess_information lpprocessinformation//used to accept information after a process has been created); Use the following method: Process_information pi;                       Startupinfo si; memset (&si,0,sizeof(SI)); SI.CB=sizeof(SI); Si.wshowwindow=Sw_show; Si.dwflags=Startf_useshowwindow; BOOLFret=createprocess ("D:\\putty.exe",null,null,false,null,null,null,null,&si,&pi); This function can open any file and invoke the program registered by the system to open the file with the suffix name. Required header file: Windows.h,winbase.h (the former is experimental, the latter is on MSDN), in addition, the order of the two can not change. Description on MSDN: [Url]http://Msdn.microsoft.com/en-us/library/ms682425[/url] (vs.85). aspxAs can be seen, through the above several different methods, can be implemented in the application to open other applications, some of the methods may be troublesome, so we need to choose the most suitable for their own purpose of the way to achieve their goal! Additional notes on the three SDK functions: WinExec, Shellexecute,createprocess:1, define the content of the original author of the header file to delete. This is something that must be noted in reference to the new function. But Ms's header file reference order is a bit strange, such as the above three kinds of cleanup. In addition, if precompiled is used, the references before the # include "stdafx.h" in any source program are invalidated and will not be valid until later. (Admittedly, precompilation has his benefits, especially when the program is big, but any benefit comes at a price)2, define path CThe path represented in + + is used" \\ "Rather than the usual." \ ", so the above three functions represent a path of: Disk:\\directory\\...\\file namewinexec ("D:\\Program Files\\test\\test.exe", sw_showmaximized); ShellExecute (NULL,"Open","C:\\Test.txt", null,null,sw_shownormal);BOOLFret=createprocess ("D:\\putty.exe",null,null,false,null,null,null,null,&si,&pi)3, note the path of the file when program a invokes program B, the original default current path of B becomes the current path of a. So, be sure to pay attention. You can get into the habit of using absolute paths, and remember to open files and things like that, and be sure to verify that there are errors. 

Go VC calls external EXE program mode

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.