• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

把exe嵌入到自己的exe中。delphixe3

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 下面是一个把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.

参考:http://hi.baidu.com/wang_yu_136/item/9c6d0ad877a4fa19d90e44fc 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SQLite与DelphiXE4(一)发布时间:2022-07-18
下一篇:
DelphiTStream,TFileStream(文件流)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap