Solved

Run another application in my application form !!!!

Posted on 2004-03-21
15
317 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
ID: 10643974
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
ID: 10647534
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
ID: 10648334

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
ID: 10649968
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
ID: 10650114
I dont get it - not with all? Which ones do you have success with and ones you dont?

Shane
0
 

Author Comment

by:peruzzi
ID: 10652313
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
ID: 10652404
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:peruzzi
ID: 10660523
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
ID: 10660665
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
ID: 10662095
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
ID: 10685791
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
ID: 10707484
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
ID: 10742609
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
ID: 10742670
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
ID: 10742768
Hi
ok shanheholmes
I will try it
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
code issue 8 100
Dev Express grid collapse 2 39
Firemonkey DbLookupComboBox equivalent ? 2 37
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 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

21 Experts available now in Live!

Get 1:1 Help Now