Title: How to include a Exe-File in your Application and execute it
var
Form1: TForm1;
NOTEPAD_FILE: string;
implementation
{$R *.DFM}
{$R MYRES.RES}
function GetTempDir: string;
var
Buffer: array[0..MAX_PATH] of Char;
begin
GetTempPath(SizeOf(Buffer) - 1, Buffer);
Result := StrPas(Buffer);
end;
// Extract the Resource
function ExtractRes(ResType, ResName, ResNewName: string): Boolean;
var
Res: TResourceStream;
begin
Result := False;
Res := TResourceStream.Create(Hinstance, Resname, PChar(ResType));
try
Res.SavetoFile(ResNewName);
Result := True;
finally
Res.Free;
end;
end;
// Execute the file
procedure ShellExecute_AndWait(FileName: string);
var
exInfo: TShellExecuteInfo;
Ph: DWORD;
begin
FillChar(exInfo, SizeOf(exInfo), 0);
with exInfo do
begin
cbSize := SizeOf(exInfo);
fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
Wnd := GetActiveWindow();
ExInfo.lpVerb := 'open';
lpFile := PChar(FileName);
nShow := SW_SHOWNORMAL;
end;
if ShellExecuteEx(@exInfo) then
begin
Ph := exInfo.HProcess;
end
else
begin
ShowMessage(SysErrorMessage(GetLastError));
Exit;
end;
while WaitForSingleObject(ExInfo.hProcess, 50) WAIT_OBJECT_0 do
Application.ProcessMessages;
CloseHandle(Ph);
end;
// To Test it
procedure TForm1.Button1Click(Sender: TObject);
begin
if ExtractRes('EXEFILE', 'TESTFILE', NOTEPAD_FILE) then
if FileExists(NOTEPAD_FILE) then
begin
ShellExecute_AndWait(NOTEPAD_FILE);
ShowMessage('Notepad finished!');
DeleteFile(NOTEPAD_FILE);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NOTEPAD_FILE := GetTempDir + 'Notepad_FROM_RES.EXE';
end;