首页 > 代码库 > 把exe嵌入到自己的exe中。delphi xe3

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

 

 

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

unit insexe;interfaceuses  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 searchingend;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 

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