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


本文整理自网络,侵删。

 
uses TLHelp32,PsAPI;

 Delphi显示进程列表
 
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;

Delphi杀死某进程
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;


 Delphi取得某进程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;

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

Delphi杀死窗口进程
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;

Delphi取得窗口进程路径
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;


 Delphi获取文件属性
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;

Delphi判断程序是否在运行
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;

杀死自己的进程再重新启动自己            

全部源代码如下(无窗体结构,最后面有说明):
program Project1;


uses
  Winprocs,SysUtils,Tlhelp32;//注意添加单元文件

function KillTask(ExeFileName:string):integer;//杀进程函数KillTask
const PROCESS_TERMINATE = $0001; 
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32; 
begin 
Result :=0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
FProcessEntry32.dwSize := SizeOf(FProcessEntry32); 
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); 



while Integer(ContinueLoop) <> 0 do 
begin 
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or 
            (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then 
Result := Integer(TerminateProcess(
  OpenProcess(PROCESS_TERMINATE,
  BOOL(0), 
  FProcessEntry32.th32ProcessID),0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

要杀死explorer.exe,调用的时候只需要 KillTask('explorer.exe');
例如:
if KillTask('explorer.exe') <> 0 then
  showmessage('结束成功')
else
  showmessage('无法结束');

//调用上面的函数KillTask

begin
  //杀进程,停止程序
  KillTask('abc.exe');//调用函数,杀你的程序abc.exe
  //重新启动程序
  WinExec('abc.exe',SW_SHOW);//然后再重新启动abc.exe
  Exit;
end.
//=========================================
//【说明】编译后的Project1.exe是一个无窗体的EXE程序
//方法:创建一个CONSOLE application 把{$APPTYPE CONSOLE}删了,这个就是一个基本的应用程序框架,
//不显示任何东西,再在里面写东东,程序很小。写在这里共享,怕日后在自己电脑里找不到^_^(笔记,2006.11.23)

相关阅读 >>

Delphi提取网页中的所有链接、点击第n个链接

Delphi xe6 switch元件�繁�w���}

Delphi实现文件防删除

Delphi 隐藏进程代码

Delphi 用 getenvironmentvariable 获取常用系统环境变量

Delphi向imagelist中加入png类型的资源图片

Delphi如何实现浏览文件夹

Delphi 对提交参数编码

Delphi 文件查找findfirst,findnext,findclose

Delphi 常用函数单元 umyfunctions

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...