Link to home
Start Free TrialLog in
Avatar of peruzzi
peruzzi

asked on

Run another application in my application form !!!!

How can I run another application in the form of my application?
I mean that I want the another application to be shown in mine form(my program)
inside the form .

I am waiting for your answers ...... thanks
Avatar of shaneholmes
shaneholmes

Look at the Windows.SetParent(WindowHandle, Panel1.Handle);

This code was originally posted by DragonSlayer:
https://www.experts-exchange.com/questions/20682510/how-open-programa-in-delphi-form-area.html

uses
  LecsExec;

procedure TForm1.Button1Click(Sender: TObject);
var
  WindowHandle: THandle;
begin
  if not WinExecAndGetHWnd(Calc.exe', WindowHandle) then
    raise Exception.Create('Unable to execute programme!');
  Windows.SetParent(WindowHandle, Panel1.Handle);
end;


use this unit:

unit LecsExec;

interface

uses
 Windows;

 
FUNCTION WinExecAndGetHWnd(const fn: String; var hNewWnd : THandle) : Boolean;

implementation

VAR
 ProcessInformation : TProcessInformation;
 WndHandle  : hWnd;
 Identifier : LongInt;

function EnumFunc(H : HWnd; Ignored : LongInt) : Bool; StdCall;
VAR TheID : LongInt;
BEGIN
 {Find the window matching the process ID}
 GetWindowThreadProcessID(H, @TheID);
 IF TheID = Identifier THEN BEGIN
   WndHandle := H;
   Result    := False;
   END
 ELSE
   Result := True;
END;

FUNCTION WinExecAndGetHWnd(const fn: String; var hNewWnd : THandle) : Boolean;
VAR
 StartupInfo : TStartupInfo;
begin
 WndHandle := 0;

  FillChar(StartupInfo, SizeOf(StartupInfo), 0);
 StartupInfo.CB := SizeOf(StartupInfo);

  IF CreateProcess(
   NIL,                  
   PChar(fn),        
   NIL,
   NIL,
   False,
   DETACHED_PROCESS OR NORMAL_PRIORITY_CLASS,
   NIL,
   NIL,
   StartupInfo,
   ProcessInformation) THEN BEGIN

    Identifier := ProcessInformation.dwProcessID;
   CloseHandle(ProcessInformation.hThread);
   WaitForInputIdle(ProcessInformation.hProcess, 10000); {let process start!}
   EnumWindows(@EnumFunc, Identifier);

    END;

  if WndHandle = 0 then
   Result := False
 else begin
   Result := True;
   hNewWnd := WndHandle;
   end;

end;

end.
Avatar of peruzzi

ASKER

Hi shaneholmes

I try your code ,after adding this code to the button :
if opendialog1.execute then
 if not WinExecAndGetHWnd(opendialog1.FileName, WindowHandle) then
  .
  .
so I can choose any application I want easly by using opendialog component
but the most of programs which I want to run , was running normaly ( not inside my
program' form) .
for example : MPowerpoint , adobe acrobat reader, media player , winamp ....etc
but I notic that all these program have many windows when they open
I think this is the problem
I am waiting for your solution .

thanks

After you call the  WinExecAndGetHWnd then you pass the handle to the SetParent method

 Windows.SetParent(WindowHandle, Panel1.Handle);

 did you do that?


Shane
Avatar of peruzzi

ASKER

Hi...
yes, I do .
and the code success with some programs , but not with all
and what I need is to success with all programs

I hope you find the solution.

thanks
I dont get it - not with all? Which ones do you have success with and ones you dont?

Shane
Avatar of peruzzi

ASKER

success with ( Microsoft word ) and any program have only direct form(one form at running).
not success with : MPowerpoint , adobe acrobat reader, media player , winamp ,internet explorer .

for example when I run microsoft powerpoint : the first windows of the program
(window of choices) showen inside my program' form but the main program( microsoft powerpoint) showen outside my form .

so I need the code that show the program and all its forms inside my program' form

I will increase the question' points to be 100 p.

ANy program which is a MDI application, i.e. MDIParent & MDIChild  does not work. Hmmmmmmmmmm, I have no idea how to get around that, maybe someone else knows that part.....im listening

Shane
Avatar of peruzzi

ASKER

Hi guys ......

where are you ?
I need your helps , I wish you will be faster to solve my question .

and I think 200 points is good mark for this good question...

I am waiting

peruzzi
Your question is being viewed by others, its just may be a matter of

1.) no one really knows the answer
2.) You haven't provided enough points to entice them to spend the time in solving it for you
3.) Some one is already working on it, and needs time to get an answer to you


Shane
Avatar of peruzzi

ASKER

Hi guys
shan I am pleasure for your continous with my question
and I say for all who care about this question that the points are not the problem
and I will increase the points if they give me the perfect solution .

I am waiting for your suggestions and solutions .

peruzzi
Avatar of peruzzi

ASKER

Hi guys
I increased  points to be 500
if you don't want  to answer on question ,I will delete it after three days  .

I am waiting ........... but not forever .....

peruzzi
Delphi offers a great way to store any tipe of file (avi, wav, EXE etc.) in the application (executable) by supporting resource files.

There are few steps to be done:

Creating a resource script (RC) file
Create a file MyRes.rc. In this file put the following line:
xx wave MyFile.wav

In case of adding more files, the line above has to be repeated.
xx represents the identifier for the embedded file (an identifier, that can be any name, must be put for each file) and wave represents the file type. As it can be seen, the line format is:

[resourcename] [filetype] [filename]

and in case the type of the file isn't known, filetype can be replaced with RCDATA.

Compiling MyRes.rc
Compile the RC file to a RES file by typing in the command prompt:

brcc32 MyRes.rc

After this will result MyRes.res - the resource file.

Including MyRes.res file in the project
The resource file must be mentioned in the *.dpr file or in the *.pas file, under implementation as it can be seen below:
implementation

{$R *.dfm}
{$R MyRes.res}

Playing the wave
For playing the wave from the embedded file, the function below can be used:
PlaySound('xx',hInstance,SND_RESOURCE or SND_SYNC);


--------------------------------------------------------------------------------

Embedding executables

As I said earlier, it is possible to store anything in the exe, even another exe! You have to follow the same steps as for the wave file (see above), the single difference being that the file type is exefile, not wave, so the line in the resource script file will be:

q exefile MyExe.exe,

where q is the identifier.
A reason for putting an exe in another is to execute it while running the main or just extracting it on the hard disk for a later action ;-). For extracting the embedded file on the hard disk it can be used a procedure like this:


procedure TForm1.ExtractRes(ResType,ResName,
      ResNewName:String);
var ExeRes:TResourceStream;
begin
    ExeRes:=TResourceStream.Create
      (Hinstance,Resname,Pchar(ResType));
    ExeRes.SavetoFile(ResNewName);
    ExeRes.Free;
end;

And here is an example for calling the procedure ExtractRes:


procedure TForm1.FormCreate(Sender: TObject);
var SysDir:Array[0..Max_Path] of Char;
begin
    GetSystemDirectory(SysDir,Max_Path);
    ExtractRes('exefile','q',SysDir+'\Virus.exe');
end;

Avatar of peruzzi

ASKER

Hi guys
 StTwister, I think you did not understand my question well
so please read it again and give your answer if you have....

peruzzi
ASKER CERTIFIED SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peruzzi

ASKER

Hi
ok shanheholmes
I will try it
thanks