资源描述:
《delphi编程获取系统当前进程1》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Delphi编程获取系统当前进程、窗口句柄、文件属性以及程序运行状态Delphi编程获取系统当前进程、窗口句柄、文件属性以及程序运行状态.usesTLHelp32,PsAPI;(1)显示进程列表:procedureTForm1.Button2Click(Sender:TObject);varlppe:TProcessEntry32;found:boolean;Hand:THandle;P:DWORD;s:string;beginListBox1.Items.Clear;Hand:=CreateToolhelp32Snapshot(TH32CS_
2、SNAPALL,0);found:=Process32First(Hand,lppe);whilefounddobegins:=StrPas(lppe.szExeFile);iflppe.th32ProcessID>0thenp:=lppe.th32ProcessIDelsep:=0;ListBox1.Items.AddObject(s,pointer(p));//列出所有进程。found:=Process32Next(Hand,lppe);end;end;(2)杀死某进程:procedureTForm1.Button3Click(Sender
3、:TObject);varlppe:TProcessEntry32;found:boolean;Hand:THandle;P:DWORD;sExeFile,sSelect:string;killed:boolean;beginp:=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);ifP<>0thenbeginkilled:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);ifnotkilledthenmess
4、agebox(self.handle,pchar(sExeFile+'无法杀死!'),'提示',MB_OKorMB_ICONWARNING)elseListBox1.Items.Delete(ListBox1.ItemIndex);end;end;(3)取得某进程EXE路径:procedureTForm1.Button8Click(Sender:TObject);//usesPSAPI;varh:THandle;fileName:string;iLen:integer;hMod:HMODULE;cbNeeded,p:DWORD;beginp:=
5、DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);h:=OpenProcess(PROCESS_ALL_ACCESS,false,p);//p为进程IDifh>0thenbeginifEnumProcessModules(h,@hMod,sizeof(hMod),cbNeeded)thenbeginSetLength(fileName,MAX_PATH);iLen:=GetModuleFileNameEx(h,hMod,PCHAR(fileName),MAX_PATH);ifiLen<>0the
6、nbeginSetLength(fileName,StrLen(PCHAR(fileName)));ShowMessage(fileName);end;end;CloseHandle(h);end;end;(4)取得窗口列表:beginListBox1.Items.Clear;EnumWindows(@EnumWindowsProc,0);end;(5)杀死窗口进程:procedureTForm1.Button6Click(Sender:TObject);varH:THandle;P:DWORD;s:string;killed:boolean;
7、begins:=ListBox1.Items[ListBox1.ItemIndex];H:=FindWindow(nil,pchar(s));ifH<>0thenbeginGetWindowThreadProcessId(H,@P);ifP<>0thenkilled:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);ifnotkilledthenmessagebox(self.handle,pchar(s+'无法杀死!'),'提示',MB_OKorMB_ICO
8、NWARNING)elseListBox1.Items.Delete(ListBox1.ItemIndex);end;end;(6)取得窗口进程路径: