📄 faq71.htm
字号:
<HTML>
<HEAD>
<TITLE>Launch another program</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Launch another program
</H3>
<P>
Call the API <TT>CreateProcess</TT>, <TT>ShellExecute</TT> or <TT>WinExec</TT> functions.
<TT>WinExec</TT> is the easiest one to use, but it is also out of date, and Microsoft
doesn't recommend that you use it. This FAQ demonstrates how to use <TT>ShellExecute</TT> and
<TT>CreateProcess</TT> to launch the solitaire game.
</P>
<H4> Using <TT>ShellExecute</TT></H4>
<P>
Here is an example of how to launch notepad using <TT>ShellExecute</TT>.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
ShellExecute<b>(</b>NULL<b>,</b>
<font color="blue">"open"</font><b>,</b>
<font color="blue">"notepad.exe"</font><b>,</b>
<font color="blue">""</font><b>,</b>
<font color="blue">""</font><b>,</b>
SW_SHOWDEFAULT<b>)</b><b>;</b>
<b>}</b>
</pre>
<P>
<TT>ShellExecute</TT> can also be used to open documents in the file system, such as Word DOC files, or
Excel XLS files. The second argument to <TT>ShellExecute</TT> determines what you are trying to do.
Valid values in include "open", "print","explore", "find", and a few others. When you are trying to launch
an executable, pass the "open" string.
</P>
<P>
The third argument is the name of the program to execute. If you were trying to open a DOC file, you would pass the
file name in the third argument. The fourth argument is the command line parameters that you want to pass to the program.
The next example demonstrates how you can use this argument.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
ShellExecute<b>(</b>NULL<b>,</b>
<font color="blue">"open"</font><b>,</b>
<font color="blue">"notepad.exe"</font><b>,</b>
<font color="blue">"c:\\cbuilder5\\readme.txt"</font><b>,</b>
<font color="blue">""</font><b>,</b>
SW_SHOWDEFAULT<b>)</b><b>;</b>
<b>}</b>
</pre>
<P>
Of course, since notepad is usually associated with TXT files anyway, you could also open the readme file using this syntax:
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
ShellExecute<b>(</b>NULL<b>,</b>
<font color="blue">"open"</font><b>,</b>
<font color="blue">"c:\\cbuilder5\\readme.txt"</font><b>,</b>
<font color="blue">""</font><b>,</b>
<font color="blue">""</font><b>,</b>
SW_SHOWDEFAULT<b>)</b><b>;</b>
<b>}</b>
</pre>
<H4> Using <TT>CreatProcess</TT></H4>
<P>
The <TT>ShellExecute</TT> function is pretty straightforward, but it does not allow you to control
the child process. For finer control of what's going on, use <TT>CreateProcess</TT> instead.
It is a little more complex, but it also provides more flexibility.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<font color="navy">// solitaire is in the windows directory,</font>
<font color="navy">// and has the name sol.exe.</font>
<b>char</b> szWindowDir <b>[</b>MAX_PATH<b>]</b><b>;</b>
GetWindowsDirectory<b>(</b>szWindowDir<b>,</b> MAX_PATH<b>)</b><b>;</b>
AnsiString strSolitaire <b>=</b> AnsiString<b>(</b>szWindowDir<b>)</b> <b>+</b> <font color="blue">"\\sol.exe"</font><b>;</b>
STARTUPINFO StartupInfo<b>;</b>
ZeroMemory<b>(</b> <b>&</b>StartupInfo<b>,</b> <b>sizeof</b><b>(</b>STARTUPINFO<b>)</b><b>)</b><b>;</b>
StartupInfo<b>.</b>cb <b>=</b> <b>sizeof</b><b>(</b>STARTUPINFO<b>)</b><b>;</b>
PROCESS_INFORMATION ProcessInfo<b>;</b>
<b>if</b><b>(</b>CreateProcess<b>(</b>strSolitaire<b>.</b>c_str<b>(</b><b>)</b><b>,</b>
NULL<b>,</b>
NULL<b>,</b>
NULL<b>,</b>
TRUE<b>,</b>
NORMAL_PRIORITY_CLASS<b>,</b>
NULL<b>,</b>
NULL<b>,</b>
<b>&</b>StartupInfo<b>,</b>
<b>&</b>ProcessInfo<b>)</b><b>)</b>
<b>{</b>
<font color="navy">// We must close the handles returned in ProcessInfo. We can</font>
<font color="navy">// close the handle at any time, might as well close it now</font>
CloseHandle<b>(</b>ProcessInfo<b>.</b>hProcess<b>)</b><b>;</b>
CloseHandle<b>(</b>ProcessInfo<b>.</b>hThread<b>)</b><b>;</b>
<b>}</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> If you want to pass command line arguments to the program, place the command line arguments in the second
argument to <TT>CreateProcess</TT>. The code below shows how to launch Notepad and have it open a file. Notice that we
don't use the first parameter for anything. The second parameter contains both the executable name, and the file to
open. The two must be separated by a space.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button2Click<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<b>char</b> szWindowDir <b>[</b>MAX_PATH<b>]</b><b>;</b>
GetWindowsDirectory<b>(</b>szWindowDir<b>,</b> MAX_PATH<b>)</b><b>;</b>
AnsiString strNotepad <b>=</b> AnsiString<b>(</b>szWindowDir<b>)</b> <b>+</b> <font color="blue">"\\notepad.exe"</font><b>;</b>
strNotepad <b>=</b> strNotepad <b>+</b> <font color="blue">" "</font> <b>+</b> <font color="blue">"c:\\cbuilder3\\deploy.txt"</font><b>;</b>
STARTUPINFO StartupInfo<b>;</b>
ZeroMemory<b>(</b> <b>&</b>StartupInfo<b>,</b> <b>sizeof</b><b>(</b>STARTUPINFO<b>)</b><b>)</b><b>;</b>
StartupInfo<b>.</b>cb <b>=</b> <b>sizeof</b><b>(</b>STARTUPINFO<b>)</b><b>;</b>
PROCESS_INFORMATION ProcessInfo<b>;</b>
<b>if</b><b>(</b>CreateProcess<b>(</b>NULL<b>,</b>
strNotepad<b>.</b>c_str<b>(</b><b>)</b><b>,</b>
NULL<b>,</b>
NULL<b>,</b>
TRUE<b>,</b>
NORMAL_PRIORITY_CLASS<b>,</b>
NULL<b>,</b>
NULL<b>,</b>
<b>&</b>StartupInfo<b>,</b>
<b>&</b>ProcessInfo<b>)</b><b>)</b>
<b>{</b>
CloseHandle<b>(</b>ProcessInfo<b>.</b>hProcess<b>)</b><b>;</b>
CloseHandle<b>(</b>ProcessInfo<b>.</b>hThread<b>)</b><b>;</b>
<b>}</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> The last argument to <TT>CreateProcess</TT> is a pointer to a <TT>PROCESS_INFORMATION</TT> structure.
If <TT>CreateProcess</TT> succeeds, the <TT>PROCESS_INFORMATION</TT> structure will contain a thread handle and a
process handle for the new program. You must call <TT>CloseHandle</TT> to close the two handles that are returned in
the <TT>PROCESS_INFORMATION</TT> structure. You can close the handles at any time, but you can't use the handles once
you close them. The two previous code samples close the handles as soon as <TT>CreateProcess</TT> returns.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -