如标题:
在delphi程序怎样调用外部.exe(需要带参数调用)程序,及怎样通过delphi程序判断外部程序是否在运行,和怎样结束外部程序的运行??

解决方案 »

  1.   

    调用外部.exe :Winexec(pchar('AAA.EXE /Start'), sw_normal);
      

  2.   

    hunanqms() :怎样带参数调用呢,我说的是对于.exe程序本身的参数,有没有关于Winexec函数的说明?
      

  3.   

    带参数的:
    Winexec('C:\AAA.EXE 555', SW_SHOW);其中555为参数。
      

  4.   

    及怎样通过delphi程序判断外部程序是否在运行
    可以通过判断相应的进程是否存在;调用API,
    ,和怎样结束外部程序的运行?? 
    可以结束进程;调用API
    具体的凼数记不清了,查HELP的WINDOWS SDK,
      

  5.   

    运行程序我知道可以用winexec和shellexecute,现在关键是不知道怎样通过delphi程序判断外部程序是否在运行,怎样将运行的外部程序kill掉??
      

  6.   

    var hwnd:THandle;
    begin
      hwnd:=findwindow('TForm1','Form1');
      if hwnd<>0 then
        sendMessage(hwnd,WM_CLOSE,0,0);
    end;
      

  7.   

    给你一段代码,自己用的,
    //调用外部程序 格式如: diff.exe a.txt b.txt >>c.txt
    function TDiff.AnsiDiff(File1,File2:string;stream:TmemoryStream):string;
    const
      DiffStr='/c diff %s %s >>%s';
    var
      sTmp:string;
    begin
      sTmp:=formatDateTime('mmddhhmmss',now)+inttostr(getTickcount)+inttostr(random(1000))+'.txt';
      ShellExecute (0,nil,'cmd.exe',pchar(Format(DiffStr,[File1,File2,sTmp])),nil,0);  
      if stream<>nil then
        stream.LoadFromFile(sTmp);
       result:=sTmp;
    end;
      

  8.   

    procedure TForm1.EndProcess(AFileName: string);
    const
      PROCESS_TERMINATE = $0001;
    var
      ContinueLoop: BOOL;
      FSnapShotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
    begin
      FSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      while integer(ContinueLoop) <> 0 do
      begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(AFileName))
          or (UpperCase(FProcessEntry32.szExeFile ) = UpperCase(AFileName))) then
        TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
                          FProcessEntry32.th32ProcessID), 0);
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
    end;
    //AFileName为要终止的程序全名
      

  9.   

    学delphi的,很多都不知道内核对象处理啊,晕死,给一个标准写法吧
    下面这个函数用于启动一个程序,我的代码拷出来的,懒得改了,自己看吧
    FHProcessService是进程句柄,THandle类型
    function TfrmMain.StartService(bStart: Boolean): Boolean;
    var
      WorkDir           : string;
      StartupInfo       : TStartupInfo;
      ProcessInfo       : TProcessInformation;
    begin
      Result := False;  if bStart then
      begin
        if not OpenIpService then
        begin
          WorkDir := ExtractFileDir(Application.ExeName);
          FillChar(StartupInfo, Sizeof(StartupInfo), #0);
          StartupInfo.cb := Sizeof(StartupInfo);
          StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
          StartupInfo.wShowWindow := SW_SHOWDEFAULT;
          if not CreateProcess(nil,
            PChar(GetExePath + szServerServiceFileName),  { pointer to command line string }
            nil, { pointer to process security attributes }
            nil, { 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
          begin
            MemoError.Lines.Add('服务创建失败');
            Exit;
          end;
          MemoError.Lines.Add('服务创建成功');
          FHProcessService := ProcessInfo.hProcess;
        end;
        if FCheckServerThread <> nil then
        begin
          if not FCheckServerThread.Terminated then
            FCheckServerThread.Terminate;
          FCheckServerThread.Free;    end;    FCheckServerThread := TCheckServerThread.Create(False);
        MemoError.Lines.Add('开始监视服务器运行情况');
        FCheckServerThread.Resume;    Result := True;
      end
      else
      begin
        if FHProcessService <> 0 then
        begin
          FControlEvent.SetEvent;
          if WaitForSingleObject(FHProcessService, 10000) <> WAIT_OBJECT_0 then
          begin
            Result := False;
          end
          else
            Result := True;
          CloseHandle(FHProcessService);
          FHProcessService := 0;
        end;
      end;
    end;FCheckServerThread 是自己写的监视进程线程,代码如下procedure TCheckServerThread.Execute;
    begin
      inherited;
      if frmMain.FHProcessService <> 0 then
      begin    if WaitForSingleObject(frmMain.FHProcessService, INFINITE) <> WAIT_OBJECT_0
          then
          PostMessage(frmMain.Handle, WM_WAITERROR, 0, 0)
        else
          PostMessage(frmMain.Handle, WM_PROCESSOVER, 0, 0);
      end;
    end;
    这种方法好处在于,不消耗系统资源,内核对象在被等待时期,不占用任何资源(对象本身除外) ,比循环扫描进程列表要好很多
      

  10.   

    结束进程可以用TerminateProcess函数
    The TerminateProcess function terminates the specified process and all of its threads.
    BOOL TerminateProcess(
      HANDLE hProcess,
      UINT uExitCode
    );Parameters
    hProcess 
    [in] Handle to the process to terminate. 
    The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.uExitCode 
    [in] Exit code to be used by the process and threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve a process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value. 
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.但个人不建议使用,该方法强行中止,有可能引起资源不释放,建议使用fancylee(小恐龙) 的方法发送消息,如果没有消息循环,可以用事件通知(程序的是自己写的),否则,就只有强行中止了
      

  11.   

    fancylee(小恐龙)的方法不错,蛮好的,谢谢你了!也要感谢 muroachanf(菜鸟中的霸王) ,dreamover(梦醒了) ,结贴了!!!