Solved

Run another application in my application form !!!!

Posted on 2004-03-21
15
316 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:peruzzi
  • 8
  • 6
15 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Look at the Windows.SetParent(WindowHandle, Panel1.Handle);

This code was originally posted by DragonSlayer:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682510.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.
0
 

Author Comment

by:peruzzi
Comment Utility
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
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility

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

 Windows.SetParent(WindowHandle, Panel1.Handle);

 did you do that?


Shane
0
 

Author Comment

by:peruzzi
Comment Utility
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
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
I dont get it - not with all? Which ones do you have success with and ones you dont?

Shane
0
 

Author Comment

by:peruzzi
Comment Utility
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.

0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:peruzzi
Comment Utility
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
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
0
 

Author Comment

by:peruzzi
Comment Utility
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
0
 

Author Comment

by:peruzzi
Comment Utility
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
0
 
LVL 2

Expert Comment

by:StTwister
Comment Utility
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;

0
 

Author Comment

by:peruzzi
Comment Utility
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
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 500 total points
Comment Utility
Peruzzi,

I don't believe anyone knows how to answer your question for a MDI style application.
However, you may be able to get someome from Borlands R & D  (or teamB) to answer it for you, if you post the question on the following newsgroup:

comp.lang.pascal.delphi.misc
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&group=comp.lang.pascal.delphi.misc

Hope this helps!

Shane
0
 

Author Comment

by:peruzzi
Comment Utility
Hi
ok shanheholmes
I will try it
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now