tags:

views:

978

answers:

3

Alright so I'm essentialyl trying to code something that will combine two files together in VB and output a single file that when run, runs both of them. I've grabbed this source from several places online and am just trying to get it to work. We have the main program that combines them with a GUI

Const FileSplit = "@<>#<>#<>@"

Private Sub cmdAdd_Click()
    With Dlg
        .Filter = "All Files(*.*) | *.*"
        .DialogTitle = "Please Select a File..."
        .ShowOpen
    End With

    lsFiles.AddItem (Dlg.FileName)


End Sub

Private Sub cmdBuild_Click()

Dim sStub As String, sFiles As String, i As Integer
Open App.Path & "\stub.exe" For Binary As #1
sStub = Space(LOF(1))

Get #1, , sStub
Close #1


Open App.Path & "\boundfile.exe" For Binary As #1
Put #1, , sStub & FileSplit
For i = 0 To lsFiles.ListCount - 1


Open lsFiles.List(i) For Binary As #2
sFiles = Space(LOF(2))
Get #2, , sFiles
Close #2

Put #1, , sFiles & FileSplit

Next i

Close #1

MsgBox "Files Successfully Combined"


End Sub

And then we have a second App that acts as a stub

Const FileSplit = "@<>#<>#<>@"
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


Private Sub Form_Load()
Dim sStub As String, sFiles() As String, i As Integer

Open App.Path & "\" & App.EXEName & ".exe" For Binary As #1
sStub = Input(LOF(1), 1)
Get #1, , stub
Close #1

sFiles = Split(sStub, FileSplit)
For i = 1 To UBound(sFiles())
Open Environ("tmp") & "\tmp" & i & ".exe" For Binary As #1
Put #1, , sFiles(i)
Close #1
Call ShellExecute(0, vbNullString, Environ("tmp") & "\tmp" & i & ".exe", vbNullString, vbNullString, vbNormalFocus)

 Next i 
 End
End Sub

however when the files are combined and run all I get is a dosbox opening and closing. Any ideas?

A: 

I would start by comparing the temporary files, are you successfully re-writing out the files where they are exact matches to that of the source?

Mitchel Sellers
+1  A: 

One major issue is that you are using String variables which terminate at the first null character (ASCII code 0)

Since executable files are binary, it is exceptionally likely that they are are not being copied into (or out of) the file in full.

As a result, I would suggest reading the files into a Byte array and encoding them using an algorithm that produces only printable characters (such as Base64). Alternately, you could devise some scheme of searching through the Byte arrays to find the EOF delimiters.

I'm going to favorite this item, as it appears that this is the start of an interesting way to embed files within an executable. With sufficient coding, you could also embed the original filenames and extensions as well as specifying what behavior to perform after expansion. Add in some compression, and you've got a pretty sweet app.

smbarbour
A: 

When using Open...As Binary and Put, some data about the string is being added to the output messing things up. Instead use Open...For Output and Print. And end your print line with a semicolon to not intert a linebreak. Also your stub is a bit weird, changed the stuff I mentioned above and that in your code as follows::

Const FileSplit = "@<>#<>#<>@"

Private Sub cmdAdd_Click()
    With Dlg
        .Filter = "All Files(*.*) | *.*"
        .DialogTitle = "Please Select a File..."
        .ShowOpen
    End With
    lsFiles.AddItem (Dlg.FileName)
End Sub

Private Sub cmdBuild_Click()
    Dim sStub As String, sFiles As String, i As Integer
    Open App.Path & "\stub.exe" For Binary As #1
    sStub = Space(LOF(1))
    Get #1, , sStub
    Close #1
    Open App.Path & "\boundfile.exe" For Output As #1
    Print #1, sStub & FileSplit;
    For i = 0 To lsFiles.ListCount - 1
        Open lsFiles.List(i) For Binary As #2
        sFiles = Space(LOF(2))
        Get #2, , sFiles
        Close #2
        Print #1, sFiles & FileSplit;
    Next i
    Close #1
    MsgBox "Files Successfully Combined"
End Sub

and

Const FileSplit = "@<>#<>#<>@"
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()
    Dim sStub As String, sFiles() As String, i As Integer
    Open App.Path & "\" & App.EXEName & ".exe" For Binary As #1
    sStub = Space(LOF(1))
    Get #1, , sStub
    Close #1
    sFiles = Split(sStub, FileSplit)
    For i = 1 To UBound(sFiles())
        Open Environ("tmp") & "\tmp" & i & ".exe" For Output As #1
        Print #1, sFiles(i);
        Close #1
        Call ShellExecute(0, vbNullString, Environ("tmp") & "\tmp" & i & ".exe", vbNullString, vbNullString, vbNormalFocus)
    Next i 
    End
End Sub
svinto