WinExec是不能的,用下面的Code了function TExecForm.Exec(FileName: string; Visibility: integer): integer; var zappname:array [0..512] of char; zcurdir:array [0..255] of char; workdir:string; startupinfo:tstartupinfo; processinfo:tprocessinformation; exitcode:cardinal; begin strpcopy(zappname,filename); getdir(0,workdir); strpcopy(zcurdir,workdir); fillchar(startupinfo,sizeof(startupinfo),#0); startupinfo.cb:=sizeof(startupinfo); startupinfo.wShowWindow:=visibility; if not createprocess(nil,zappname,nil,nil,false,create_new_console or Normal_priority_class, nil,nil,startupinfo,processinfo) then result:=-1 else begin waitforsingleobject(processinfo.hProcess,infinite); getexitcodeprocess(processinfo.hProcess,exitcode); result:=exitcode; end; end;procedure TExecForm.ExecuteClick(Sender: TObject); var h:thandle; begin exec(exename.Text,0); showmessage('Ruturn'); end;
我倒是觉得楼主的问题可以解决: //对于 WinExec If the function succeeds, the return value is greater than 31. If the function fails, the return value is one of the following error values: Value Meaning 0 The system is out of memory or resources. ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image). ERROR_FILE_NOT_FOUND The specified file was not found.procedure TForm1.Button1Click(Sender: TObject); var ReTurn:Integer; begin ReTurn:=winexec(pchar('command.com /C '+'net view '+' >'+'c:\xinxi.txt'),sw_hide); if ReTurn>31 then begin if not fileexists('c:\xinxi.txt') then sleep(100) else memo1.Lines.LoadFromFile('c:\xinxi.txt'); end; end;//既然你都能把结果给得到了,还你能判断妈 /?
var
zappname:array [0..512] of char;
zcurdir:array [0..255] of char;
workdir:string;
startupinfo:tstartupinfo;
processinfo:tprocessinformation;
exitcode:cardinal;
begin
strpcopy(zappname,filename);
getdir(0,workdir);
strpcopy(zcurdir,workdir);
fillchar(startupinfo,sizeof(startupinfo),#0);
startupinfo.cb:=sizeof(startupinfo);
startupinfo.wShowWindow:=visibility;
if not createprocess(nil,zappname,nil,nil,false,create_new_console or Normal_priority_class,
nil,nil,startupinfo,processinfo) then result:=-1
else begin
waitforsingleobject(processinfo.hProcess,infinite);
getexitcodeprocess(processinfo.hProcess,exitcode);
result:=exitcode;
end;
end;procedure TExecForm.ExecuteClick(Sender: TObject);
var h:thandle;
begin
exec(exename.Text,0);
showmessage('Ruturn');
end;
StrCopy ( buff, "Project1.exe" ) ;
SHELLEXECUTEINFO ExeInfo;
// Animate1->Active=true;
ZeroMemory(&ExeInfo,sizeof( SHELLEXECUTEINFO));
ExeInfo.cbSize = sizeof( SHELLEXECUTEINFO);
ExeInfo.lpFile = buff;
ExeInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ExeInfo.nShow = SW_MAXIMIZE;//SW_HIDE;
ShellExecuteEx(&ExeInfo);
delete []buff ;
this->Hide();
WaitForSingleObject(ExeInfo.hProcess,INFINITE);
this->Show();
var
cAppName: array [0..512] of char;
cCurDir: array [0..255] of char;
strWorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(cAppName, strFileName);
GetDir(0, strWorkDir);
StrPCopy(cCurDir, strWorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := uCmdShow;
if not CreateProcess(nil, cAppName, nil, nil, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := INFINITE
else
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
我想直接判断netsh.exe是否在进程中是否可以,如何判断,请教。
如果进程中存在,如何等待直到进程执行完,再执行下一条命令?
//对于 WinExec
If the function succeeds, the return value is greater than 31.
If the function fails, the return value is one of the following error values: Value Meaning
0 The system is out of memory or resources.
ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
ERROR_FILE_NOT_FOUND The specified file was not found.procedure TForm1.Button1Click(Sender: TObject);
var ReTurn:Integer;
begin
ReTurn:=winexec(pchar('command.com /C '+'net view '+' >'+'c:\xinxi.txt'),sw_hide);
if ReTurn>31 then
begin
if not fileexists('c:\xinxi.txt') then
sleep(100)
else
memo1.Lines.LoadFromFile('c:\xinxi.txt');
end;
end;//既然你都能把结果给得到了,还你能判断妈 /?