捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  沪ICP备05001939号 DELPHI盒子 | 盒子论坛 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 论坛检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
盒子资源分类
全部展开 - 全部合拢
屏幕录像专家 V7.5 命令行控制
关键字:邮槽 , 线程, 命令行,窗体枚举
来 自:原创
平 台:Win2K/2003/NT/XP 下载所需:0 火柴
深浅度:初级 完成时间:2013/6/22
发布者:louemusic (奖励50火柴) 发布时间:2013/6/27
编辑器:Delphi7 语  种:简体中文
分 类:杂项 下载浏览:202/7293
加入到我的收藏
下载错误报错
登陆以后才能下载
 用户名:
 密 码:
自动登陆(30天有效)
图片如果打不开,说明流量不够了,请稍候下载……
本程序同时作为控制  和受控端. 使用 邮槽 , 线程, 命令行,窗体枚举等


program Nurse;

uses
  Windows,
  Messages,
  SysUtils,
  Registry,
  Classes;
const
  SHostWindowName1 = '屏幕录像专家 V7.5';
  SHostWindowName = '屏幕录像专家';
  SRunKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Run';
  SSettingKey = 'SOFTWARE\Microsoft\Windows\cfgmon';
  SSettingHostPath = 'HostPath';
  SAppName = 'ctgmon';
  CmdDataLen = 1024;
  
  cSetCapture = $01;
  cSetCapPath = $02;
  cRestartHost = $04;
  cExit = $08;
  cHide = $10;
  cForce = $20;
  cHostPath = $40;

  cCapState = $1;
  cAppID = $C0000000;

type
  PFindWindowData = ^TFindWindowData;
  TFindWindowData = record
    WindowName: array[0..255] of Char;
    Wnd: HWND;
  end;

  TCommand = packed record  
    Cmd: Integer;
    Data: array[0..CmdDataLen] of Byte;
  end;

  PHostPathData = ^THostPathData;
  THostPathData = packed record
    Size: Cardinal;
    Data: Byte;
  end;

procedure EmKeyDown(const vk: Cardinal);
var
  Input: TInput;
begin
  with Input, ki do begin
    Itype := INPUT_KEYBOARD;
    wVk := vk;
    time := 0;
    wScan := 0;
    dwFlags := 0;
    dwExtraInfo := GetMessageExtraInfo;
  end;
  SendInput(1, Input, sizeof(Input));
end;

procedure EmKeyUp(const vk: Cardinal);
var
  Input: TInput;
begin
  with Input, ki do begin
    Itype := INPUT_KEYBOARD;
    wVk := vk;
    time := 0;
    wScan := 0;
    dwFlags := KEYEVENTF_KEYUP;
    dwExtraInfo := GetMessageExtraInfo;
  end;
  SendInput(1, Input, sizeof(Input));
end;

function CreateTask(const FileName: string): Boolean;
var
  Start: TStartupInfo;
  Process: TProcessInformation;
begin
  FillChar(Start, sizeof(Start), 0);
  FillChar(Process, sizeof(Process), 0);
  Start.cb := sizeof(Start);
  Start.dwX := 0;
  Start.dwY := 0;
  Start.dwXSize := 0;
  Start.dwYSize := 0;
  Start.wShowWindow := 0;
  Start.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESIZE or STARTF_USEPOSITION;
  Result := CreateProcess(PChar(FileName), nil, nil, nil, false, CREATE_DEFAULT_ERROR_MODE, nil, nil, Start, Process);
end;

function E(h: HWND; l: Integer): LongBool; stdcall;
var
  Buff: array[0..255] of Char;
  PWinData: PFindWindowData;
begin
  Result := False;
  GetWindowText(h, Buff, 255);
  PWinData := PFindWindowData(l);
  if AnsiCompareStr(Buff, PWinData.WindowName)=0 then begin
    PWinData.Wnd := h;
    Result := false;
  end;
  Result := true;
end;

function XFindWindow(const WindowName: string; Timeout: Integer): HWND;
var
  h: HWND;
  WinData: TFindWindowData;
  TheTime: Cardinal;
begin
  Result := 0;
  FillChar(WinData, sizeof(WinData), 0);
  StrLCopy(WinData.WindowName, @WindowName[1], 255);
  TheTime := GetTickCount;
  while (Result = 0) and(GetTickCount - TheTime < Timeout)do begin
    if EnumWindows(@E, Integer(@WinData)) then
      Result := WinData.Wnd;
  end;
end;

function RegReadString(const Key, Entry: string): string;
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    Reg.OpenKey(Key, true);
    Result := Reg.ReadString(Entry);
  finally
    Reg.Free;
  end;
end;

procedure RegWriteString(const Key, Entry, Value: string);
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    Reg.OpenKey(Key, true);
    Reg.WriteString(Entry, Value);
  finally
    Reg.Free;
  end;
end;

function SetAutorun(AutoRun: Boolean): Boolean;
const
  FileLen = 1024;
var
  Reg: TRegistry;
  FileName: array[0..FileLen -1] of Char;
begin
  Result := false;
  GetModuleFileName(0, FileName, FileLen);
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    Reg.OpenKey(SRunKey, true);
    if AutoRun then begin
      Reg.WriteString(SAppName, FileName);
      Result := true;
    end else
      Result := Reg.DeleteValue(SAppName);
  finally
    Reg.Free;
  end;
end;

function GetState(Wnd: HWND): Cardinal;
begin
  Result := GetWindowLong(Wnd, GWL_USERDATA);
end;

procedure SetState(Wnd: HWND; State: Cardinal);
begin
  SetWindowLong(Wnd, GWL_USERDATA, State);
end;

function RunHost(const DefPath: string): HWND;
begin
  Result := XFindWindow(SHostWindowName1, 10);
  while (Result = 0)do begin
    if not CreateTask(DefPath) then
      Exit;

    Result := XFindWindow(SHostWindowName, 200);
    SetWindowPos(Result, 0, -100,-100, 0,0, SWP_HIDEWINDOW);

    Result := XFindWindow(SHostWindowName1, 10);
    SetWindowPos(Result, 0, -100,-100, 0, 0, SWP_HIDEWINDOW);
    
    MoveWindow(Result, -100, -100, 0,0,true);
    EnableWindow(Result, false);
    Sleep(500);
  end;
end;

function IsFirstInstance: Boolean;
begin
  CreateMutex(nil, false, SAppName);
  Result := GetLastError <> ERROR_ALREADY_EXISTS;
end;

const
  SServicePipeName = '\\.\mailslot\cfgmon';

type
  TCmd = (cmdSetCapture, cmdSetCapPath, cmdRestartHost, cmdHide, cmdForce, cmdHostPath);
  TCmds = set of TCmd;
  TWorker = class(TThread)
  private
    hWnd, hOldWnd: HWND;
    FCmds: TCmds;
    FHostPath: string;
  protected
    procedure Execute; override;
    procedure SetCapture;
    procedure QuitHost;
    procedure ForceCapture;
  public
    constructor Create(State: Boolean);
    procedure ResponseCommand(const Cmd: TCommand);
  public
    property Cmds: TCmds read FCmds write FCmds;
    property HostPath: string read FHostPath write FHostPath;
  end;

  TMailslot = class
  private
    hSlot: THandle;
  protected
    destructor Destroy; override;
  public
    constructor Create(const Name: string; NewInstance: Boolean = true);
  public
    function Read(var Buff; Size: Cardinal): Cardinal;
    function Write(const Buff: Pointer; Size: Cardinal): Cardinal;
    function GetMsgLen: Cardinal;
  end;

{ TMailslots }

constructor TMailslot.Create(const Name: string; NewInstance: Boolean = true);
begin
  if NewInstance then
    hSlot := CreateMailslot(PChar(Name), 0, MAILSLOT_WAIT_FOREVER, nil)
  else
    hSlot := CreateFile(PChar(Name), GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
end;

destructor TMailslot.Destroy;
begin
  CloseHandle(hSlot);
  inherited;
end;

function TMailslot.GetMsgLen: Cardinal;
var
  Msgs: Cardinal;
  bRet: LongBool;
begin
  bRet := GetMailslotInfo(hSlot, nil, Result, @Msgs, nil);
  if Result = MAILSLOT_NO_MESSAGE then
    Result := 0;
end;

function TMailslot.Read(var Buff; Size: Cardinal): Cardinal;
begin
  ReadFile(hSlot, Buff, Size, Result, nil);
end;

function TMailslot.Write(const Buff: Pointer; Size: Cardinal): Cardinal;
begin
  WriteFile(hSlot, Buff^, Size, Result, nil);
end;

{ TWorker }

constructor TWorker.Create(State: Boolean);
begin
  inherited Create(State);
  Priority := tpIdle;
end;

procedure TWorker.Execute;
begin
  FHostPath := RegReadString(SSettingKey, SSettingHostPath);
  while not Terminated do
  try
    hWnd := RunHost(FHostPath);
    if hOldWnd <> hWnd then begin
      hOldWnd := hWnd;
      ForceCapture;
      Continue;
    end;

    if cmdSetCapture in FCmds then begin
      Exclude(FCmds, cmdSetCapture);
      SetState(hWnd, not (GetState(hWnd) and cCapState));
      SetCapture;
    end;

    if cmdForce in FCmds then begin
      Exclude(FCmds, cmdForce);
      ForceCapture;
    end;

    if cmdSetCapPath in FCmds then begin
      Exclude(FCmds, cmdSetCapture);
    end;
    
    if cmdRestartHost in FCmds then begin
      Exclude(FCmds, cmdRestartHost);
      QuitHost;
    end;

    if cmdHide in FCmds then begin
      Sleep(20);
      Exclude(FCmds, cmdHide);
      ShowWindow(hWnd, SW_HIDE);
    end;

    Sleep(100);
  except
  end;
end;

procedure TWorker.ForceCapture;
begin
  if (GetState(hOldWnd) and cCapState) = 0 then begin
    SetState(hOldWnd, cCapState);
    SetCapture;
  end;
end;

procedure TWorker.QuitHost;
begin
  if not IsWindowVisible(hWnd) then
    SetCapture;
  PostMessage(hWnd, WM_QUIT, 0, 0);
end;

procedure TWorker.SetCapture;
begin
  EmKeyDown(VK_CONTROL);
  EmKeyDown(VK_MENU);
  EmKeyDown(VK_LSHIFT);
  EmKeyDown(VK_F2);

  EmKeyUp(VK_CONTROL);
  EmKeyUp(VK_MENU);
  EmKeyUp(VK_LSHIFT);
  EmKeyUp(VK_F2);
end;

function ParseParam(PCmd, PSwitch: PChar; out Value: string): Boolean;
var
  sPos: Integer;
  C, P: PChar;
begin
  Result := false;
  sPos := Pos(PSwitch, PCmd);
  if sPos = 0 then
    Exit;
    
  P := PCmd + sPos + 2;
  while (P^<>#0)and(P^=' ') do Inc(P);
  C := P;
  while (C^<>#0)and(C^<>' ') do Inc(C);
  SetLength(Value, C - P);
  StrLCopy(PChar(Value), P, C-P);
  Result := true;
end;

procedure ParseCmdLine(var Cmd: TCommand);
var
  P: PChar;
  Value: string;
  Size: Cardinal;
begin
  P := GetCommandLine;
  if Pos('-c', P) > 0 then Cmd.Cmd := cSetCapture;

  if Pos('-r', P) > 0 then Cmd.Cmd := Cmd.Cmd or cRestartHost;

  if Pos('-x', P) > 0 then Cmd.Cmd := Cmd.Cmd or cExit;

  if Pos('-h', P) > 0 then Cmd.Cmd := Cmd.Cmd or cHide;

  if Pos('-f', P) > 0 then Cmd.Cmd := Cmd.Cmd or cForce;

  if ParseParam(P, '-p', Value) then begin
    Cmd.Cmd := Cmd.Cmd or cHostPath;
    RegWriteString(SSettingKey, SSettingHostPath, Value);
    Size := Length(Value);
    Move(Size, Cmd.Data, 4);
    Move(Value[1], Cmd.Data[4], Size);
  end;
end;

procedure TWorker.ResponseCommand(const Cmd: TCommand);
function ParseValue: string;
var
  Size: Cardinal;
begin
  Move(Cmd.Data, Size, 4);
  if Size < CmdDataLen then begin
    SetLength(Result, Size);
    Move(Cmd.Data[4], Result[1], Size);
  end;
end;
begin
  if Cmd.Cmd and cSetCapture <> 0 then
    FCmds := FCmds + [cmdSetCapture];

  if Cmd.Cmd and cRestartHost <> 0 then
    FCmds := FCmds + [cmdRestartHost];

  if Cmd.Cmd and cHide <> 0 then
    FCmds := FCmds + [cmdHide];

  if Cmd.Cmd and cForce <> 0 then
    FCmds := FCmds + [cmdForce];

  if Cmd.Cmd and cHostPath <> 0 then begin
    FHostPath := ParseValue;
    FCmds := FCmds + [cmdHostPath];
  end;
end;


var
  Slot: TMailslot;
  Worker: TWorker;
  Cmd: TCommand;
  FirstInst: Boolean;
begin
  SetAutorun(true);
  FirstInst := IsFirstInstance;
  Slot := TMailslot.Create(SServicePipeName, FirstInst);

  if FirstInst then begin
    Worker := TWorker.Create(False);
    while True do begin
      if Slot.GetMsgLen > 0 then begin
        Slot.Read(Cmd, sizeof(Cmd));
        Worker.ResponseCommand(Cmd);
        if Cmd.Cmd and cExit <> 0 then
          Break;
      end;
      Sleep(10);
    end;
    Worker.Terminate;
    Worker.Free;
  end else begin
    if ParamCount > 0 then begin
      ParseCmdLine(Cmd);
      Slot.Write(@Cmd, sizeof(Slot));
    end;
  end;
  
  Slot.Free;
end.
Google
 
本站原创作品,未经作者许可,严禁任何方式转载;转载作品,如果侵犯了您的权益,请联系我们
龙脉加密锁 15元起 Grid++Report 报表 申请支付@网
 相关文章
没有相关文章
相关评论
共有评论0条
我要发表评论 查看全部评论
 
  DELPHI盒子版权所有 1999-2023 V4.01 粤ICP备10103342号-1 更新RSS列表