Another way to read and write binary files with vbs

Time:2022-5-18

Although the method given in the previous article is good, when the file is too large (actually very small, just a few m), the corresponding array will also be large, and then there will be a runtime error, indicating that the memory is exhausted. For this reason, I have been depressed for a long time.

Recently, I found Baidu keyword “some very mischievous VBS programs” in the traffic record of the blog (it seems that young people now like to play this), so I opened Baidu, which I haven’t seen for a long time, and found that it appeared in the third place. Baidu Hi appears in the second place, which is also a prank program, and it is encrypted. I tried to decrypt, but found that the encryption seemed to be more abnormal than the virus decrypted a few days ago.

Although I didn’t decrypt the program (if any real master can, please give me some advice), I learned another way to read and write binary files with VBS, that is, Microsoft XMLDOM+ADODB. Stream。 This method is much more efficient than the original one, and there seems to be no file size limit.

Copy codeThe code is as follows:


Function ReadBinary(FileName)
Const adTypeBinary = 1
Dim stream, xmldom, node
Set xmldom = CreateObject(“Microsoft.XMLDOM”)
Set node = xmldom.CreateElement(“binary”)
node.DataType = “bin.hex”
Set stream = CreateObject(“ADODB.Stream”)
stream.Type = adTypeBinary
stream.Open
stream.LoadFromFile FileName
node.NodeTypedValue = stream.Read
stream.Close
Set stream = Nothing
ReadBinary = node.Text
Set node = Nothing
Set xmldom = Nothing
End Function


The parameter of the function is the file path to be read, and the return value is a string containing the hexadecimal value of the binary file.

Copy codeThe code is as follows:


Sub WriteBinary(FileName, Buf)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim stream, xmldom, node
Set xmldom = CreateObject(“Microsoft.XMLDOM”)
Set node = xmldom.CreateElement(“binary”)
node.DataType = “bin.hex”
node.Text = Buf
Set stream = CreateObject(“ADODB.Stream”)
stream.Type = adTypeBinary
stream.Open
stream.write node.NodeTypedValue
stream.saveToFile FileName, adSaveCreateOverWrite
stream.Close
Set stream = Nothing
Set node = Nothing
Set xmldom = Nothing
End Sub


The first parameter is the file path to be read in, and the second parameter is a string containing the hexadecimal value of the binary file. As for how to obtain the hexadecimal value of the binary file, there are many methods. Of course, the simplest method is to write the return value of the readbinary function to the text file with the FSO object, and then Ctrl + C, Ctrl + V. In fact, using hexadecimal tools is also very simple.

I won’t say much about others. If you don’t understand, please look at MSDN reference documents. Wrote an example program, which contains all the data of sky city music. Double click it to generate and play the MP3 file of sky city in the same folder.

Original text: http://demon.tw/programming/vbs-binary-file-another.html