Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write binary files with VBS

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report