如何在Windows中获取当前进程列表

编程入门 行业动态 更新时间:2024-10-26 07:41:02
本文介绍了如何在Windows中获取当前进程列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图显示系统上当前正在运行的所有进程. 谁能指导我做到这一点.

I'm trying to show all processes which are currently running on my system. Can any one guide me to do that.

推荐答案

我将WMI用于此任务,因为WMI还可列出我认为Windows API方式无法执行的64位进程.这是使用 Win32_Process 类以查询正在运行的进程的列表:

I would use WMI for this task since WMI can list also 64-bit processes which I think the Windows API way cannot. Here is an example which uses the Win32_Process class to query list of running processes:

[Setup] AppName=My Program AppVersion=1.5 DefaultDirName={pf}\My Program [Code] const WM_SETREDRAW = $000B; var ProcessList: TNewListBox; // this is a substitution for missing BeginUpdate method // of TStrings class procedure WinControlBeginUpdate(Control: TWinControl); begin SendMessage(Control.Handle, WM_SETREDRAW, 0, 0); end; // this is a substitution for missing EndUpdate method // of TStrings class procedure WinControlEndUpdate(Control: TWinControl); begin SendMessage(Control.Handle, WM_SETREDRAW, 1, 0); Control.Refresh; end; function GetProcessNames(Items: TStrings): Integer; var I: Integer; WQLQuery: string; WbemLocator: Variant; WbemServices: Variant; WbemObject: Variant; WbemObjectSet: Variant; begin Result := 0; WbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); WbemServices := WbemLocator.ConnectServer('localhost', 'root\CIMV2'); WQLQuery := 'SELECT Name FROM Win32_Process'; WbemObjectSet := WbemServices.ExecQuery(WQLQuery); if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then begin Items.Clear; for I := 0 to WbemObjectSet.Count - 1 do begin WbemObject := WbemObjectSet.ItemIndex(I); if not VarIsNull(WbemObject) then Items.Add(WbemObject.Name); end; Result := Items.Count; end; end; procedure RefreshButtonClick(Sender: TObject); begin // this try..finally block is used to reduce annoying visual effects // when filling the list box; Inno Setup doesn't publish BeginUpdate, // EndUpdate method pair, hence this home brewn solution WinControlBeginUpdate(ProcessList); try GetProcessNames(ProcessList.Items); finally WinControlEndUpdate(ProcessList); end; end; procedure InitializeWizard; var RefreshBtn: TNewButton; ProcessPage: TWizardPage; begin ProcessPage := CreateCustomPage(wpWelcome, 'Caption', 'Description'); ProcessList := TNewListBox.Create(WizardForm); ProcessList.Parent := ProcessPage.Surface; ProcessList.SetBounds(0, 0, ProcessPage.SurfaceWidth, ProcessPage.SurfaceHeight - 33); RefreshBtn := TNewButton.Create(WizardForm); RefreshBtn.Parent := ProcessPage.Surface; RefreshBtn.Left := 0; RefreshBtn.Top := ProcessPage.SurfaceHeight - RefreshBtn.Height; RefreshBtn.Caption := 'Refresh'; RefreshBtn.OnClick := @RefreshButtonClick; end;

更多推荐

如何在Windows中获取当前进程列表

本文发布于:2023-11-17 11:41:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   如何在   列表   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!