首先你必须明确一点:正确使用CreateProcess()函数执行外部命令必须结合WaitForSingle()函数。
同时注意调用SetEnvironmentVariable()来设置相关的环境变量。
函数原型是:
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
);
第一个参数是可执行文件的全路径。第二个参数是其所带的命令行,
第三和第四个参数是分别是进程和线程的安全属性,如为null , 则继承父亲。
第五个表示是否继承父亲打开的句斌,第六个是一些创建标志具体可察看msdn
其余的可察看msdn。
给出一个示例:
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
CreateProcess( para, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi )
只要把para改成你的可执行文件名字!!
成功是返回true, 失败返回false
再给你贴个例子:
这个 process Execute 封装了createProcess()win32API 函数。用于调用另一个
应用程序。commanline参数指定了要调用的应用程序,另一个参数包含了SW_XXX
常量,用于控制窗口的显示方式,有SW_HIDE,SW_SHOW,SW_SHOWNRMAL等。
具体请见帮助。
function ProcessExecute(CommandLine: TCommandLine; cShow: Word): Integer;
{ This method encapsulates the call to CreateProcess() which creates
a new process and its primary thread. This is the method used in
Win32 to execute another application, This method requires the use
of the TStartInfo and TProcessInformation structures. These structures
are not documented as part of the Delphi 4 online help but rather
the Win32 help as STARTUPINFO and PROCESS_INFORMATION.
The CommandLine parameter specifies the pathname of the file to
execute.
The cShow parameter specifies one of the SW_XXXX constants which
specifies how to display the window. This value is assigned to the
sShowWindow field of the TStartupInfo structure. }
var
Rslt: LongBool;
StartUpInfo: TStartUpInfo; // documented as STARTUPINFO
ProcessInfo: TProcessInformation; // documented as PROCESS_INFORMATION
begin
{ Clear the StartupInfo structure }
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
{ Initialize the StartupInfo structure with required data.
Here, we assign the SW_XXXX constant to the wShowWindow field
of StartupInfo. When specifying a value to this field the
STARTF_USESSHOWWINDOW flag must be set in the dwFlags field.
Additional information on the TStartupInfo is provided in the Win32
online help under STARTUPINFO. }
with StartupInfo do
begin
cb := SizeOf(TStartupInfo); // Specify size of structure
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
wShowWindow := cShow
end;
{ Create the process by calling CreateProcess(). This function
fills the ProcessInfo structure with information about the new
process and its primary thread. Detailed information is provided
in the Win32 online help for the TProcessInfo structure under
PROCESS_INFORMATION. }
Rslt := CreateProcess(PChar(CommandLine), nil, nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
{ If Rslt is true, then the CreateProcess call was successful.
Otherwise, GetLastError will return an error code representing the
error which occurred. }
if Rslt then
with ProcessInfo do
begin
{ Wait until the process is in idle. }
WaitForInputIdle(hProcess, INFINITE);
CloseHandle(hThread); // Free the hThread handle
CloseHandle(hProcess);// Free the hProcess handle
Result := 0; // Set Result to 0, meaning successful
end
else Result := GetLastError; // Set result to the error code.
end;
调用:
var WERetVal: Word;
WERetVal := ProcessExecute(FCommandLine, sw_ShowNormal);
if WERetVal <> 0 then begin
raise Exception.Create('Error executing program. Error Code:; '+
IntToStr(WERetVal));
end;
都是以前的帖子里的,呵呵。