如题

解决方案 »

  1.   

    1. CreateProcess 里面会有新执行的进程的handle
    2. 保存这个handle / 不保存也可以。。
    3. TerminateProcess() // 传入1/2的那个 handle.The CreateProcess function is used to run a new program. The WinExec and LoadModule functions are still available, but they are implemented as calls to CreateProcess. The CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file. BOOL CreateProcess(
      LPCTSTR lpApplicationName,
                             // pointer to name of executable module
      LPTSTR lpCommandLine,  // pointer to command line string
      LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes
      LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes
      BOOL bInheritHandles,  // handle inheritance flag
      DWORD dwCreationFlags, // creation flags
      LPVOID lpEnvironment,  // pointer to new environment block
      LPCTSTR lpCurrentDirectory,   // pointer to current directory name
      LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO
      LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION
    );
    // 注意最后一个参数....里面
    PROCESS_INFORMATION
    The PROCESS_INFORMATION structure is filled in by the CreateProcess function with information about a newly created process and its primary thread. typedef struct _PROCESS_INFORMATION { // pi 
        HANDLE hProcess;  //  这个参数就是新执行的handle 到时候你TerminateProcess
        HANDLE hThread;   //  的时候就用这个 hProcess
        DWORD dwProcessId;  // 
        DWORD dwThreadId; 
    } PROCESS
    然后..TerminateProcess
    The TerminateProcess function terminates the specified process and all of its threads. BOOL TerminateProcess(
      HANDLE hProcess, // handle to the process
      UINT uExitCode   // exit code for the process
    );
      

  2.   

    如果用winexec的话 你需要再去遍历你新执行的进程的HANDLE 比较麻烦
    还是用 CreateProcess好
      

  3.   

    beyondtkl(大龙驹<逝追>) 方法的代碼!
    將 WaitForSingleObject 去掉就可, 修改後面的代碼, 
    需要時就用 TerminateProcess 終止程序
     
    uses Forms, Windows;procedure TForm1.Button1Click(Sender: TObject);
    var
      proc_info: TProcessInformation;
      startinfo: TStartupInfo;
      ExitCode: longword;
    begin
      // Initialize the structures
      FillChar(proc_info, sizeof(TProcessInformation), 0);
      FillChar(startinfo, sizeof(TStartupInfo), 0);
      startinfo.cb := sizeof(TStartupInfo);  // Attempts to create the process
      if CreateProcess('c:\windows\notepad.exe', nil, nil,
          nil, false, NORMAL_PRIORITY_CLASS, nil, nil,
           startinfo, proc_info) <> False then begin
        // The process has been successfully created
        // No let's wait till it ends...
        WaitForSingleObject(proc_info.hProcess, INFINITE);
        // Process has finished. Now we should close it.
        GetExitCodeProcess(proc_info.hProcess, ExitCode);  // Optional
        CloseHandle(proc_info.hThread);
        CloseHandle(proc_info.hProcess);
        Application.MessageBox(
          PChar(Format('Notepad finished! (Exit code=%d)', [ExitCode])),
          'Info', MB_ICONINFORMATION);
      end else begin
        // Failure creating the process
        Application.MessageBox('Couldn''t execute the '
          + 'application', 'Error', MB_ICONEXCLAMATION);
      end;//if
    end;
      

  4.   

    運行程序:procedure TForm1.btnExecuteClick(Sender: TObject);
    begin
      FillChar(proc_info, sizeof(TProcessInformation), 0);
      FillChar(startinfo, sizeof(TStartupInfo), 0);
      startinfo.cb := sizeof(TStartupInfo);
      if CreateProcess(nil, 'c:\windows\notepad.exe', nil,
          nil, false, CREATE_DEFAULT_ERROR_MODE
          + NORMAL_PRIORITY_CLASS, nil, nil, startinfo,
          proc_info) then begin
        btnExecute.Enabled := False;
        btnCancel.Enabled := True;
        Timer1.Enabled := True;
      end else begin
        CloseHandle(proc_info.hProcess);
        Application.MessageBox('Couldn''t execute the '
          + 'application', 'Error', MB_ICONEXCLAMATION);
      end;
    end;
      

  5.   

    終止程序:
    procedure TForm1.btnCancelClick(Sender: TObject);
    begin
      Timer1.Enabled := False;
      if Application.MessageBox('You should try to finish'
      + ' the application normally.'#13#13'&iquest;Terminate it '
      + 'anyway?', 'Warning', MB_YESNO + MB_DEFBUTTON2 +
      MB_ICONQUESTION + MB_TASKMODAL) = ID_YES then begin
        TerminateProcess(proc_info.hProcess, 0);
        CloseHandle(proc_info.hProcess);
        btnCancel.Enabled := False;
        btnExecute.Enabled := True;
      end else begin
        Timer1.Enabled := True;
      end;
    end;
      

  6.   

    我认为一个简捷的方法是往DOS窗口发消息:
    SENDMESSAGE(DOSHANDLE,WM_KEYDOWN,CTRL+C,0)
    从而中止应用程序的执行.比较容易实现.
      

  7.   

    请问aiirii(ari-爱的眼睛),在使用你的这段代码时是否可以将dos窗口隐藏运行我的程序呢?
      

  8.   

    此问另给100分!!http://community.csdn.net/Expert/topic/3226/3226267.xml?temp=.4068415
      

  9.   

    FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
      StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartUpInfo.wShowWindow := SW_HIDE;使用这个参数!!!
      // create hidden process
      if CreateProcess(nil, PChar(FileName), nil, nil,False, IDLE_PRIORITY_CLASS,
                       nil, nil, StartUpInfo,ProcessInfo) then
         begin
           CloseHandle(ProcessInfo.hThread);
           CloseHandle(ProcessInfo.hProcess);
         end;