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

2022-10-26 10:51:11 作者:admin

本文整理自网络,侵删。

 

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

相关阅读 >>

Delphi 禁止改变窗口大小和移动窗口

在 Delphi 中调用 javascript

Delphi ��b以 listbox 的item �� hint

Delphi修改资源文件的代码

Delphi搜索文件夹和文件

Delphi 遍历所有目录和子目录下的文件名(可按后缀名遍历)

Delphi tchart 的使用

Delphi 获取系统当前活动窗口的句柄及对应的进程名

Delphi 时间差函数及部分字符串与日期时间相互转换的函数

Delphi 判断uefi与 legacy bios启动模式

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



在线咨询 拨打电话