0

I have this code in vb6 that can create an exe file from its hex code. I want to do the same in vb.net.

This is my vb6 code:

Public Sub Document_Open()

    Dim str As String
    Dim hex As String

    hex = hex & "4D 5A 50 00 02 00 00 00 04 00 0F 00 FF FF 00 00 B8 00 00 00 00 00 00 00" 
    hex = hex & "40 00 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

    'you have to put the full hex code of the application here

    Dim exe As String
    Dim i As Long
    Dim puffer As Long

    i = 1

    Do
        str = Mid(hex, i, 2)

        'convert hex to decimal
        puffer = Val("&H" & str)

        'convert decimal to ASCII
        exe = exe & Chr(puffer)

        i = i + 2

        If i >= Len(hex) - 2 Then
            Exit Do
        End If
    Loop

    'write to file
    Open "C:\application.exe" For Append As #2
    Print #2, exe
    Close #2

    'and run the exe
    Dim pid As Integer
    pid = Shell("C:\application.exe", vbNormalFocus)

End Sub
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
Camilla Dx
  • 9
  • 1
  • 1

1 Answers1

0

It would be easier if the data was defined as a byte array literal, like this:

Dim bytes() As Byte = {&H4D, &H5A, &H50,
                       &H0, &H2, &H0} ' etc...
File.WriteAllBytes("c:\application.exe", bytes)

However, it would be nicer to store the binary data in a resource, then just write the resource out to a file, like this:

File.WriteAllBytes("c:\application.exe", My.Resources.Application_exe)

If you really need to convert it from a hexadecimal string, you could do it like this:

Dim hex As String = "4D 5A 50 00 02 00 00 00 04 00 0F 00 FF FF 00 00 B8 00 00 00 00 00 00 00" &
                    "40 00 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
Using fs As New FileStream("c:\application.exe", FileMode.Create, FileAccess.Write)
    For Each byteHex As String In hex.Split()
        fs.WriteByte(Convert.ToByte(byteHex, 16))
    Next
End Using
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105