查找cmd的handle就可以判断是否有cmd.exe

解决方案 »

  1.   

    献上API一个:
    EnumProcess我也不懂。
      

  2.   

    没找到这个API函数呀!到底应该怎么做呀?
      

  3.   

    Takes a snapshot of the processes and the heaps, modules, and threads used by the processes.HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, 
        DWORD th32ProcessID);  
    ParametersdwFlagsFlags specifying portions of the system to include in the snapshot. These values are defined:TH32CS_INHERIT Indicates that the snapshot handle is to be inheritable.
    TH32CS_SNAPALL Equivalent to specifying the TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE, TH32CS_SNAPPROCESS, and TH32CS_SNAPTHREAD values.
    TH32CS_SNAPHEAPLIST Includes the heap list of the specified  process in the snapshot.
    TH32CS_SNAPMODULE Includes the module list of the specified  process in the snapshot.
    TH32CS_SNAPPROCESS Includes the Win32 process list in the snapshot.
    TH32CS_SNAPTHREAD Includes the Win32 thread list in the snapshot.
     th32ProcessIDProcess identifier. This parameter can be zero to indicate the current process. This parameter is used when the TH32CS_SNAPHEAPLIST or TH32CS_SNAPMODULE value is specified. Otherwise, it is ignored. Return ValueReturns an open handle to the specified snapshot if successful or  - 1 otherwise. ResThe snapshot taken by this function is examined by the other tool help functions to provide their results. Access to the snapshot is read only. The snapshot handle acts like an object handle and is subject to the same rules regarding which processes and threads it is valid in. 
    To retrieve an extended error status code generated by this function, use the GetLastError function.
      

  4.   

    然后调用
    process32First
    process32Next
      

  5.   

    用Findwindow得到窗口Handle,详细的看看msdn。
      

  6.   

    用findwindow的话,我不知道cmd.exe的classname是什么?能告诉我吗?
      

  7.   

    Delphi 中关闭“计算器,对你可能有帮助
    var 
    HWndCalculator : HWnd; 
    begin 
    // find the exist calculator window 
    HWndCalculator := Winprocs.FindWindow(nil, '计算器'); // close the exist Calculator 
    if HWndCalculator <> 0 then 
    SendMessage(HWndCalculator, WM_CLOSE, 0, 0); 
    end;
      

  8.   

    诶,送佛送到西了。看下面吧:
    procedure TForm1.Button2Click(Sender: TObject);
    var
    HWndCalculator : HWnd;
    begin
    // find the exist calculator window
    HWndCalculator := FindWindow(nil, 'D:\WINNT\System32\cmd.exe'); // close the exist Calculator
    if HWndCalculator <> 0 then
    SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
    end;
      

  9.   

    procedure Search(Strings:TStrings);
    var
      Snap:THandle;
      RB:Boolean;
      PE:TProcessEntry32;
    begin
      if Strings=nil then
        Exit;
      snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
      if snap = -1 then Exit;
      try
        PE.dwSize:=SizeOf(TProcessEntry32);
        RB:=Process32First(snap,PE);
        while RB do
        begin
          if PE.szExeFile=CMD.EXE' then
            Strings.AddObject(PE.szExeFile,Pointer(PE.th32ProcessID));
          PE.dwSize:=SizeOf(TProcessEntry32);
          RB:=Process32Next(snap,PE);
        end;
      finally
        CloseHandle(snap);
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      MList : TStringList;
      Str   : String;
    begin
      Str := ExtractFilePath(application.ExeName) ;
      if Str[Length(Str)]<> '\'then Str:=Str+'\';
      MList := TStringList.Create ;
      Search(MList);
      if MList= nil then
      begin
        try
          ShellExecute(Handle,pchar('open'),pchar(Str+CMD.EXE'),nil,nil,SW_SHOW);
        except
          MessageBox(Handle,'请确定CMD.EXE程序是否存在!','系统提示',MB_OK+MB_ICONWARNING);
        end;
      end;
      MList.Free ;
    end;
      

  10.   

    这段代码对你会有帮助
    //==============================================================================
    //强制终止某应用程序运行********************************************************
    //==============================================================================
    procedure AppForceExit(const AppName: string);
    var lppe: TProcessEntry32;
        ssHandle: THandle;
        Wnd: HWND;
        AppFound: Boolean;
    begin
      ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
      AppFound := Process32First(sshandle, lppe);
      while AppFound do
      begin
        //其中lppe.szExefile就是程序名**********************************************
        if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then
        begin
          Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
          TerminateProcess(Wnd, 0);
        end;
        AppFound := Process32Next(ssHandle, lppe);
      end;
    end;