delphi 把exe嵌入到自己的exe中。delphi xe3


本文整理自网络,侵删。

 

下面是一个把exe程序嵌入到我们自己的exe中。开发环境 Delphi XE3 Version 17.0.4625.53395。OS环境WIN7 SP1,由于xe3版本的引用库发生变化。换成其他版本的需要做对应的修改。

 

unit insexe;


interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    pnl1: TPanel;
    btn1: TButton;
    dlgOpen1: TOpenDialog;
    procedure btn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    FhProc: HWND;
  public
    { Public declarations }
  end;

  PProcessWindow = ^TProcessWindow;

  TProcessWindow = record
    TargetProcessID: Cardinal;
    FoundWindow: HWND;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function EnumWindowsProc(Wnd: HWND; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
  WndProcessID: Cardinal;
begin
  GetWindowThreadProcessId(Wnd, @WndProcessID);
  if WndProcessID = ProcWndInfo^.TargetProcessID then
  begin
    ProcWndInfo^.FoundWindow := Wnd;
    Result := False; // stop enumerating since a window found.
  end
  else
    Result := True; // Keep searching
end;

function GetProcessWindow(TargetProcessID: Cardinal): HWND;
var
  ProcWndInfo: TProcessWindow;
begin
  ProcWndInfo.TargetProcessID := TargetProcessID;
  ProcWndInfo.FoundWindow := 0;
  EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo));
  Result := ProcWndInfo.FoundWindow;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  si: STARTUPINFO;
  pi: TProcessInformation;
  bRet: Boolean;
begin
  if not dlgOpen1.Execute then
    Exit;

  FillChar(si, SizeOf(si), 0);
  si.cb := SizeOf(si);

  si.dwFlags := STARTF_USESHOWWINDOW;
  // MUST, otherwise, wShowWindow won't work.
  si.wShowWindow := SW_HIDE;
  // Hide the process Windows, otherwise could be trouble.
  bRet := CreateProcess(nil, PChar(dlgOpen1.FileName), nil, nil, True,
    CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi);

  if not bRet then
    Exit;

  // Wait until child process exits.
  WaitForSingleObject(pi.hProcess, 100); // minor delay

  FhProc := GetProcessWindow(pi.dwProcessID);
  if FhProc > 0 then
  begin
    Winapi.Windows.SetParent(FhProc, pnl1.Handle);
    SetWindowPos(FhProc, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
    ShowWindow(FhProc, SW_SHOW);
  end;

  // Clear up
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if FhProc > 0 then
    PostMessage(FhProc, WM_CLOSE, 0, 0);
end;

end.

相关阅读 >>

Delphi 判断端口(port)是否被占用

Delphi xe10.2 firedac 三种连接

Delphi 压缩与修复access数据库

Delphi 判断文件是否正在被使用

Delphi加密access文件头

vclzip控件的简单使用

Delphi 突破主动防御

Delphi 关闭uac 单元

Delphi xe6 取得app自己的版本号(横跨4个平台)

Delphi 使format输出百分号 %

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...