procedure button1Onclick(.....);
var
sPath:string;
mybat:textfile;
begin
sPath:=当前工程路径; assignfile(mybat,spath+'a.bat');
rewrite(mybat);
writeln(mybat,'ping 127.0.0.1');
closefile(mybat);
winexec(spath+'a.bat',sw_hide); //再对mybat重写一次
assignfile(mybat,spath+'a.bat');
rewrite(mybat); //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
//如果前面没运行过,就正常重写了a.bat中的内容
writeln(mybat,'ping 192.168.1.3');
closefile(mybat);
winexec(spath+'a.bat',sw_hide);end;想问 用同一个.bat文件 怎么运行多次?????????
var
sPath:string;
mybat:textfile;
begin
sPath:=当前工程路径; assignfile(mybat,spath+'a.bat');
rewrite(mybat);
writeln(mybat,'ping 127.0.0.1');
closefile(mybat);
winexec(spath+'a.bat',sw_hide); //再对mybat重写一次
assignfile(mybat,spath+'a.bat');
rewrite(mybat); //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
//如果前面没运行过,就正常重写了a.bat中的内容
writeln(mybat,'ping 192.168.1.3');
closefile(mybat);
winexec(spath+'a.bat',sw_hide);end;想问 用同一个.bat文件 怎么运行多次?????????
语句都试过了,没有问题
要是没执行过winexec
那么重写a.bat后,内容也变化了
就是执行一次后,执行到rewrite那里就i/o异常了
procedure TForm1.btn2Click(Sender: TObject);
var
s1: TStringList;
s2:TMemoryStream;
s:String;
i:integer;
sPath:string;
begin
sPath:='当前工程路径';
s1 := TStringList.Create;
s2:=TMemoryStream.Create;
if fileexists(sPath + 'a.bat') then
begin
s1.LoadFromFile(sPath + 'a.bat');
end;
try
s:=Trim(Edit1.Text);{可变内容}
s1.add(s);
s1.SaveToStream(s2);
s2.SaveToFile(sPath + 'a.bat');
finally
s1.free;
s2.Free;
end;
end;
我想知道怎么,才能执行了一次winexec后
再对mybat重写内容,能够再执行winexec.因为程序中,要不断对mybat重写后,再执行呀.急急急!!!
var
sPath:string;
mybat:textfile;
begin
sPath:=ExtractFilePath(ParamStr(0));
assignfile(mybat,spath+'a.bat');
rewrite(mybat);
writeln(mybat,'ping 127.0.0.1');
closefile(mybat);
winexec(pchar(spath+'a.bat'),sw_hide);
assignfile(mybat,spath+'a.bat');
rewrite(mybat);
writeln(mybat,'ping 192.168.1.3');
closefile(mybat);
winexec(pchar(spath+'a.bat'),sw_hide);
end;
Procedure TForm1.Button1Click(Sender: TObject);
Var
sPath: String;
mybat: textfile;
Begin
sPath := ExtractFilePath(Application.ExeName); assignfile(mybat, sPath + 'a.bat');
rewrite(mybat);
writeln(mybat, 'ping 127.0.0.1');
closefile(mybat);
winexec(PChar(sPath + 'a.bat'), sw_hide); //再对mybat重写一次
assignfile(mybat, sPath + 'a.bat');
rewrite(mybat); //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
//如果前面没运行过,就正常重写了a.bat中的内容
writeln(mybat, 'ping 192.168.1.3');
closefile(mybat);
winexec(PChar(sPath + 'a.bat'), sw_hide);
End;
sPath:string;
mybat:textfile;
begin
sPath:=ExtractFilePath(ParamStr(0)); assignfile(mybat,spath+'a.bat');
rewrite(mybat);
writeln(mybat,'ping 127.0.0.1');
closefile(mybat);
winexec(PChar(spath+'a.bat'),1);
Sleep(3000); //注意要加上停顿时间
//再对mybat重写一次
assignfile(mybat,spath+'a.bat');
rewrite(mybat); //这里会有i/o异常 (只要前面有运行了一次winexec(a.bat...))
//如果前面没运行过,就正常重写了a.bat中的内容
writeln(mybat,'ping 192.168.1.3');
closefile(mybat);
winexec(pchar(spath+'a.bat'),1);
var
sa:TSecurityAttributes;
hReadPipe,hWritePipe:THandle;
ret:BOOL;
strBuff:array[0..255] of char;
lngBytesread:DWORD;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(sa,Sizeof(sa),#0);
sa.nLength := Sizeof(sa);
sa.bInheritHandle := True;
sa.lpSecurityDescriptor := nil;
ret := CreatePipe(hReadPipe, hWritePipe, @sa, 0); WorkDir:=ExtractFileDir(Application.ExeName);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb:=Sizeof(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow:=Visibility; StartupInfo.hStdOutput:=hWritePipe;
StartupInfo.hStdError:=hWritePipe; if not CreateProcess(nil,
PChar(FileName), { pointer to command line string }
@sa, { pointer to process security attributes }
@sa, { pointer to thread security attributes }
True, { handle inheritance flag }
// CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
PChar(WorkDir), { pointer to current directory name, PChar}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
then Result := INFINITE {-1} else
begin
// Form1.Hide;
// FileOpen(FileName,fmShareExclusive);
// SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
ret:=CloseHandle(hWritePipe);
mOutputs:=';
while ret do
begin
FillChar(strBuff,Sizeof(strBuff),#0);
ret := ReadFile(hReadPipe, strBuff, 256, lngBytesread, nil);
mOutputs := mOutputs + strBuff;
end; Application.ProcessMessages;
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess); { to prevent memory leaks }
CloseHandle(ProcessInfo.hThread);
// Form1.Close; { exit application }
ret := CloseHandle(hReadPipe);
end;
end;
有没有方法,让它一执行完第一次winexec后,马上就执行下面的语句????