当前位置:首页 » 软件行业

怎么检查一个进程是否存在?

八月 3, 2007

 发表在: java



各位大哥,怎么检查一个进程是否存在呀。比如c:\ok.exe我想在进程里检查这个程序是否启动,如果没有那么就启动他。

电脑软件技术推荐:

  • WebEXE----将HTML文件转换为EXE的利器[详细内容]
  • 思科推出Cisco CTE 1400系列内容转换引擎[详细内容]
  • 从Java/J2EE到C#的自动转换工具[详细内容]
  • 给Nero加个MP3 Pro转换制作插件[详细内容]
  • ACDSee 4.0对文件批量重命名和格式转换[详细内容]
  • 美国电力转换公司(APC)[详细内容]
  • VC编程实现灰度图像与彩色图像的相互转换[详细内容]
  • C++ 类型转换[详细内容]
  • 用你的手机转换MPEG[详细内容]
  • 日韩逆势强补等离子 憧憬中国市场[详细内容]
  • 相关提议:
    发表者:unsigned)

    uses  
          psapi,   tlhelp32;  
      //   portions   by   project   jedi   www.delphi-jedi.org/  
      const  
          rssystemidleprocess   =   system   idle   process;  
          rssystemprocess   =   system   process;  
       
      function   iswinxp:   boolean;  
      begin  
          result   :=   (win32platform   =   ver_platform_win32_nt)   and  
              (win32majorversion   =   5)   and   (win32minorversion   =   1);  
      end;  
       
      function   iswin2k:   boolean;  
      begin  
          result   :=   (win32majorversion   >=   5)   and  
              (win32platform   =   ver_platform_win32_nt);  
      end;  
       
      function   iswinnt4:   boolean;  
      begin  
          result   :=   win32platform   =   ver_platform_win32_nt;  
          result   :=   result   and   (win32majorversion   =   4);  
      end;  
       
      function   iswin3x:   boolean;  
      begin  
          result   :=   win32platform   =   ver_platform_win32_nt;  
          result   :=   result   and   (win32majorversion   =   3)   and  
              ((win32minorversion   =   1)   or   (win32minorversion   =   5)   or  
              (win32minorversion   =   51));  
      end;  
       
      function   inrunningprocesses(const   filename:string   ;   fullpath:   boolean):   boolean;  
       
          function   processfilename(pid:   dword):   string;  
          var  
              handle:   thandle;  
          begin  
              result   :=   ;  
              handle   :=   openprocess(process_query_information   or   process_vm_read,   false,   pid);  
              if   handle   <>   0   then  
                  try  
                      setlength(result,   max_path);  
                      if   fullpath   then  
                      begin  
                          if   getmodulefilenameex(handle,   0,   pchar(result),   max_path)   >   0   then  
                              setlength(result,   strlen(pchar(result)))  
                          else  
                              result   :=   ;  
                      end  
                      else  
                      begin  
                          if   getmodulebasenamea(handle,   0,   pchar(result),   max_path)   >   0   then  
                              setlength(result,   strlen(pchar(result)))  
                          else  
                              result   :=   ;  
                      end;  
                  finally  
                      closehandle(handle);  
                  end;  
          end;  
       
          function   inlistth:   boolean;  
          var  
              snapprochandle:   thandle;  
              procentry:   tprocessentry32;  
              nextproc:   boolean;  
              localfilename:   string;  
              localresult:boolean;  
          begin  
              result:=false;  
              snapprochandle   :=   createtoolhelp32snapshot(th32cs_snapprocess,   0);  
              localresult   :=   (snapprochandle   <>   invalid_handle_value);  
              if   localresult   then  
                  try  
                      procentry.dwsize   :=   sizeof(procentry);  
                      nextproc   :=   process32first(snapprochandle,   procentry);  
                      while   nextproc   do  
                      begin  
                          if   procentry.th32processid   =   0   then  
                          begin  
                              //   pid   0   is   always   the   "system   idle   process"   but   this   name   cannot   be  
                              //   retrieved   from   the   system   and   has   to   be   fabricated.  
                              localfilename   :=   rssystemidleprocess;  
                          end  
                          else  
                          begin  
                              if   iswin2k   or   iswinxp   then  
                              begin  
                                  localfilename   :=   processfilename(procentry.th32processid);  
                                  if   localfilename   =     then  
                                      localfilename   :=   procentry.szexefile;  
                              end  
                              else  
                              begin  
                                  localfilename   :=   procentry.szexefile;  
                                  if   not   fullpath   then  
                                      localfilename   :=   extractfilename(localfilename);  
                              end;  
                          end;  
                          if   uppercase(localfilename)=uppercase(filename)   then  
                          begin  
                                result:=true;  
                                exit;  
                          end;  
                          nextproc   :=   process32next(snapprochandle,   procentry);  
                      end;  
                  finally  
                      closehandle(snapprochandle);  
                  end;  
          end;  
       
          function   inlistps:   boolean;  
          var  
              pids:   array   [0..1024]   of   dword;  
              needed:   dword;  
              i:   integer;  
              localfilename:   string;  
              enumresult:boolean;  
          begin  
              result:=false;  
              enumresult   :=   enumprocesses(@pids,   sizeof(pids),   needed);  
              if   enumresult   then  
              begin  
                  for   i   :=   0   to   (needed   div   sizeof(dword))   -   1   do  
                  begin  
                      case   pids[i]   of  
                          0:  
                              //   pid   0   is   always   the   "system   idle   process"   but   this   name   cannot   be  
                              //   retrieved   from   the   system   and   has   to   be   fabricated.  
                              localfilename   :=   rssystemidleprocess;  
                          2:  
                              //   on   nt   4   pid   2   is   the   "system   process"   but   this   name   cannot   be  
                              //   retrieved   from   the   system   and   has   to   be   fabricated.  
                              if   iswinnt4   then  
                                  localfilename   :=   rssystemprocess  
                              else  
                                  localfilename   :=   processfilename(pids[i]);  
                              8:  
                              //   on   win2k   pid   8   is   the   "system   process"   but   this   name   cannot   be  
                              //   retrieved   from   the   system   and   has   to   be   fabricated.  
                              if   iswin2k   or   iswinxp   then  
                                  localfilename   :=   rssystemprocess  
                              else  
                                  localfilename   :=   processfilename(pids[i]);  
                              else  
                                  localfilename   :=   processfilename(pids[i]);  
                      end;  
                      if   uppercase(localfilename)   =   uppercase(filename)     then  
                      begin  
                            result:=true;  
                            exit;  
                      end;  
                  end;  
              end;  
          end;  
      begin  
          if   iswin3x   or   iswinnt4   then  
              result   :=   inlistps  
          else  
              result   :=   inlistth;  
      end;  
       
      procedure   tform1.button1click(sender:   tobject);  
       
      begin  
       
            if   inrunningprocesses(c:\ok.exe,true)   then  
                  caption:=true  
            else  
                  caption:=false;  
      end;

    .

    No comments in this entry


    Post a Comment »

    Logged in

    Advertising

    Categories

    相关文章