Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

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
0
peruzzi
Asked:
peruzzi
  • 8
  • 6
1 Solution
 
shaneholmesCommented:
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
 
peruzziAuthor Commented:
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
 
shaneholmesCommented:

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Shane
0
 
peruzziAuthor Commented:
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
 
shaneholmesCommented:
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
 
peruzziAuthor Commented:
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
 
shaneholmesCommented:
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
 
peruzziAuthor Commented:
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
 
peruzziAuthor Commented:
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
 
StTwisterCommented:
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
 
peruzziAuthor Commented:
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
 
shaneholmesCommented:
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
 
peruzziAuthor Commented:
Hi
ok shanheholmes
I will try it
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now