Q&A

  • 특정프로그램을 실행한후에 종료할때까지 기달리기.
실행하고대기하는 소스는 찾아서 붙여
4  COMMENTS
  • Profile
    정병선 2006.10.19 03:53
    TForm의 ShowModal 부분을 수정해서 만들었습니다.
    참고해서 코딩하시길...

    uses
      ShellAPI, Consts;

    {$R *.dfm}

    function MakeModalState(H: HWND): Integer;
    var
      WindowList: Pointer;
      ActiveWindow: HWnd;
      ModalResult: Integer;
      Visible, Enabled: Boolean;
    begin
      Visible:= IsWindowVisible(H);
      Enabled:= IsWindowEnabled(H);
      if Not Visible or not Enabled then
        raise EInvalidOperation.Create(SCannotShowModal);
      if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
      ReleaseCapture;
      Application.ModalStarted;
      try
      ActiveWindow := GetActiveWindow;
      WindowList := DisableTaskWindows(0);
      try
        SendMessage(H, CM_ACTIVATE, 0, 0);
        ModalResult := 0;
        repeat
          Application.HandleMessage;
          if Application.Terminated then
            ModalResult := mrCancel;
          Sleep(1);
        until Not IsWindow(H);
        Result := ModalResult;
        SendMessage(H, CM_DEACTIVATE, 0, 0);
        if GetActiveWindow <> H then
          ActiveWindow:= 0;

      finally
        EnableTaskWindows(WindowList);
        if ActiveWindow <> 0 then
          SetActiveWindow(ActiveWindow);
      end;
      finally
        Application.ModalFinished;
      end;
    end;

    function ForceSetForegroundWindow(AHandle: HWND): Boolean;
    var
      fromId, toId: DWORD;
    begin
      fromId:= GetCurrentThreadId();
      toId:= GetWindowThreadProcessId(GetForegroundWindow, nil);
      AttachThreadInput(fromId, toId, TRUE);
      Result:= SetForegroundWindow(AHandle);
      AttachThreadInput(fromId, toId, FALSE);
    end;

    procedure ShowFindWindow(WND: HWND);
    var
      Placement: TWindowPlacement;
    begin
      if IsWindow(WND) then
      begin
        Placement.length := SizeOf(TWindowPlacement);
        GetWindowPlacement(WND, @Placement);
        case Placement.showCmd of
          SW_SHOWMINIMIZED:
            ShowWindow(WND, SW_RESTORE)
          else
            ShowWindow(WND, SW_SHOW);
        end;
        if GetActiveWindow <> WND then
          ForceSetForegroundWindow(WND);
      end;
    end;

    function WaitUntilProcessTerminated(AppHandle: HWND; ExeFileName: TFileName;
        ExeClass, ExeCaption: String): Boolean;
    var
      ExeHandle: HWND;
      Count: Integer;
      ValueNames: TStrings;
      i: Integer;
      dtsrunuifile: TFileName;
      ec: PChar;
    begin
      Result:= False;

      if Not FileExists(ExeFileName) then
        Raise Exception.Create(Format('%s 파일이 존재하지 않습니다', [ExeFileName]));

      if ExeCaption = '' then
        ec:= nil
      else
        ec:= PChar(ExeCaption);
      if FindWindow(PChar(ExeClass), ec) <> 0 then
      begin
        MessageDlg(Format('이미 %s 파일이 실행되어 있습니다', [ExtractFileName(ExeFileName)]), mtInformation, [mbYes], 0);
        Exit;
      end;

      Screen.Cursor:= crHourGlass;
      ShellExecute(AppHandle, 'Open', PChar(ExeFileName), nil, nil, SW_SHOW);
      Count:= 0;
      // 실행파일이 로딩 될 때 까지 기다리기
      ExeHandle:= FindWindow(PChar(ExeClass), ec);
      while (Count < 1000) and (ExeHandle = 0) do
      begin
        ExeHandle:= FindWindow(PChar(ExeClass), ec);
        inc(Count);
        Sleep(10);
      end;
      Screen.Cursor:= crDefault;

      if ExeHandle = 0 then
        Raise Exception.Create(Format('%s 실행파일의 윈도우를 찾을 수 없습니다', [ExtractFileName(ExeFileName)]));

      SetForegroundWindow(ExeHandle);

      if MakeModalState(ExeHandle) = 0 then
      begin
        ShowFindWindow(AppHandle);
        Result:= True;
      end;
    end;


    ex)
      WaitUntilProcessTerminated(Application.Handle, 'c:\windows\system32\notepad.exe', 'notepad', '');


  • Profile
    이정욱 2006.09.25 01:58
    그럼 ExecFileAndWait 다음에 필요한것을 하시면 됩니다.
    종료 후 다음 문장이 실행 됩니다.


  • Profile
    최진환 2006.09.26 21:48
    저 프로그램이 로딩될때를 기달리는게 아닌.
    저 프로그램이 로딩된다음에...프로세서에 올라오고 나서 .
    사용이 끝나면 프로세서에서 해제 된것을 찾고 싶습니다..

    채팅 프로그램처럼 서버 클라이언트 소켓을 들여다보느라 이제 봤네요..죄송합니다..


  • Profile
    이정욱 2006.09.27 01:39
    네. 프로세스가 끝날때 까지 대기하게 되는것입니다...
    로딩이 아니구요 ^^