我要实现利用这个小程序来检测test.exe是否已经运行,没有运行的话,启动test.exe.
我的代码如下:
procedure TForm1.Timer1Timer(Sender: TObject);
var
  hd:Thandle;
  mypath,z:string;
  z: string;
begin
  MyPath:=ExtractFilePath(ParamStr(0));
  hd:=findwindow(nil,'测试');
  if hd=0 then
  begin
    z:=mypath+'test.exe';
    winexec(pchar(z),SW_SHOW);
  end;
end;执行第一次是正常的,但test.exe关闭之后,就不再重新启动了(这段代码在Timer里面),请高手指点,还有什么好办法没有?

解决方案 »

  1.   

    怎么关闭的?关闭之后是不是进程还在?也就是说hd>0
      

  2.   

    多了个z: string;
    将SW_SHOW改成SW_NORMAL
      

  3.   

    估计是你的test.exe有问题,关闭后进程还在。
      

  4.   

    你可以试一下别的程序,别用test.exe测试了,比如说打开一个计算器procedure TForm1.Button1Click(Sender: TObject);
    var
      hd: Thandle;
    begin
      hd := findwindow(nil, '计算器');
      if hd=0 then
      begin
        winexec(PChar('calc'), SW_SHOW);
      end;
    end;
      

  5.   

    如果我把这个程序关闭再开的话,又可以运行test.exe一次,但Timer循环的话,只能运行一次,跟踪的话,每次都运行了winexec,只是没有调用成功,有什么返回值吗?是不是要初始化,不明白,请指点,谢谢
      

  6.   

    if hd=0 then
      begin
        z:=mypath+'test.exe';
        winexec(pchar(z),SW_SHOW);
      end
      else showmessage('a')看能不能showmessage,好确定问题出在hd>0还是winexec没调用成功
      

  7.   

    用sysmessage(getlasterror)看看错误原因
      

  8.   

    如果没有运行test.exe的话,hd=0,我已经跟踪了,执行了winexec(pchar(z),SW_SHOW),只是执行不成功。