我现在要写一个程序,按一个button,启动金山词霸.再按这个的时候系统要提示金山词霸已经运行.

解决方案 »

  1.   

    是的,这个我明白,但是我不知道如何能获取要启动程序的handle
      

  2.   

    EnumProcess或用快照枚举进程,判断要启动的进程是否在列表中。
      

  3.   

    使用Windows的API FindWindow
      

  4.   

    FindWindow找到窗口
    PostMessage(句柄,WM_close,0,0);
      

  5.   

    在主窗体创建时候调用一下函数:
    uses
      TLHelp32;
    //根据应用名字结束指定模块路径的程序,结束所有
    //ModuleFullPath --程序路径,如: c:\test\test.exe
    //AppName --程序名, 如:test.exe,可省略不写  function KillProcessByFullPath(ModuleFullPath: string; AppName: string = ''): integer;
    var
      lppe: TPROCESSENTRY32;
      found: boolean;
      ProcessList: THandle;
      ModuleList: Thandle;
      pm: TMODULEENTRY32;
      h: Thandle;
      a: DWORD;
      ModuleName: string;
      AppProcessId: DWORD; // 线程ID
    begin
      Result := 0;
      AppProcessId := GetCurrentProcessId;
      if Length(AppName) = 0 then
        AppName := ExtractFileName(ModuleFullPath);
      lppe.dwSize := sizeof(TPROCESSENTRY32);
      pm.dwSize := sizeof(TMODULEENTRY32);
      ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      found := Process32First(ProcessList, lppe);
      while found do //进程列表
      begin
        if StrIComp(PChar(StrPas(lppe.szExeFile)), PChar(AppName)) = 0 then //应用程序名字是否相同?
        begin
          ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, lppe.th32ProcessID);
          found := module32first(ModuleList, pm);
          while found do //模块列表
          begin
            ModuleName := StrPas(pm.szexepath);
            ModuleName := copy(ModuleName, max(1, pos(':', ModuleName) - 1), MaxInt);
            if StrIComp(pchar(ModuleName), pchar(ModuleFullPath)) = 0 then //是否包含要找的模块
            begin
              if AppProcessId <> lppe.th32ProcessID then
              begin
                h := openprocess(PROCESS_ALL_ACCESS, TRUE, lppe.th32ProcessID);
                GetExitCodeProcess(h, a);
                if Integer(TerminateProcess(h, a)) <> 0 then
                  inc(Result);
              end;
              break;
            end;
            found := module32next(ModuleList, pm);
          end;
          closehandle(ModuleList);
        end;
        found := Process32Next(ProcessList, lppe);
      end;
      closehandle(ProcessList);
    end;
      

  6.   

    使用Dos外部命令:TaskList 把它保存到一个文本文件中,然后读取该文本文件中是否有金山词霸这个进程,如果有,就不再运行。