Title: Adding an application into yours
Question: How can I put an EXE file into my application and run it?
Answer:
1) Create file "hearts.rc"
2) Insert following text in that file:
"TESTFILE EXEFILE \Mshearts.exe"
Mshearts.exe - Mshearts from Windows
3) Compile this file with brcc32.exe, which is located in your
"\Borland\Delphi\Bin" directory by writing
following command: "brcc32 -32 \hearts.rc"
Now you should have file "hearts.RES". How to add it to your application:
1) Copy "hearts.RES" to your project directory
2) Add ShellApi to USES
To global vars add "Hearts:String";
Next add to project:
{$R HEARTS.RES}
function GetTempDir:String;
var
Buffer: array[0..MAX_PATH] OF Char;
begin
GetTempPath(Sizeof(Buffer)-1,Buffer);
result := StrPas(Buffer);
end;
procedure ExtractRes(ResType, ResName, ResNewName : String);
var
Res : TResourceStream;
begin
Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
try
Res.SavetoFile(ResNewName);
finally
Res.Free;
end;
end;
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;
3) Add a Button on the form and to its OnClick event put this:
procedure TForm1.Button1Click(Sender: TObject);
begin
ExtractRes('EXEFILE','TESTFILE',Hearts);
if FileExists(Hearts) then
begin
ShellExecute_AndWait(Hearts);
ShowMessage('Hearts finished');
DeleteFile(Hearts);
end;
end;
4) To OnCreate event of form put this:
procedure TForm1.FormCreate(Sender: TObject);
begin
Hearts := GetTempDir + 'Hearts_FROM_RES.EXE';
end;
5) Run program and click the button
That's all!