zoukankan      html  css  js  c++  java
  • 绑架其它程序

    -------
    unit Unit1;

    interface

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

    type
      PEnumInfo = ^TEnumInfo;
      TEnumInfo = record
      ProcessID : DWORD;
      HWND : THandle;
    end;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        Label4: TLabel;
        Button2: TButton;
        Label5: TLabel;
        OpenDialog1: TOpenDialog;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;
      hwndOldParent,hwndNotePad : integer;

    implementation

    {$R *.dfm}
    {
    ====================
    用于取得启动程序句柄
    ====================
    }
    function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
    var
      PID : DWORD;
    begin
      GetWindowThreadProcessID(Wnd, @PID);
      Result := (PID <> EI.ProcessID) or
      (not IsWindowVisible(WND)) or
      (not IsWindowEnabled(WND));

      if not result then EI.HWND := WND;
    end;

    {
    =====================
    用于取得启动程序句柄
    =====================
    }
    function FindMainWindow(PID: DWORD): DWORD;
    var
      EI : TEnumInfo;
    begin
      EI.ProcessID := PID;
      EI.HWND := 0;
      EnumWindows(@EnumWindowsProc, Integer(@EI));
      Result := EI.HWND;
    end;

    //用于 NOTEPAD
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      hwndNotePad:=FindWindow(PChar(Edit1.Text),0);
      if hwndNotePad = 0 then
        Showmessage('没找到')
      else begin
        hwndOldParent:=GetParent(hwndNotePad);
        Windows.SetParent(hwndNotePad,handle);
      end;

    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    Windows.SetParent(hwndNotePad,hwndOldParent);
    end;

    用于我的程序自选启动的程序
    procedure TForm1.Button2Click(Sender: TObject);
    var
      SI : TStartupInfo;
      PI : TProcessInformation;
      H : THandle;
      S : String;

      EXEFileName : String;
    begin
      if not OpenDialog1.Execute then exit;
      EXEFileName := OpenDialog1.FileName;
      ZeroMemory(@SI, SizeOf(SI));
      ZeroMemory(@PI, SizeOf(PI));
      SI.cb := SizeOf(SI);
      if CreateProcess(nil,PChar(EXEFileName), nil, nil, FALSE, 0 ,nil,nil, SI, PI) then
      begin
        //注意!
        WaitForInputIdle(PI.hProcess, INFINITE);

        H := FindMainWindow(PI.dwProcessID);
        if H > 0 then
        begin
          SetLength(S, 255);
          GetWindowText(H, PChar(S), 255);
          SetLength(S, StrLen(PChar(S)));
          //绑架
          hwndOldParent:=GetParent(H);
          Windows.SetParent(H,handle);

          ShowMessage(S);
        end;

        CloseHandle(PI.hProcess);
        CloseHandle(PI.hThread);
      end;
    end;

    end.

  • 相关阅读:
    python之路day10命名空间和作用域、函数嵌套,作用域链、闭包
    20200719 千锤百炼软工人第十四天
    20200720 千锤百炼软工人第十五 天
    JavaScript中基本数据类型和引用数据类型的区别
    js 经纬度坐标格式化 度分秒转度 度转度分秒
    export ,export default 和 import 区别 以及用法
    10月28日学习日志
    10月20日学习日志
    10月29日学习日志
    10月27日学习日志
  • 原文地址:https://www.cnblogs.com/martian6125/p/9631569.html
Copyright © 2011-2022 走看看