转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
进程、窗口句柄、文件属性、程序运行状态         ★★★★

进程、窗口句柄、文件属性、程序运行状态

作者:闵涛 文章来源:闵涛的学习笔记 点击数:864 更新时间:2009/4/23 18:31:29

uses TLHelp32,PsAPI;

(1)显示进程列表:
procedure TForm1.Button2Click(Sender: TObject);
var lppe: TProcessEntry32;
    found : boolean;
    Hand : THandle;
    P:DWORD;
    s:string;
begin
  ListBox1.Items.Clear ;
  Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  found := Process32First(Hand,lppe);
  while found do
  begin
    s := StrPas(lppe.szExeFile);
    if lppe.th32ProcessID>0 then
      p := lppe.th32ProcessID
    else
      p := 0;
    ListBox1.Items.AddObject(s,pointer(p));//列出所有进程。
    found := Process32Next(Hand,lppe);
  end;
end;

(2)杀死某进程:
procedure TForm1.Button3Click(Sender: TObject);
var lppe: TProcessEntry32;
    found : boolean;
    Hand : THandle;
    P:DWORD;
    sExeFile,sSelect:string;
    killed:boolean;
begin
  p :=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);
  if P<>0 then
  begin
    killed := TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
    if not killed then
       messagebox(self.handle,pchar(sExeFile+''''无法杀死!''''),''''提示'''',MB_OK or MB_ICONWARNING)
    else
      ListBox1.Items.Delete(ListBox1.ItemIndex);
  end;
end;

(3)取得某进程EXE路径:
procedure TForm1.Button8Click(Sender: TObject);  //uses  PSAPI;
var
  h:THandle;  fileName:string; iLen:integer; hMod:HMODULE;cbNeeded,p:DWORD;
begin
  p :=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);
  h  :=  OpenProcess(PROCESS_ALL_ACCESS,  false,  p);      //p  为 进程ID
  if  h  >  0  then
  begin
    if  EnumProcessModules(  h,  @hMod,  sizeof(hMod),  cbNeeded)  then
    begin
       SetLength(fileName,  MAX_PATH);
       iLen  :=  GetModuleFileNameEx(h,  hMod,  PCHAR(fileName),  MAX_PATH);
       if  iLen  <>  0  then
       begin
         SetLength(fileName,  StrLen(PCHAR(fileName)));
         ShowMessage(fileName);
       end;
    end;
    CloseHandle(h);
  end;
end;

(4)取得窗口列表
begin
   ListBox1.Items.Clear ;
   EnumWindows(@EnumWindowsProc, 0);
end;

(5)杀死窗口进程
procedure TForm1.Button6Click(Sender: TObject);
var
  H:THandle;
  P:DWORD;
  s:string;
  killed:boolean;
begin
  s := ListBox1.Items[ListBox1.ItemIndex];
  H:=FindWindow(nil,pchar(s));
  if H<>0 then
  begin
    GetWindowThreadProcessId(H,@P);
    if P<>0 then
      killed:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
    if not killed then
       messagebox(self.handle,pchar(s+''''无法杀死!''''),''''提示'''',MB_OK or MB_ICONWARNING)
    else
      ListBox1.Items.Delete(ListBox1.ItemIndex);
  end;
end;

(6)取得窗口进程路径:
procedure TForm1.Button9Click(Sender: TObject);
var
  H:THandle;  P,cbNeeded: DWORD;  s,fileName:string;
  iLen:integer;   hMod:HMODULE;
begin
  s := ListBox1.Items[ListBox1.ItemIndex];
  H:=FindWindow(nil,pchar(s));

  if H<>0 then
  begin
    GetWindowThreadProcessId(H,@P);
    if P<>0 then
    begin
       h  :=  OpenProcess(PROCESS_ALL_ACCESS,  false,  p);      //p  为 进程ID
       if  h  >  0  then
       begin
         if  EnumProcessModules(  h,  @hMod,  sizeof(hMod),  cbNeeded)  then
         begin
           SetLength(fileName,  MAX_PATH);
           iLen  :=  GetModuleFileNameEx(h,  hMod,  PCHAR(fileName),  MAX_PATH);
           if  iLen  <>  0  then
           begin
                   SetLength(fileName,  StrLen(PCHAR(fileName)));
                   ShowMessage(fileName);
           end;
         end;
         CloseHandle(h);
       end;
    end;
  end;
end;

(7)文件属性:
procedure TForm1.Button1Click(Sender: TObject);
var
  SR: TSearchRec;
  V1, V2, V3, V4: integer  ;
const
  dtFmt:string = ''''YYYY-MM-DD HH:NN:SS'''';
begin
  // ============== 方法一 ==================== //
  if FindFirst(sFileName, faAnyFile, SR) = 0 then
  begin
    Edit1.Text := intToStr(SR.Attr);   //文件属性
    Edit2.Text := intToStr(SR.Size);   //文件大小
    Edit3.Text := FormatDateTime(dtFmt,CovFileDate(SR.FindData.ftCreationTime));   //创建时间
    Edit4.Text := FormatDateTime(dtFmt,CovFileDate(SR.FindData.ftLastWriteTime));  //最后修改时间
    Edit5.Text := FormatDateTime(dtFmt,CovFileDate(SR.FindData.ftLastAccessTime)); //最后访问时间

    if SR.Attr and faHidden <> 0 then
      FileSetAttr(sFileName, SR.Attr-faHidden);

    FindClose(SR);
  end;

  if GetFileVersion(sFileName,V1, V2, V3, V4) then
     Edit7.Text := intToStr(v1)+''''.''''+intToStr(v2)+''''.''''+intToStr(v3)+''''.''''+intToStr(v4);

  // ============== 方法二 ==================== //
  {
  var
  Attrs: Word;
  f: file of Byte;   // 文件大小 必须要 定义为" file of byte" ,这样才能取出 bytes
  size: Longint;

  //文件属性
  Attrs := FileGetAttr(sFileName);

  Edit1.Text := intToStr(Attrs);

  //文件大小
  AssignFile(f, OpenDialog1.FileName);
  Reset(f);
  try
    AssignFile(f, sFileName);
    Reset(f);
    size := FileSize(f);
    Edit2.Text := intToStr(size);
  finally
    CloseFile(f);
  end;
  }
end;

(8)判断程序是否在运行:
procedure TForm1.Button5Click(Sender: TObject);
var PrevInstHandle:Thandle;
  AppTitle:pchar;
begin
 AppTitle := pchar(''''test'''');
 PrevInstHandle := FindWindow(nil, AppTitle);
    if PrevInstHandle <> 0 then begin
      if IsIconic(PrevInstHandle) then
        ShowWindow(PrevInstHandle, SW_RESTORE)
      else
        BringWindowToTop(PrevInstHandle);
      SetForegroundWindow(PrevInstHandle);
    end;
end;


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台