怎样判断winexec调用的命令已经执行完成了?
我使用winexec(pchar(...),sw_hide)执行一个DOS命令,如何知道这个命令已经执行完成了? 问题点数:0、回复次数:11Top
1 楼nzh517(沙中泥)回复于 2003-12-03 00:26:07 得分 0
用这个函数好象有些难办,你试试用别的函数调用执行其它程序如:CreateProcessTop
2 楼ly_liuyang(Liu Yang LYSoft http://lysoft.7u7.net)回复于 2003-12-03 00:46:11 得分 0
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;Top
3 楼Behard(我爱天安门)回复于 2003-12-03 14:02:12 得分 0
char *buff = new char[256] ;
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();Top
4 楼sailer_shi(孤独的我,孤单的走,孤单的活着,孤单呀)回复于 2003-12-03 23:58:35 得分 0
执行完winexec后判断最上面的窗体的handle的名称是不是你想要得应用程序的handle的名称Top
5 楼d0347(旭日升群枭)回复于 2003-12-04 10:58:45 得分 0
UpTop
6 楼ouyang75928(Thinking in Pascal)回复于 2003-12-04 11:53:29 得分 0
function WinExecAndWait(strFileName: string; uCmdShow: UINT): DWORD;
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;Top
7 楼greating(快乐青年)回复于 2003-12-05 13:47:36 得分 0
我试了ly_liuyang函数,不能满足需要,主要是我执行的是NETSH命令,无法如何该函数运行时都没有办法隐藏cmd对话框。
我想直接判断netsh.exe是否在进程中是否可以,如何判断,请教。
如果进程中存在,如何等待直到进程执行完,再执行下一条命令?Top
8 楼xiangwangz(<*敝屣荣华 浮云生死 此身何惧*>)回复于 2003-12-05 17:01:10 得分 0
我倒是觉得楼主的问题可以解决:
//对于 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;//既然你都能把结果给得到了,还你能判断妈 /?Top
9 楼selfboss(黃海)回复于 2003-12-05 17:39:57 得分 0
GZTop
10 楼angelface(§<@#$&^%$>§)回复于 2003-12-07 18:11:22 得分 0
XUEXI...Top
11 楼greating(快乐青年)回复于 2003-12-09 14:49:08 得分 0
xiangwangz兄的方法不可行,因为RETURN返回的值在程序开始时,而我是要知道程序什么时候结束,当然不行。Top




