0

我在vb6中有这个代码,可以从它的十六进制代码创建一个 exe 文件。我想在vb.net做同样的事情。

这是我的 vb6 代码:

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
4

1 回答 1

0

如果将数据定义为字节数组文字会更容易,如下所示:

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

但是,最好将二进制数据存储在资源中,然后将资源写入文件,如下所示:

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

如果你真的需要将它从十六进制字符串转换,你可以这样做:

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
于 2013-10-18T20:18:02.487 回答