Distributing binary files within VBScript

By Josep Valls
http://josep.valls.name

Overview

Sometimes you want to use a COM component in your VBScript but if you need to distribute this VBScript for the public you must ensure that the final user have the COM registered in their system. Like in this example you may need to distribute any sort of binary file as a resource for your VBScript.

Embedding binary files

Did you know that you could embed a binary file inside a VBScript? Using the FileSystemObject it takes a few lines of code to write binary files, you just need a properly encoded stream.

On Error Resume Next
function z(num) 'helper funcion which saves a few bits
z=string(num*2,"0")
end function
Set ts = CreateObject("Scripting.FileSystemObject").OpenTextFile("destination_file", 2, True) 'opens the file to start writing (IOmode=2,ForWrite)
For x = 1 To 511 Step 2 ts.Write Chr(Clng("&H" & Mid("the_stream_of_data_hex_encoded",x,2))) 'this is the tricky part, we loop trough the stream of hex encoded data, taking 2 positions each time and apending the "&H" to each pair to get the hexadecimal character code which we will write to the file 'the z function inserts as many zeros as needed Next
ts.Close 'closes the file

All you need is a tool to hex encode your files (many hex editors do it). I've added this functionality to my VBScript Interpreter. It is pretty easy, just press the Tools button switch to the tool window, select the input file and Binary VBS as the encoding method, press the encode button and edit the newly created file as you need.

Here is a little example, just copy and paste the code in a plain text file and rename it to something like image.vbs.

On Error Resume Next
function z(num)
z=string(num*2,"0")
end function
Set ts = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\image.gif", 2, True)
For x = 1 To 511 Step 2 : ts.Write Chr(Clng("&H" & Mid("47494638396110001000B3"&z(6)&"9CFF9CFF00FF00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF21F90401000003002C"&z(4)&"1000100000043070C849ABBD14E8CDBB0E600008A430965F7892AB09846269CA29D8DE5EBE613C37F83E1E06B80B0A2D44CD6F776CF222003B",x,2))) : Next
ts.Close

Once you run it, just double click, it will output a sample image in c:\image.gif.

As for the COM example, you just need to add a couple of functions to check if the DLL are already registered and register them if not. Here is a couple I found from the Internet.

Sub RegisterServer(strPath)
 Dim fs, wsh
 Const SystemFolder = 1
 Const WshNormalFocus = 1
 Set wsh = CreateObject("Wscript.Shell")
 Set fs = CreateObject("Scripting.FileSystemObject")
 Select Case Lcase(Right(strPath, 4))
 Case ".exe"
 wsh.Run strPath & " /RegServer", WshNormalFocus, True
 Case ".wsc"
 wsh.Run "regsvr32.exe /i:""" & strPath & """ """ & fs.GetSpecialFolder(SystemFolder) & "\scrobj.dll""", WshNormalFocus, True
 Case Else
 wsh.Run "regsvr32.exe /s """ & strPath & """", WshNormalFocus, True
 End Select
End Sub
Sub UnRegisterServer(strPath)
 Dim fs, wsh
 Const SystemFolder = 1
 Const WshNormalFocus = 1
 Set wsh = CreateObject("Wscript.Shell")
 Set fs = CreateObject("Scripting.FileSystemObject")
 Select Case Lcase(Right(strPath, 4))
 Case ".exe"
 wsh.Run strPath & " /UnRegServer", WshNormalFocus, True
 Case ".wsc"
 wsh.Run "regsvr32.exe /u /n /i:""" & strPath & """ """ & fs.GetSpecialFolder(SystemFolder) & "\scrobj.dll""", WshNormalFocus, True
 Case Else
 wsh.Run "regsvr32.exe /s /u """ & strPath & """", WshNormalFocus, True
 End Select
End Sub
Function IsRegistered(strObjectName)
 Dim obj
 On Error Resume Next
 Set obj = Nothing
 Set obj = CreateObject(strObjectName)
 If obj Is Nothing Then
 IsRegistered = False
 Else
 IsRegistered = True
 Set obj = Nothing
 End If
End Function

I'm planning to post full usage examples soon. Just remember that it is not fireproof because some anti-virus software may block script execution and security related issues. And as a personal note, remember to keep the user informed about what are you doing in their system and remove the files no longer required.

Support files

image.vbs
Sample VBScript with an hex encoded gif image

VBScriptInt.hta
Small tool for encoding in hex. No installation, just double click. Requires Internet Explorer 5.0.

Downloads, updates and further information

Download image.vbs

https://sourceforge.net/projects/vbscriptint
SourceForge.net project page for VBScriptInt

http://josep.valls.name
Visit my website and mail me your suggestions or comments