Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write binary files with VBS

Shulou Source: shulou.com Published: 2022-06-03 07:42:31 09月17日 Update

This article is about how to use VBS to read and write binary files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The method given in the previous article is good, but when the file is too large (actually very small, only a few M), the corresponding array will be very large, and then there will be a run-time error indicating that the memory is exhausted, which has depressed me for a long time.

Recently, I found the keyword "some mischievous vbs programs" in the blog's traffic records (it seems that young people like to play this), so I opened the long-lost Baidu search and found that it appeared in the third place. The second is Baidu HI, which is also a prank program and is encrypted. I tried to decrypt it, but found that the encryption seemed to be more abnormal and unsuccessful than the virus that was decrypted a few days ago.

Although I didn't decrypt that program (please give me some advice if any real master can), 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.

The copy 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 argument to the function is the file path to be read, and the return value is a string containing the hexadecimal value of the binary file.

The copy 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 get the hexadecimal value of the binary file, there are many ways, of course, the easiest way is to use the fso object to write the return value of the ReadBinary function to the text file, and then Ctrl+C,Ctrl+V. In fact, it is easy to use hexadecimal tools.

Thank you for reading! This is the end of the article on "how to use VBS to read and write binary files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Files binaries methods code hexadecimal parameters programs good content functions characters strings pranks more articles paths encryption practical young big Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Shulou Tech Info Apple Shulou Technology