我在 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
答案 0 :(得分: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