请问如何在B程序中得到当前活动的A程序的 句柄、名称 等信息?(100分)

  • 主题发起人 主题发起人 SeaGod1
  • 开始时间 开始时间
S

SeaGod1

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在B程序中得到当前活动的A程序的 句柄、名称 等信息?
 
请问如何在B程序中得到当前活动的A程序的 句柄、文件名、应用名、路径 等信息?<br>在A程序中可以通过下列方法得到A程序的这些信息,<br>Application.Handle;Application.Title;Application.ExeName;<br>可在B程序中怎么得到这些信息?请高手指教!
 
我通过下面的代码得到当前主窗体的句柄,<br>cWnd := GetForegroundWindow;<br>pWnd := GetParent(cWnd);<br>但是怎样能得到该应用程序的句柄,怎么得到它的 文件名。
 
有没有高手能解答我的问题?
 
我以前写的一个类,完全合你的要求,从但是我不保证所有程序都能取得信息,也不保证程序的健壮性。<br>-----------------------------------------------------<br>unit ProcessFunc;<br><br>interface<br>uses<br> &nbsp;Windows,Classes,SysUtils,Types,TLHelp32;<br>Type<br> &nbsp;TProcessListItem= class<br> &nbsp; &nbsp; &nbsp;Size: Longword ; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Longword :无符号32位整数。<br> &nbsp; &nbsp; &nbsp;Usage: Longword ;<br> &nbsp; &nbsp; &nbsp;ProcessID: Longword ; &nbsp; &nbsp; &nbsp; &nbsp; // this process<br> &nbsp; &nbsp; &nbsp;DefaultHeapID: Longword ;<br> &nbsp; &nbsp; &nbsp;ModuleID: Longword ; &nbsp; &nbsp; &nbsp; &nbsp; // associated exe<br> &nbsp; &nbsp; &nbsp;Threads: Longword ;<br> &nbsp; &nbsp; &nbsp;ParentProcessID: Longword ; &nbsp; // this process's parent process<br> &nbsp; &nbsp; &nbsp;PriClassBase: Longint; &nbsp; &nbsp; &nbsp; &nbsp;// Base priority of process's threads<br> &nbsp; &nbsp; &nbsp;Flags: Longword ;<br> &nbsp; &nbsp; &nbsp;ExeFile:string;<br> &nbsp; &nbsp; &nbsp;ModuleList:Tstrings;<br> &nbsp;private<br> &nbsp; &nbsp;miHandle:THandle;<br> &nbsp; &nbsp;function GetHandle: THandle;<br> &nbsp;published<br> &nbsp; &nbsp; &nbsp;property Handle :Thandle &nbsp;read GetHandle;<br> &nbsp; &nbsp; &nbsp;constructor Create;<br> &nbsp; &nbsp; &nbsp;destructor Destroy;override;//销毁器<br> &nbsp; &nbsp; &nbsp;Function GetModuleList:TStrings;<br> &nbsp; &nbsp; &nbsp;Function Kill:integer;<br> &nbsp;end;<br> &nbsp;TProcessList = Class<br> &nbsp; &nbsp; &nbsp;Count:integer;<br> &nbsp; &nbsp; &nbsp;Hand : THandle;<br> &nbsp; &nbsp; &nbsp;ProcessItem :array [0..255] of TprocessListItem;<br> &nbsp; &nbsp; &nbsp;MyProcess:TprocessListItem;<br> &nbsp; &nbsp; &nbsp;constructor Create;<br> &nbsp; &nbsp; &nbsp;destructor Destroy;override;//销毁器<br> &nbsp; <br><br> &nbsp; &nbsp; &nbsp;Function GetList :integer;<br><br> &nbsp;end;<br> &nbsp;<br> &nbsp;Function KillProcess(FormTitle:string):integer; &nbsp;overload;<br> &nbsp;Function KillProcess(Handle:THandle):integer; &nbsp;overload;<br> &nbsp;Function KillProcess2(ProcessID:DWORD):integer; &nbsp;overload;<br><br><br>implementation<br><br>Function KillProcess(FormTitle:string):integer;<br>var<br> &nbsp;H:THandle;<br> &nbsp;P:DWORD;<br> &nbsp;TerminateResult:LongBool;<br>begin<br> &nbsp;H:=FindWindow(nil,PAnsiChar(FormTitle));<br> &nbsp;if H&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowThreadProcessId(H,@P);<br> &nbsp; &nbsp;if P&lt;&gt;0 then<br> &nbsp; &nbsp;TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);<br> &nbsp; &nbsp;if not TerminateResult then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; //GetLastError();<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br>Function KillProcess(Handle:THandle):integer;<br>var<br> &nbsp;P:DWORD;<br> &nbsp;TerminateResult:LongBool;<br>begin<br> &nbsp;if Handle&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;GetWindowThreadProcessId(Handle,@P);<br> &nbsp; &nbsp;if P&lt;&gt;0 then<br> &nbsp; &nbsp;TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);<br> &nbsp; &nbsp;if not TerminateResult then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := -1;<br> &nbsp; &nbsp; &nbsp;//GetLastError();<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp;end;<br> &nbsp;end else begin<br> &nbsp; &nbsp; &nbsp;Result := -1;<br> &nbsp;end;<br><br> &nbsp;Result:=0;<br>end;<br><br>Function KillProcess2(ProcessID:DWORD):integer;<br>var<br> &nbsp;TerminateResult:LongBool;<br>begin<br> &nbsp; &nbsp;TerminateResult:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,ProcessID),$FFFFFFFF);<br> &nbsp; &nbsp;if not TerminateResult then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := -1;<br> &nbsp; &nbsp; &nbsp;//GetLastError();<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result :=0;<br>end;<br><br>{ TProcessListItem }<br><br>constructor TProcessListItem.Create;<br>begin<br> &nbsp; &nbsp;ModuleList:= TStringList.Create;<br>end;<br><br>destructor TProcessListItem.Destroy;<br>begin<br> &nbsp; &nbsp;ModuleList.Destroy;<br> &nbsp; &nbsp;if self.Handle &gt;0 then CloseHandle(self.Handle );<br> &nbsp;inherited;<br>end;<br><br>function TProcessListItem.GetHandle: THandle;<br>begin<br> &nbsp; &nbsp;if self.miHandle =0 then<br> &nbsp; &nbsp;self.miHandle :=CreateToolHelp32SnapShot(TH32CS_SNAPModule,self.ProcessID );<br> &nbsp; &nbsp;result:=self.miHandle ;<br><br>end;<br><br>function TProcessListItem.GetModuleList: Tstrings;<br>var<br> &nbsp;me:MODULEENTRY32;<br> &nbsp;found:boolean;<br> &nbsp;strings:string;<br><br>begin<br> &nbsp; &nbsp;self.ModuleList.Clear;<br> &nbsp; &nbsp;me.dwSize:=sizeof(ModuleEntry32);<br><br> &nbsp; &nbsp;found:=Module32First(self.Handle,me);<br> &nbsp; &nbsp;while &nbsp;found do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if me.th32ProcessID =self.ProcessID &nbsp;then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.ModuleList.Add( me.szExePath);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;found:=Module32Next(self.Handle ,me);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;CloseHandle(self.miHandle);<br> &nbsp; &nbsp;self.miHandle :=0;<br> &nbsp; &nbsp;Result:=ModuleList;<br><br><br>end;<br><br>function TProcessListItem.Kill: integer;<br>begin<br> &nbsp; &nbsp;if TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,self.ProcessID),$FFFFFFFF)<br> &nbsp; &nbsp;then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 0;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result :=-1;<br>end;<br><br>{ TProcessList }<br><br>constructor TProcessList.Create;<br>begin<br><br>end;<br><br>destructor TProcessList.Destroy;<br>var<br> &nbsp; &nbsp;i:integer;<br>begin<br> &nbsp;for i := 0 to self.Count -1 do self.ProcessItem.Destroy;<br> &nbsp;inherited;<br>end;<br><br>function TProcessList.GetList: integer;<br>var<br> &nbsp; &nbsp;lppe: TProcessEntry32;<br> &nbsp; &nbsp;found : boolean;<br><br> &nbsp; &nbsp;iCount,i:integer;<br> &nbsp; &nbsp;//me:MODULEENTRY32;<br>begin<br> &nbsp;for i := 0 to self.Count -1 do self.ProcessItem.Destroy;<br> &nbsp;iCount:= 0;<br> &nbsp;if hand =0 then<br> &nbsp;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);<br> &nbsp;found := Process32First(Hand,lppe);<br> &nbsp;while found do //<br> &nbsp;begin<br> &nbsp; &nbsp;self.ProcessItem[iCount]:= TProcessListItem.Create;<br><br> &nbsp; &nbsp;with self.ProcessItem[iCount] do<br> &nbsp; &nbsp;begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp;Size := lppe.dwSize;<br> &nbsp; &nbsp; &nbsp; &nbsp;Usage:= lppe.cntUsage ;<br> &nbsp; &nbsp; &nbsp; &nbsp;ProcessID:= lppe.th32ProcessID ;<br> &nbsp; &nbsp; &nbsp; &nbsp;DefaultHeapID:= lppe.th32DefaultHeapID ;<br> &nbsp; &nbsp; &nbsp; &nbsp;ModuleID:= lppe.th32ModuleID ;<br> &nbsp; &nbsp; &nbsp; &nbsp;Threads:= lppe.cntThreads ;<br> &nbsp; &nbsp; &nbsp; &nbsp;ParentProcessID:= lppe.th32ParentProcessID ;<br> &nbsp; &nbsp; &nbsp; &nbsp;PriClassBase:= lppe.pcPriClassBase ;<br> &nbsp; &nbsp; &nbsp; &nbsp;Flags:= lppe.dwFlags ;<br> &nbsp; &nbsp; &nbsp; &nbsp;ExeFile := StrPas(lppe.szExeFile );<br><br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;inc(iCount);<br> &nbsp; &nbsp;found := Process32Next(Hand,lppe);<br> &nbsp;end;<br><br> &nbsp;self.Count := iCount;<br><br>end;<br><br><br>end.
 
Thank you very much!<br>问题解决了
 
后退
顶部