在DELPHI6中,用API函数 SHELLECEXTUE 运行一个已关联的文件或者是一个可执行文件,之后如何再通过DELPHI结束它?

解决方案 »

  1.   

    uses tlhelp32;
    假设要终止的程序的文件名为:project2.exe,那么例程如下:
    var
    lppe:tprocessentry32;
    sshandle:thandle;
    hh:hwnd;
    found:boolean;
    begin
    sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);
    found:=process32first(sshandle,lppe);
    while found do
    begin
      //进行你的处理其中lppe.szExefile就是程序名。
      if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then
      begin
        hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);
        TerminateProcess(hh,0);
      end;
      found:=process32next(sshandle,lppe);
    end;
    end;
    ********************
    HANDLE hProcess
    Windows NT/2000: The handle must have PROCESS_TERMINATE access. 
    For more information, see Process Security and Access Rights. 所以要先使用 
    DWORD SetSecurityInfo(
      HANDLE handle,                     // handle to object
      SE_OBJECT_TYPE ObjectType,         // object type
      SECURITY_INFORMATION SecurityInfo, // buffer
      PSID psidOwner,                    // new owner SID
      PSID psidGroup,                    // new primary group SID
      PACL pDacl,                        // new DACL
      PACL pSacl                         // new SACL
    );
      

  2.   

    获得进程句柄,销毁进程,请参考Win32 API
      

  3.   

    问题是如何通过SHELLEXECUTE获得进程句柄?
      

  4.   

    用CreateProcess不就行了吗,干吗非用SHExecute?
    另外,最好不要用TerminateProcess关闭进程,要改
    用向程序的主进程PostThreadMessage(WM_QUIT)的方
    式让它自己关闭。
      

  5.   

    CreateProcess怎么用,可以运行关联文件吗?比如直接运行README.txt,会自动打开记事本?
      

  6.   

    不能关联文件,如果你要这样运行,必须用SHExecute或者WinExec.
      

  7.   

    对呀,可用SHELLEXECUTE是不是得不到进程句柄,也就是无法关闭它了,对吗?
      

  8.   

    执行外部程序一直到它结束 ㈠ 编程语言Delphi 1.0,操作系统 Window3.1以下是一个执行外部程序ARJ.EXE的例子的部分代码
    相信看过就会用了。var
      sCommandLine: string;
      bCreateProcess: boolean;
      lpStartupInfo: TStartupInfo;
      lpProcessInformation: TProcessInformation;
    begin
      sCommandLine := 'ARJ.EXE /?';
      bCreateProcess := CreateProcessA(nil, 
    PChar(sCommandLine),
        nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
        lpStartupInfo, lpProcessInformation);
      if bCreateProcess then
        WaitForSingleObject(lpProcessInformation.hProcess, 
    INFINITE);
    end;
    ㈡ 编程语言Delphi3.0,操作系统 Window95 
    同样是上面的例子的部分代码 var
       pWindowsList: pointer;
       hActiveWindow: HWnd;
       hExeHandle: THandle;
    begin
       pWindowsList := DisableTaskWindows(0);
       hActiveWindow := GetActiveWindow;
       try
          hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);
          while GetModuleUsage(hExeHandle) <> 0 do
          Application.ProcessMessages;
       finally
          EnableTaskWindows(pWindowsList);
          SetActiveWindow(hActiveWindow);
       end;
    end;
    // 相信你明白了。
    题外话:如果执行的是 MSDOS 外部程序,如何能让它的窗口不显示
    出来呢? [ 接上例 ]:
    TStartupInfo 这个结构中有一个 sShowWindow 栏位, 将之设为 
    SW_HIDE即可,
    同时, dwFlags 标志中至少需含有 STARTF_USESHOWWINDOW, 否则
    CreateProcess
    时, sShowWindow 栏位的设定会无效, 以下是修改过的程式:var
       sCommandLine: string;
       bCreateProcess: boolean;
       lpStartupInfo: TStartupInfo;
       lpProcessInformation: TProcessInformation;
    begin
       // sCommandLine 的内容请视您的情况修改
       sCommandLine :='Xcopy d:\temp\temp1\*.* d:\temp\temp2 
    /v/y';
       lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
       lpStartupInfo.wShowWindow := SW_HIDE;
       bCreateProcess := CreateProcess(nil, 
    PChar(sCommandLine),nil,nil,True,
                    HIGH_PRIORITY_CLASS, nil, 
    nil,lpStartupInfo, lpProcessInformation);
       if bCreateProcess then
            WaitForSingleObject(lpProcessInformation.hProcess, 
    INFINITE);
    end;