Solved

how to attach my application to an exe?

Posted on 1998-11-28
29
236 Views
Last Modified: 2010-04-04
i want to attach my application to an exe-file and run it before the original exe runs.
How do i do this?
0
Comment
Question by:bqui
  • 12
  • 8
  • 3
  • +4
29 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1348404
You would have to obtain the source code for the original exe and alter it and recompile it.  Or you could use a batch file.
0
 

Expert Comment

by:elkiors
ID: 1348405
What about calling the second exe from within your own and making the first one unavailable, maybe hidden ?

Darren
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348406
Do you want to write a virus???

I see 3 possibilities (I use "notepad.exe" as an example):

(1) If you have the source code: fine. Add your sources and recompile.   (see scrapdog's comment)
(2) Rename "notepad.exe" to "blabla.exe". Name your own exe "notepad.exe". Then if your notepad is started just call "CreateProcess(nil,'blabla.exe',...)".   (see Darren's comment)
(3) Compile your own exe and put the length of "notepad.exe" in a constant. Then write a little utility that does nothing but copy your exe to a new file and copy "notepad.exe" behind it. (I think the dos copy command can do it do). Then if your exe is started just create at runtime the original "notepad.exe" by copying the last xxx bytes to "blabla.exe". Then start "blabla.exe".
(4) Look at (3), but don't create a "blabla.exe", but jump directly to the beginning of "notepad.exe" in the end of your exe file (using asm command jump). But I fear that won't work because perhaps there's perhaps/probably some absolute addressing in the notepad code.

Regards, Madshi.
0
 

Author Comment

by:bqui
ID: 1348407
Hi Madshi!

I do not write a  virus, only a utility for a program i do not have the sourcecode for.
Can you give me more informations to point (4)?
what address have i jump to?
The problem with absolute adresseing i would not have if i attach my prog to the other and modify the entrypoint in the exe-header. After executing my program i could jump to the real entrypoint of the old program. (??)
Any idea how to do this?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348408
Hmmm. You could either put your exe in front of the other one. Then Windows would automatically start YOUR application, but the other one would be mapped to another address than normal. So you could jump to the entry point of the other exe. But I'm sure, I think you might get into trouble with that if the other exe uses absolute addressing rather than relative addressing. I don't know how the compiler/linker do addressing...
The other possibility (I think that is what you've in mind) is to save your exe BEHIND the other one and just change the original exe's entry point to your's. But there I see two problems: (1) Where is the entry point? (Perhaps you know that). (2) Now YOUR exe is at another address as it is thought to be from the compiler/linker. So the other process will probably have no problems, but perhaps yours. Perhaps you can solve this by writing assember code. Then you have full control over the addressing...
I'm not sure if I can really help you more than this, because I never did something like this before. I think, you will have to experiment with it yourself...   :-(

Regards, Madshi.
0
 

Author Comment

by:bqui
ID: 1348409
getting the entrypoint will be a problem. i'll need to have informations about the exe-header.
thanks so far.
i'll post a question to programming-windows to get more informations.
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1348410
bqui

Why don't you write a hook that loads in the background everytime windows starts, the hook waits until the required application is called then traps the message and executes your app first, so basically you have two parts to the application one can be a .DLL this is a much faster and safier way of doing what you require, instead of using ASM code etc. all though madshi has so neat ideas there...

Later
BoRiS
0
 

Author Comment

by:bqui
ID: 1348411
0
 

Expert Comment

by:mtieland
ID: 1348412
Check out the following link for a great component to solve your problem!

http://www.worldnet.net/~cycocrew/delphi/components.html

Good Luck,

Martijn
0
 

Author Comment

by:bqui
ID: 1348413
i did not found anything about that on the given site.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348414
He probably means the component that can simply execute other processes...   :-(
0
 

Author Comment

by:bqui
ID: 1348415
boris
using a hook is much easier, but if i do so i have the problem, that i have two files and my application need to be installed on the users system.
0
 

Expert Comment

by:elkiors
ID: 1348416
why is having to copy 2 files onto the users system a problem ?

Darren

0
 

Author Comment

by:bqui
ID: 1348417
if i uses a hook to run my application on the programs startup, the dll need to be loaded before. I do not want to have the user 5to do anything.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:BoRiS
ID: 1348418
bqui

I don't see a problem the install programs allow you to add entries to the registry so you can just popit in there and there you go the dll is loaded and waiting for the message of the application it is monitoring...

Later
BoRiS
0
 

Author Comment

by:bqui
ID: 1348419
As you say i need an install program. this is what i do not want.
any idea how to do it an other way?
I think it is the best to attach my app, but it may be the difficultes.
0
 

Expert Comment

by:elkiors
ID: 1348420
Can you not write a mini-app in delphi to create the registry keys for you, it doesn't need to be anything fancy, just a kind of primer before you do the copy of your main exe.

Or, have your main exe create the registry keys if the don't already exist?

Darren

0
 
LVL 4

Expert Comment

by:jeurk
ID: 1348421
hi,
I would suggest the following tactic :
It's similar to what a autodecompactable zipfile is doing.
I know that you will still have the work to do but hey, this is a comment.
If you need further help, let me know.
Ok, here it is:
You should go to http://www.geocities.com/SiliconValley/Network/2114/
and get   DZ-DEL15.ZIP 445,365 bytes, The Freeware Delphi Zip package
direct link : http://www.geocities.com/SiliconValley/Network/2114/dz-del15.zip
In there you will find the source of a program that is aimed to extract the zip file that is attached.
The principle should be the same for you.

But here I have another idea : If you program is not to big, then do the following
let's say that you have your exe-app, let's call it app1 and your application, let's call it app2.
app1 will launch app2 that is attached to it.
to attache app2 to app1 you can do like that : copy app1.exe + app2.exe app3.exe /b
/b to let opy know that it's a binary copy.

in app1, your code will do the following :
it will open app3.exe
if app3.exe is x o long then advance from x in the file
and the copy the rest of the file in a new file called app4.exe
execute app4.exe

to get the x value, put a ramdom value in the app1 project
compile it, and then look at it's size.

What do you think of that ?
Sorry if someone already says this in that thread, I've not read it to carefully ;)

Hope this helps.
Let me know.

0
 

Author Comment

by:bqui
ID: 1348422
Jeurk

This seems to be a practicable way.
If i find no other way i'll doit the way you discribed.
there is one thing why that way is not the best:
i'll have to create a new exe-file.
it would be nicer if i only got one exe, but ...
well, i think i'll do it the way you discribed.

If anybody has an idea how to do it an other way, please tell me.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348423
Hey, bqui,

what jeurk suggested, is exactly what I suggested already (Sunday, November 29 1998 - 02:17AM). See suggestion (3).
It's only fair to say: I was first...   :-)

Regards, Madshi.
0
 

Author Comment

by:bqui
ID: 1348424
Madshi

I see.  sorry. thank you both.
:-)

f anybody has an idea how to do it an other way, let me know.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 1348425
Like I sayd Madshi I've not read the thread too carefully ;) and you are right you were first.

If you want bQui, to get the points I can write the exe for you.
By the way I don't understand you exactly:

'i'll have to create a new exe-file. '
Yuo have one exe and want to launch another before yours, that's 2 exes

it would be nicer if i only got one exe, but ...
And the end you only have one exe.
I think it should be faisaible to make a component that can store and exe in a property
and execute it whenever the user wants to.

Can you explain me what you mean by one exe ?

0
 

Author Comment

by:bqui
ID: 1348426
jeurk

if i do it the way you proposed (i'll do so), i attach the program's exe (app1.exe) to my exe (app2.exe) i'll get app3.exe.
before running it i'll have to create app2.exe as a new exefile because i need a exefile to call createprocess.
so i have my app3.exe and app2.exe.
after runnig app2.exe i have to delete it. any idea how to notice the termination of app2?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348427
bqui,

hope this helps...

unit runThread_;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, tools, ddraw;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen}
  public
    { Public-Deklarationen}
  end;

var Form1 : TForm1 = nil;

implementation

{$R *.DFM}

const unitName = 'runThread_.';

type TRunThread = class(TThread)
  private
    processHandle : cardinal;
    processReady  : boolean;
    waitingThread : cardinal;
    procedure Execute; override;
  end;

procedure TRunThread.Execute;
begin
  WaitForSingleObject(processHandle,INFINITE);   // This call does not return, unless copy is stopped
  processReady:=true;                            // Set "processReady" flag for main thread
  PostThreadMessage(waitingThread,WM_NULL,0,0);  // Wake up main thread
                                                 // If you call Application.HandleMessage (see below) in the
                                                 // main thread, the main thread is sleeping the most time in
                                                 // winAPI "waitMessage". So we send a "dummy" message in order
                                                 // to let the main thread return from Application.HandleMessage
end;

procedure TForm1.Button1Click(Sender: TObject);
var si : TStartupInfo;
    pi : TProcessInformation;
begin
  caption:='start copy...';
  ZeroMemory(@si,sizeOf(si)); si.cb:=sizeOf(si);
  si.dwFlags:=STARTF_USESHOWWINDOW; si.wShowWindow:=SW_HIDE;
  if CreateProcess(nil,'c:\command.com /c copy c:\autoexec.bat c:\test.bat >c:\output.txt',nil,nil,false,0,nil,nil,si,pi) then begin
    caption:='copy started...';
    with TRunThread.Create(true) do         // create the thread object, but do not start it now...
      try
        processHandle:=pi.hProcess;         // tell the thread what process it has to watch
        processReady:=false;                // flag for the loop (see below)
        waitingThread:=GetCurrentThreadID;  // the current threadID for the wakeup message (see above)
        caption:='wait for copy...';
        Resume;                             // now all information is prepared; so let's start the thread
        repeat
          Application.HandleMessage;        // message loop
        until Application.Terminated or processReady;  // continue with normal program when either the
                                                       // started process has stopped or our program is closed
        caption:='copy stopped...';
      finally Free end;
    CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Never forget to close handles...
    caption:='ready...';
  end else caption:='could not start copy...';
end;

end.

Regards... Madshi.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348428
Hmmm. You should remove the following line from the sources:
"si.dwFlags:=STARTF_USESHOWWINDOW; si.wShowWindow:=SW_HIDE;"

Originally I wrote this unit the start a dos program in hidden state and wait until it has finished.
0
 

Author Comment

by:bqui
ID: 1348429
ok thank you.
Madshi, i'll give you the points.

last question: why my program hangs up, when i finish it before finishing the created process? any idea?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1348430
bqui,

thanx for the points. In the moment I've no idea why your program hangs up, because if you're running my sources without any modifications, it works without hanging.
Do you have a main form in your application or do you show any other things (e.g. systray icon) while the other process runs or is your application just hidden and does nothing but wait for the ending of the other process?

Regards, Madshi.
0
 

Author Comment

by:bqui
ID: 1348431
Madshi,

Now It works.
thank you!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1348432
That's great! Am glad that I could help you...  :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

22 Experts available now in Live!

Get 1:1 Help Now