⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delphi程序限制系统登陆qq个数.txt

📁 网上收集的一些资料,希望对大家有点帮助,包括:Delphi 基本函数集,Delphi程序限制系统登陆QQ个数,Delphi经常用到的公共代码(Tools.pas),网上流传的 Delphi版 熊猫烧香
💻 TXT
字号:
在文章<利用进程控制应用程序的运行>中,采用的是C#语言实现了一些基本的功能,但是由于生成的文件运行时太占内存(15M左右),且又要在有.net framework的系统中才能运行,所以,我改用Delphi写了一个,完成监控,开机自动启动等功能.下面给出代码,大家一起学习.

--------------------

Main.pas文件 :

--------------------

unit main;

interface

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

type
  Tmainform = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  mainform: Tmainform;
  cur,pos: array[0..4] of integer ;
  cntcur:shortint;

const closeprocess='QQ.EXE';
const close2= 'TIMPLATFORM.EXE';

implementation

{$R *.dfm}

procedure Comp;
var i,j:shortint;
begin
  if cntcur>3 then
    begin
      for I := 0 to cntcur - 1 do
        if (cur[i]<>pos[0]) and (cur[i]<>pos[1]) and (cur[i]<>pos[2]) and (cur[i]<>pos[3]) then
          begin
            TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),cur[i]), 0);
            if messagebox(mainform.handle,'系统检测到该电脑上已登陆了3个QQ,为了避免死机,'
                                   +'该系统将禁止你继续登陆QQ,你可以选择更改用户或先'
                                   +'关闭其他未使用的QQ再试,如果你看不到其它的QQ界面,'
                                   +'他们一定是被隐藏起来了,你可以点击“确定”按钮关闭所有QQ,'
                                   +'也可以点击“取消”按钮不做任何操作。','系统提示',65) =idok then
              begin
                for j := 0 to 4 do
                 begin
                   pos[j]:=0;
                   if cur[j]<>0 then
                     TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),cur[j]), 0);
                 end;
                cntcur:=0;
                exit;
              end;
          end;
    end
  else
    begin
      pos[0]:=cur[0];
      pos[1]:=cur[1];
      pos[2]:=cur[2];
      pos[3]:=cur[3];
    end;
end;

procedure EndProcess(AFileName: string);
const
  PROCESS_TERMINATE=$0001;
var
  ExeFileName: String;
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  cntcur:=0;
  cur[0]:=0;
  cur[1]:=0;
  cur[2]:=0;
  cur[3]:=0;
  cur[4]:=0;
  ExeFileName := AFileName;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
  while integer(ContinueLoop) <> 0 do
    begin
      if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
           UpperCase(ExeFileName))
       or (UpperCase(FProcessEntry32.szExeFile) =
           UpperCase(ExeFileName))) then
           begin
             cur[cntcur]:=FProcessEntry32.th32ProcessID;
             inc(cntcur);
           end;
     if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =close2)
       or (UpperCase(FProcessEntry32.szExeFile) = close2)) then
        TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID), 0);
      ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
    end;
end;

procedure Tmainform.FormCreate(Sender: TObject);
VAR Reg:TRegistry;
begin
if not(FileExists('C:\WINDOWS\system32\Watcher.exe')) then
begin
copyfile(pchar(paramstr(0)),'C:\WINDOWS\system32\Watcher.exe',false); //拷贝自己到系统目录
end;
//修改注册表,开机自启动
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Microsoft\Windows NT\CurrentVersion\Windows', true) then
begin
reg.WriteString('load','C:\WINDOWS\system32\Watcher.exe');
Reg.CloseKey;
end;
Reg.Free;
pos[0]:=0;
pos[1]:=0;
pos[2]:=0;
pos[3]:=0;
pos[4]:=0;
timer1.Enabled:=true;
end;

procedure Tmainform.Timer1Timer(Sender: TObject);
begin    
       endprocess(closeprocess);
       comp; 
end;

end.
-------------------------------

watcher.dpr文件:

-------------------------------

program watcher;

uses
  Forms,
  main in 'main.pas' {mainform};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(Tmainform, mainform);
  Application.ShowMainForm:=false;//*****隐藏主窗体***** 
  Application.Run;
end.

-------------------------------

   我只所以给出dpr文件的代码,是因为使用代码Application.ShowMainForm:=false来隐藏主窗体,最开始,我想在FormCreate事件里添加代码self.hide来实现,但是在FormCreate事件里调用没反应,写在其他函数里放在FormCreate事件里调用也一样没用,于是我在timer1的响应事件里添加self.hide,能实现主窗体的隐藏和任务条的隐藏,但在重启电脑自动加载运行后,主窗体能隐藏,但任务条不能隐藏(我也不晓得怎么回事?),而且在timer事件里重复调用浪费资源. 于是,我采用在工程文件中添加代码Application.ShowMainForm:=false来实现(我也是在网上找到的,呵呵.)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -