Delphi编程获取系统当前进程、窗口句柄、文件属性以及程序运行状态.

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;

Delphi编程获取系统当前进程、窗口句柄、文件属性以(转)的更多相关文章

  1. Android开发之获取系统所有进程信息。

    最近在做一个app,有一个进程管理模块用于管理系统中正在运行的进程,并且可以关闭进程进行加速手机的功能,基本把它实现了出来.界面的效果都是自己写的,虽然有BUG,但是基本上能满足需求,后期我会改进BU ...

  2. Android 5.0以上获取系统运行进程信息

    在Android 5.0以上系统,调用getRunningAppProcesses 方法返回的列表为空,这是因为谷歌考虑到安全原因,已经把这个方法移除掉了, 那以后要获取系统运行的后台进程这个方法用不 ...

  3. Delphi 获取系统当前进程、窗口句柄、文件属性以及程序运行状态

    uses TLHelp32,PsAPI;(1)显示进程列表: procedure TForm1.Button2Click(Sender: TObject); var lppe: TProcessEnt ...

  4. DELPHI FMX 获取系统版本 ANDROID IOS通用

    引用System.sysutils function getOSInfo:String; begin result:= fomrat('%s:%d.%d', TOSVersion.Name,TOSVe ...

  5. Delphi获取其它进程窗口句柄的3种方法

    本文主要跟大家介绍Delphi中获取其它进程的窗口句柄,在Delphi中获取其它进程的窗口句柄,绝大部分人首先想到的会使用:FindWindow或者用GetWindow来遍历查找,如: handle ...

  6. Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)

    本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...

  7. MFC下获取系统内存和当前进程的内存使用情况

    GlobalMemoryStatusEx来获取系统的内存使用情况 GetProcessMemoryInfo获取某个进程的内存使用情况.和任务管理器看到的是一样的. 具体代码如下: void GetSy ...

  8. C++常见编程--获取当前系统时间

    C++常见编程--获取当前系统时间 文章首发https://www.cppentry.com 本文主要使用time() 及strftime() 函数 C++系统和时间相关的函数基本上都是使用C语言提供 ...

  9. VC++编程中获取系统时间

    <span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...

随机推荐

  1. box-shadow中的理解(bootstrap)

    刚研究了bootstrap中css里面的源码,找到了表单(form)中关于输入框的一些设置,根据要求,label标签和input标签需要一起使用,(屏幕阅读器中不能单独辨认input),如需隐藏lab ...

  2. UE4 Android打包 问题 记录笔记

    问题一:error: expression result unused [-Werror,-Wunused-value] 虽然看了输出日志知道了这行沉余代码删掉就行,但是不是很懂这个地方报错意义. 问 ...

  3. 软件工程总结(Final)

    又到了学期末了,此时此刻软件工程课业接近尾声了.时间过得太快,仿佛昨天这学期的课才开始, 开学时,老师让我们提出了不少对这门课的问题和疑惑,通过一学期的努力学习,我可以试着解开我自己的疑问了. 下面是 ...

  4. Reconstruct Itinerary

    Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...

  5. 解决织梦DEDEcms指定arclist的特定ID排序的方法

    转载网址:http://blog.hrseo.net/xuexi/184.html 替换/include/taglib/arclist.lib.php这个文件,下载链接: http://pan.bai ...

  6. Sublime Text2 快捷键汇总

    一个好的编辑器,能大大提高编程的效率.如果能熟知软件的快捷键,那更能让你得心印手.这些内容都是我网上和自己实际使用过程中所收集而来的,在网络上应该也算比较全面的了吧.欢迎大家补充,我也会在以后慢慢添加 ...

  7. OpenMP编程总结表

    本文对OpenMP 2.0的全部语法——Macro(宏定义).Environment Variables(环境变量).Data Types(数据类型).Compiler Directives(编译指导 ...

  8. LintCode MinStack

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  9. Tomcat的ISO-8859-1

    Tomcat的默认编码时ISO8859-1,有些老工程,遗留项目很可能没改这块,这样写代码时如果传输中文,服务器收到的就可能是乱码. 昨天就被郁闷了1小时,左右都不通. 后来发现Android里的字符 ...

  10. ased

    1.document.write(""); 输出语句 2.JS 中的注释为 // 3. 传统的 HTML 文档顺序是 :document->html->(head,bo ...