• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

how to attach my application to an exe?

i want to attach my application to an exe-file and run it before the original exe runs.
How do i do this?
0
bqui
Asked:
bqui
  • 12
  • 8
  • 3
  • +4
1 Solution
 
scrapdogCommented:
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
 
elkiorsCommented:
What about calling the second exe from within your own and making the first one unavailable, maybe hidden ?

Darren
0
 
MadshiCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bquiAuthor Commented:
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
 
MadshiCommented:
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
 
bquiAuthor Commented:
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
 
BoRiSCommented:
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
 
bquiAuthor Commented:
0
 
mtielandCommented:
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
 
bquiAuthor Commented:
i did not found anything about that on the given site.
0
 
MadshiCommented:
He probably means the component that can simply execute other processes...   :-(
0
 
bquiAuthor Commented:
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
 
elkiorsCommented:
why is having to copy 2 files onto the users system a problem ?

Darren

0
 
bquiAuthor Commented:
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
 
BoRiSCommented:
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
 
bquiAuthor Commented:
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
 
elkiorsCommented:
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
 
jeurkCommented:
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
 
bquiAuthor Commented:
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
 
MadshiCommented:
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
 
bquiAuthor Commented:
Madshi

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

f anybody has an idea how to do it an other way, let me know.
0
 
jeurkCommented:
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
 
bquiAuthor Commented:
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
 
MadshiCommented:
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
 
MadshiCommented:
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
 
bquiAuthor Commented:
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
 
MadshiCommented:
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
 
bquiAuthor Commented:
Madshi,

Now It works.
thank you!
0
 
MadshiCommented:
That's great! Am glad that I could help you...  :-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 12
  • 8
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now