获得当前windows下正在运行的程序名(50分)

  • 主题发起人 主题发起人 dcba
  • 开始时间 开始时间
可惜是用 C 写的, 将就吧
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <conio.h>

int main(int argc, char* argv[])
{
HANDLE hand;
BOOL found;
PROCESSENTRY32 pe;
hand = CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
pe.dwSize = sizeof(PROCESSENTRY32);
found = Process32First(hand,&pe);
while(found){
puts(pe.szExeFile);
found = Process32Next(hand,&pe);
}
getch();
return 0;
}
 
;下面是去年计算机世界上的一段代码,功能是查找出所有的进程,如果点击按钮则会终止进
程,我懒的修改代码符合你的需求,但是肯定可以满足你的要求:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,TLHelp32,
StdCtrls;

type

TProcessInfo=Record
ExeFile:String;
ProcessId:DWORD;
End;
ProcessInfo=^TProcessInfo;

TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ProcessList(var PList:TList);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Current:TList;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ProcessList(var pList:TList);
var p:ProcessInfo;
ok:Bool;
ProcessListHandle:THandle;
ProcessStruct:TProcessEntry32;
Begin
PList:=TList.Create;
PList.Clear;
ProcessListHandle:=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
ProcessStruct.dwSize:=Sizeof(ProcessStruct);
ok:=Process32First(ProcessListHandle,ProcessStruct);
while Integer(ok)<>0 do Begin
new(p);
p.ExeFile:=ProcessStruct.szExeFile;
p.ProcessID:=ProcessStruct.th32ProcessID;
PList.Add(p);
ok:=Process32Next(ProcessListHandle,ProcessStruct);
End;
End;

procedure TForm1.FormCreate(Sender: TObject);
var i:Integer;
p:PRocessInfo;
begin
current:=TList.Create;
Current.Clear;
ListBox1.Clear;
ProcessList(Current);
for i:=0 to Current.Count-1 do Begin
new(p);
p:=Current.Items;
ListBox1.Items.Add(p.ExeFile);
End;
end;

procedure TForm1.Button1Click(Sender: TObject);
var h:THandle;
a:DWORD;
p:PRocessInfo;
begin
if ListBox1.ItemIndex>=0 then Begin
p:=Current.Items[ListBox1.ItemIndex];
h:=openProcess(Process_All_Access,true,p.ProcessID);
GetExitCodeProcess(h,a);
if Integer(TerminateProcess(h,a))<>0 then Begin
ListBox1.Clear;
FormCreate(Self);
End;
End;
end;

end.
 
显示不出来的 3 个头文件是:
windows.h
tlhelp32.h
conio.h
在 windows 98下实现, 在 Windows NT/2000 下不能通过
你看着办吧
 
在 Windows NT/2000 下可以使用psapi
 
var
HWnd: THandle;
Str: PChar;
begin
GetMem(Str, 256);
try
HWnd := GetWindow(GetDesktopWindow, GW_CHILD);
repeat
GetWindowText(HWnd, Str, 255);
if Str[0] <> #0 then
ListBox1.Items.Add(Str);
HWnd := GetWindow(HWnd, GW_HWNDNEXT);
until HWnd = 0;
finally
FreeMem(Str, 256);
end;
end;
 
多人接受答案了。
 
后退
顶部