Windows API calls external programs

Source: Internet
Author: User

To be appliedProgramTo start other applications, there are three functions available for use, I will talk about them one by one (I will open the zeecalls.exe application under D: \ Program Files \ zeecballs \ directory as an example ):

1. winexec

This function is the simplest and has only two parameters. The prototype is as follows:

Uint winexec (
Maid, // address of command line
Uint ucmdshow // window style for New Application
);

The usage is as follows:

Winexec (_ T ("d :\\ Program Files \ zeecballs \ zeecalls.exe"), sw_showmaximized );

In this example, zeecalls.exe is opened in simplified format. In VC, '/' must be written.

2. ShellExecute

ShellExecute is a little more flexible than winexex, so more parameters are required. The prototype is as follows:

Hinstance ShellExecute (
Hwnd, // parent window handle
Lpctstr lpoperation, // Open Mode
Lptstr lpfile, // name of the file to be opened
Lptstr lpparameters, lptstr lpdirectory, // file path
Int nshowcmd );

Similarly, we can use this function to open the files we need:

ShellExecute (null, _ T ("open"), _ T ("zeecalls.exe"), null, _ T ("d :\\ Program Files \ zeecall \\"), sw_shownormal );

In this case, open zeecalls.exe in the normal format.

3. CreateProcess

The preceding methods all enable other applications in our own applications. However, we do not get enough information about new processes, there is no excessive means to control the detailed attributes of the new process. Therefore, to achieve these objectives, we need to use the CreateProcess function. First, let's look at the prototype of this function:

Bool CreateProcess (

Lptstr lpapplicationname, // name of the execution Program

Lptstr lpcommandline, // Parameter Line

// The following two parameters describe the security attributes of the created process and thread. If it is null, the default security attribute is used.
Lpsecurity_attributes lpprocessattributes, // process security attributes
Lpsecurity_attributes lpthreadattributes, // thread security attributes

Bool binherithandles, // inheritance flag
DWORD dwcreationflags, // create a flag
Lpvoid lpenvironment, // environment variable
Maid directory, // The initial directory where the process is running
Lpstartupinfo, // used to set various attributes when creating a sub-process
Lpprocess_information lpprocessinformation // used to receive relevant information after a process is created.
);

The preceding parameters use two important data structures: startupinfo and process_information. The two structures are defined as follows:

Typedef struct _ startupinfo {// Si
Dword cb; // structure Length
Lptstr lpreserved; // Reserved
Lptstr lpdesktop; // Reserved
Lptstr lptitle; // The title displayed if it is a console process
DWORD dwx; // window abscissa
DWORD dwy; // window cluster coordinates
DWORD dwxsize; // window width
DWORD dwysize; // window height
DWORD dwxcountchars; // width of the console window symbol
DWORD dwycountchars; // The text symbol height of the console window
DWORD dwfillattri; // Console window filling mode
DWORD dwflags; // create a tag
Word wshowwindow; // The display tag of the window, which is similar to the tag in showwindow.
3. CreateProcess

The preceding methods all enable other applications in our own applications. However, we do not get enough information about new processes, there is no excessive means to control the detailed attributes of the new process. Therefore, to achieve these objectives, we need to use the CreateProcess function. First, let's look at the prototype of this function:

Bool CreateProcess (

Lptstr lpapplicationname, // name of the execution Program

Lptstr lpcommandline, // Parameter Line

// The following two parameters describe the security attributes of the created process and thread. If it is null, the default security attribute is used.
Lpsecurity_attributes lpprocessattributes, // process security attributes
Lpsecurity_attributes lpthreadattributes, // thread security attributes

Bool binherithandles, // inheritance flag
DWORD dwcreationflags, // create a flag
Lpvoid lpenvironment, // environment variable
Maid directory, // The initial directory where the process is running
Lpstartupinfo, // used to set various attributes when creating a sub-process
Lpprocess_information lpprocessinformation // used to receive relevant information after a process is created.
);

The preceding parameters use two important data structures: startupinfo and process_information. The two structures are defined as follows:

typedef struct _ startupinfo {// Si
dword cb; // structure length
lptstr lpreserved; // reserved
lptstr lpdesktop; // retain
lptstr lptitle; // The title displayed for the console process
DWORD dwx; // the abscissa of the window
DWORD dwy; // window cluster coordinate
DWORD dwxsize; // window width
DWORD dwysize; // window height
DWORD dwxcountchars; // Console window symbol width
DWORD dwycountchars; // Console window symbol height
DWORD dwfillattribute; // Console window filling mode
DWORD dwflags; // create a flag
word wshowwindow; // display a flag in the window, just like the flag in the showwindow
word cbreserved2; // keep the parameter
lpbyte lpreserved2; // reserved parameter
handle hstdinput; // standard input handle
handle hstdoutput; // standard output handle
handle hstderror; // standard error handle
}startupinfo, * lpstartupinfo;

Typedef struct _ process_information {// pi
Handle hprocess; // Process Handle
Handle hthread; // The main thread handle of the process.
DWORD dwprocessid; // process ID
DWORD dwthreadid; // the ID of the main thread of the process.
} Process_information;

As an example, let's see how to use CreateProcess to open the same file:

Process_information PI;
Startupinfo Si;
Memset (& Si, 0, sizeof (SI ));
Si. cb = sizeof (SI );
Si. wshowwindow = sw_show;
Si. dwflags = startf_useshowwindow;
 
Bool fret = CreateProcess ("d :\\ Program Files \ zeecballs \ zeecalls.exe", null, null, false, null, & Si, & PI );
 
It can be seen that the above several different methods can achieve the purpose of opening other applications in the application, the above methods may be a little troublesome, therefore, we need to select the method that best suits us for different purposes to achieve our own goals!

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.