Extract .exe from .exe


                                           Hi Experts:)
                   I would like to have program A.exe launch program b.exe
                   I would like program b.exe to somehow be within program
                     A.exe, then when program A.exe runs, it could extract
                                         program B.exe
                      If this is possible to do in vb 5.0 enterprise I would
                                    appreciate knowing how?
                   Please show code example with comments. I know what i
                    would like to do in the above question but i am not that
                                        great in vb yet !
                                             Thanks
                                              necco
neccoAsked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
Yes, you can do this... It's called "stub programming".... Basically it works like this...

Once you have compiled program A into A.EXE, you can copy B.EXE at the end of the file.  This does not affect the A.EXE because the B.EXE is outside the execution boundry.

You can do this with the dos command:

   COPY /B A.EXE + B.EXE C.EXE

Once you have done this, distribute C.EXE as A.EXE



WHAT NEEDS TO HAPPEN IN YOUR PROGRAM BEFORE YOU DO THIS DOS COPY???

In your A.EXE program, you have to write a routine that opens your A.EXE in BINARY READ mode, starting at where your program ends MINUS the size of B.EXE in bytes... Read the data and write it to a temp .EXE file and then Execute it...

For example, lets say that after you create your A.EXE (including routine to read the second EXE) it is 14,226 bytes.

The number you need to be using in your program to read the offset where the second program starts is 14,227.



Hope this help!



Cheers!
0
 
mcriderCommented:
By the way, you can get the size of B.EXE from a DIR command...

When your program is running, you can get the size of the file you opened (your A.EXE) by using the LOF or FileLen functions...


Cheers!
0
 
vbkannCommented:
mcrider, can you have a look at my question (it is exactly like this one for 200 points) - i just need to be able to get the code to do it ( the reading of the data)

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10256495 
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
anthonycCommented:
why would you want to do this?
0
 
neccoAuthor Commented:
Hi mcrider:) I know your code will work. I can't seem to write the routine:
(In your A.EXE program, you have to write a routine that opens your A.EXE in BINARY READ mode,
                starting at where your program ends MINUS the size of B.EXE in bytes... Read the data and write it
                to a temp .EXE file and then Execute it...)
Could you show "my ignoranz" an example of this ? I have picked up another five points and promise the excellent grade you deserve .
necco
0
 
mcriderCommented:
OK, here you go... In this example, PROGA.EXE is this program, PROGB.EXE is the EXE you are going to attach, and PROGC will be the new executable containing both PROGA and PROGB...

First, put the following code in a MODULE. This code will execute a command and wait for it to end.

    Public Const INFINITE = -1&

    Private Declare Function OpenProcess Lib "kernel32" (ByVal _
        dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessID As Long) As Long

   Private Type STARTUPINFO
      cb As Long
      lpReserved As String
      lpDesktop As String
      lpTitle As String
      dwX As Long
      dwY As Long
      dwXSize As Long
      dwYSize As Long
      dwXCountChars As Long
      dwYCountChars As Long
      dwFillAttribute As Long
      dwFlags As Long
      wShowWindow As Integer
      cbReserved2 As Integer
      lpReserved2 As Long
      hStdInput As Long
      hStdOutput As Long
      hStdError As Long
   End Type

   Private Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessID As Long
      dwThreadID As Long
   End Type

   Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
      hHandle As Long, ByVal dwMilliseconds As Long) As Long

   Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
      lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
      lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
      ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
      ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
      lpStartupInfo As STARTUPINFO, lpProcessInformation As _
      PROCESS_INFORMATION) As Long

   Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hObject As Long) As Long

   Private Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

   Private Const NORMAL_PRIORITY_CLASS = &H20&
   Public Function ExecCmd(cmdline$)
      Dim proc As PROCESS_INFORMATION
      Dim start As STARTUPINFO

      ' Initialize the STARTUPINFO structure:
      start.cb = Len(start)

      ' Start the shelled application:
      ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
         NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)


      ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         Call GetExitCodeProcess(proc.hProcess, ret&)
         Call CloseHandle(proc.hThread)
         Call CloseHandle(proc.hProcess)
         ExecCmd = ret&
    End Function
   

----------------------------------------------------------
Then put the following code in your program... For this example, I am using a commandbutton:

    Private Sub Command1_Click()
        'THIS CONSTANT MUST BE CHANGE TO THE SIZE
        'OF THE ATTACHED EXE IN BYTES
        Const AttachedExeSize = 8704
       
        Dim TempFnum As Long
        Dim TempExe As String
        Dim MySelfFnum As Long
        Dim MySelfExe As String
        Dim MySelfSize As Long
        Dim lByte As Byte
       
        'BUILD THE PATH TO MYSELF
        MySelfExe = App.Path
        If Not Right$(MySelfExe, 1) = "\" Then MySelfExe = MySelfExe + "\"
        MySelfExe = MySelfExe + App.EXEName + ".EXE"
       
        'GET A FREE FILENUMBER TO OPEN MYSELF AND OPEN MYSELF
        MySelfFnum = FreeFile
        Open MySelfExe For Binary Access Read As MySelfFnum
       
       
        'FIND THE START OF THE SECOND EXE
        MySelfSize = LOF(MySelfFnum)
        Seek MySelfFnum, MySelfSize - AttachedExeSize + 1
       
        'BUILD THE PATH TO THE TEMP FILE
        TempExe = Environ("TEMP")
        If Not Right$(TempExe, 1) = "\" Then TempExe = TempExe + "\"
        TempExe = TempExe + "TMP" + Format(Now, "hhnnss") + ".EXE"
       
        'GET A FREE FILENUMBER TO THE TEMP FILE AND OPEN IT
        TempFnum = FreeFile
        Open TempExe For Binary Access Write As TempFnum
       
        'EXTRACT THE FILE
        Do
            Get #MySelfFnum, , lByte
            If EOF(MySelfFnum) = True Then Exit Do
            Put #TempFnum, , lByte
        Loop
       
        'CLOSE THE FILES
        Close MySelfFnum
        Close TempFnum
       
        'EXECUTE AND WAIT FOR THE EXTRACTED EXE TO COMPLETE
        ExecCmd TempExe
       
        'DELETE THE EXTRACTED EXE
        Kill TempExe
    End Sub


----------------------------------------------------------
Make sure you change the constant AttachedExeSize to be the size of the EXE you are attaching in BYTES.

Compile your program.

In a dos command window, do the copy to attach the file:

   COPY /B PROGA.EXE + PROGB.EXE PROGC.EXE

Run PROGC.EXE... You program will run. when you click the commandbutton, PROGB.EXE will be extracted to a temp EXE in the TEMP directory and executed.  Once PROGB terminates, the temp EXE will be deleted!



Cheers!
0
 
mcriderCommented:
anthonyc

You would want to do this for a number of reasons... The code above provides a way to do a SelfExtracting EXE in VB...

Cheers!
0
 
neccoAuthor Commented:
mcrider:)
I will test this code later. I will issue your "much deserved points and grade" now, for you should not have to wait anylonger. If i run into problems i will post it here, but i doubt i will as the code is beautiful !!!!

Thanks
necco
0
 
mcriderCommented:
Thanks for the points! Glad I could help! Let me know how things work out...


Cheers!
0
 
neccoAuthor Commented:
mcrider:) I used the code and it worked very well Thanks Again. I showed your code to my neighbor and he is very impressed. He says he will post a question to you personally. Look for his question because i would like to know the answer too ! His name is "gbfine".
later
necco
0
 
mcriderCommented:
I'm glad you got the code to work! ;-)

If you're feeling generous, you can open a new question with the title "FOR MCRIDER ONLY" and assign a couple of points to it... I will answer it.


Cheer!
0
All Courses

From novice to tech pro — start learning today.