procedure TForm1.Button1Click(Sender: TObject); var s: String; begin s:=ExtractFilePath(Application.ExeName)+'yourApp.exe'; winExec(pchar(s),SW_SHOWNORMAL); end;
ShellExecutewinExecF1 Help a once. =====================================
还有:function WinExecAndWait(Path: string; Visibility: Word): DWORD; function CreateProcessAndWait(const AppPath, AppParams: string; Visibility: Word): DWORD; var SI: TStartupInfo; PI: TProcessInformation; Proc: THandle; WaitResult: DWORD; begin FillChar(SI, SizeOf(SI), 0); SI.cb := SizeOf(SI); SI.wShowWindow := Visibility; SI.dwFlags := STARTF_USESHOWWINDOW; if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then raise Exception.CreateFmt('不能执行,请输入或选择可执行文件的名称和路径.错误信息为“%s”', [SysErrorMessage(GetLastError)]); Proc := PI.hProcess; CloseHandle(PI.hThread); repeat Application.ProcessMessages; WaitResult := WaitForSingleObject(Proc, 200); until (WaitResult <> WAIT_TIMEOUT) or (WaitResult = WAIT_FAILED); GetExitCodeProcess(Proc, Result); CloseHandle(Proc); end;var P: Integer; Params: string; begin Params := Path; P := Pos(' ', Path); // assume params start at first space Delete(Path, P, Length(Path)); Result := CreateProcessAndWait(Path, Params, Visibility); end;
Visibility应该是:Value Meaning SW_HIDE Hides the window and activates another window. SW_MAXIMIZE Maximizes the specified window. SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active. SW_SHOWNA Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active. SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
...
ShellExecute(self.Handle, 'open', 'd:\pro.exe', nil, nil, SW_SHOWNORMAL);
WinExec('d:\pro.exe', SW_SHOWNORMAL);
cmdLine := 'c:\windows\notepad.exe';
var
sInfo : TStartupInfo;
pInfo : TProcessInformation;
begin
FillChar(sInfo,sizeof(sInfo),#0);
sInfo.cb := SizeOf(sInfo);
sInfo.dwFlags := STARTF_USESHOWWINDOW;
sInfo.wShowWindow := SW_NORMAL; if not CreateProcess(nil,pchar(cmdLine),nil,nil,false,CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then
MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)
else
begin
WaitForSingleObject(pInfo.hProcess,INFINITE);
GetExitCodeProcess(pInfo.hProcess,exitCode);
end;
end;
...
ShellExecute(self.Handle, 'open', 'd:\pro.exe', nil, nil, SW_SHOWNORMAL);
WinExec('d:\pro.exe', SW_SHOWNORMAL);
var s: String;
begin
s:=ExtractFilePath(Application.ExeName)+'yourApp.exe';
winExec(pchar(s),SW_SHOWNORMAL);
end;
用ShellExecute()的意见,90分
用WinExec()的意见,60分
用ShellExecute()的意见,90分
用WinExec()的意见,60分
ShellExecute(调用窗口的句柄,'open',被调用函数名,调用参数,被调用函数目录,SW_SHOW);
svar:string;
begin
svar:='c:\name.exe';
...
ShellExecute(self.Handle, 'open', svar, nil, nil, SW_SHOWNORMAL);
WinExecShellExecute在CSDN搜索一大把
=====================================
Visibility: Word): DWORD;
var
SI: TStartupInfo;
PI: TProcessInformation;
Proc: THandle;
WaitResult: DWORD;
begin
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
SI.wShowWindow := Visibility;
SI.dwFlags := STARTF_USESHOWWINDOW;
if not CreateProcess(PChar(AppPath), PChar(AppParams), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
raise Exception.CreateFmt('不能执行,请输入或选择可执行文件的名称和路径.错误信息为“%s”',
[SysErrorMessage(GetLastError)]);
Proc := PI.hProcess;
CloseHandle(PI.hThread);
repeat
Application.ProcessMessages;
WaitResult := WaitForSingleObject(Proc, 200);
until (WaitResult <> WAIT_TIMEOUT) or (WaitResult = WAIT_FAILED);
GetExitCodeProcess(Proc, Result);
CloseHandle(Proc);
end;var
P: Integer;
Params: string;
begin
Params := Path;
P := Pos(' ', Path); // assume params start at first space
Delete(Path, P, Length(Path));
Result := CreateProcessAndWait(Path, Params, Visibility);
end;
SW_HIDE Hides the window and activates another window.
SW_MAXIMIZE Maximizes the specified window.
SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.
SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
SW_SHOW Activates the window and displays it in its current size and position.
SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.
SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.
SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.
SW_SHOWNA Displays the window in its current state. The active window remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
uses ShellApi;ShellExecute
WinExec