WaitForSingleObject可能已进入等待結束 状态 程序没结束就没法执行下一句
你把TerminateProcess(execinfo.hProcess,h); 写到另一个buttun里试式

解决方案 »

  1.   

    用这个试试,我试成功的:var
      execinfo: TSHELLEXECUTEINFO;
      dwRet, dwTicks: LongWord;
    begin
      FillChar(execinfo,SizeOf(execinfo),0);
      execinfo.cbSize:=sizeof(execinfo);
      execinfo.lpVerb:='Open';
      execinfo.lpFile:='cn_server.exe';
      execinfo.fMask:=SEE_MASK_NOCLOSEPROCESS;
      execinfo.nShow:=SW_SHOWDEFAULT;
      if ShellExecuteEx(@execinfo) then
      begin
        ShowMessage( Format('execinfo.hProcess=%d', [execinfo.hProcess]) );    dwTicks := GetTickCount;
        repeat
          if GetExitCodeProcess(execinfo.hProcess, dwRet) and (STILL_ACTIVE<>dwRet) then
          begin // terminated
            ShowMessage( 'Returned!' );
            Break;
          end;      if GetTickCount-dwTicks>3000 then  // Time out
          begin
            if Application.MessageBox( 'kill the process?', 'Timed out', MB_ICONQUESTION or MB_OKCancel)=IDCancel then
              dwTicks := GetTickCount // recount
            else
            begin
              TerminateProcess(execinfo.hProcess, 255);
              ShowMessage( 'Killed!' );
              Break;
            end;
          end;      Application.ProcessMessages; // 不要让应用程序象死了一样,:)
        until False; // dead loop  end
      else
        ShowMessage( 'Can not run it!' );
    end;
      

  2.   

    agui(阿贵):非常感谢,经改进现在是可以杀死,但就是投盘不刷新,要鼠标进入其图标焦点时才消失,有点美中不足就是。procedure con_ser;
    var execinfo: TSHELLEXECUTEINFO;dwRet, dwTicks,FileHandle:LongWord;fn:string;
    begin
     IF not FILEEXISTS('ser_comp.txt') THEN
     begin
      FillChar(execinfo,SizeOf(execinfo),0);
      execinfo.cbSize:=sizeof(execinfo);
      execinfo.lpVerb:='Open';
      execinfo.lpFile:='cn_server.exe';
      execinfo.fMask:=SEE_MASK_NOCLOSEPROCESS;
      execinfo.nShow:=SW_SHOWDEFAULT;
      if ShellExecuteEx(@execinfo) then
      begin
        //ShowMessage( Format('execinfo.hProcess=%d', [execinfo.hProcess]) );
        dwTicks := GetTickCount;
        repeat
          if GetExitCodeProcess(execinfo.hProcess, dwRet) and (STILL_ACTIVE<>dwRet)
          then  Break;  // terminated
          if GetTickCount-dwTicks>3000 then  // Time out
          begin
             IF not FILEEXISTS('ser_comp.txt') THEN   //需要创建一文本做标识
             begin
               fn:='ser_comp.txt';
               FileHandle := FileCreate(fn);
               FileWrite(FileHandle,fn,1);
               FileClose(FileHandle);
             end;
              TerminateProcess(execinfo.hProcess, 255);
             // ShowMessage( 'Killed!' );
              Break;
          end;
          Application.ProcessMessages; // 不要让应用程序象死了一样,:)
        until False; // dead loop
      end
      else ShowMessage( '自动运行首次服务器配置失败,请手工运行!' );
     end;
    end;
      

  3.   

    找<<程序员开发指南>>瞧一下相关文章,就明白了
      

  4.   

    你先前没有提托盘的问题啊,呵呵。杀死程序不会让托盘图标自动消失,我见过很多这样的例子。我建议你先杀图标,再杀程序。我做过一个程序,用来杀托盘图标,但愿对你有所帮助。我的解决步骤是这样的:
    1、用SPY++找出该进程中的窗口的类名和标题;
    2、编程序用窗口的类名和标题找到窗口句柄(FindWindow);
    3、使用Shell_NotifyIcon来验证图标,图标ID用1到65535试验足够了,你可以用NIM_MODIFY试试。关于Shell_NotifyIcon请查API帮助;
    4、找到了正确的窗口和图标ID后,用Shell_NotifyIcon+NIM_DELETE就可以杀图标了。源程序如下:
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        ScrollBox1: TScrollBox;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
        Fnid: TNotifyIconData;
        FCurrID: LongWord;
        lblCurrMsg: TLabel;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      hProxy: HWND;  procedure AddMsg( const Msg: String );
      var
        t: Integer;
      begin
        if Assigned(lblCurrMsg) then
          t := lblCurrMsg.Top+lblCurrMsg.Height
        else
          t := 0;    lblCurrMsg := TLabel.Create( Self );
        with lblCurrMsg do
        begin
          Parent := ScrollBox1;
          SetBounds( 0, t, ScrollBox1.Width, Height );
          ScrollBox1.ScrollInView(lblCurrMsg);
          //Align := alTop;
          Caption := Msg;      Update;
        end;
      end;  procedure UpdateMsg( const Msg: String );
      begin
        with lblCurrMsg do
        begin
          Caption := Msg;
          Update;
        end;
      end;begin
      Button2.Enabled := True;
      Button1.Enabled := False;
      AddMsg( 'Searching Proxy window...' );
      hProxy := FindWindow( 'ProxyGotClass', 'Proxy' );
      if not IsWindow(hProxy) then
      begin
        AddMsg( 'not found!' );
        Button1.Enabled := True;
        Button2.Enabled := False;
        Exit;
      end;  AddMsg( Format('found: $%.8X', [hProxy]) );
      AddMsg( Format( 'Start from %d', [FCurrID]) );  AddMsg( 'Try ID...' );
      Fnid.Wnd := hProxy;
      Fnid.uID := FCurrID;
      StrCopy( Fnid.szTip, 'Hey! I got it!' );
      while Fnid.uID<=High(Word) do  // we just test from 0 to 65535
      begin
        UpdateMsg( Format( 'Try ID...%d', [Fnid.uID]) );    if Shell_NotifyIcon(NIM_DELETE, @Fnid) then  // this will kill the icon
        //if Shell_NotifyIcon(NIM_MODIFY, @Fnid) then // just make it invisible
        begin
          Button1.Enabled := False;
          AddMsg( 'Conguration! you got it!' );
          ShowMessage( 'Conguration! you got it!' );
          Break;
        end;    Inc( Fnid.uID );    if not Button2.Enabled then
        begin
          Break;
        end;    Application.ProcessMessages;  // dont freeze the application
      end;
      FCurrID := Fnid.uID;  Button1.Enabled := not Button2.Enabled;
      Button2.Enabled := False;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Button2.Enabled := False;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FillChar( Fnid, sizeof(Fnid), 0 );
      Fnid.cbSize := sizeof( Fnid );
      Fnid.hIcon := Application.Icon.Handle;
          // change to 0 (null icon) will make the icon invisible
      Fnid.uFlags := NIF_ICON or NIF_TIP;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Button2.Click;  // make Button1Click exit
    end;end.