Can I script with VB 2005 Express?

Good Afternoon,

Is it possible to write VB Script (.vbs) using Visual Basic Express 2005, rather than using a text editor? Thank you.

[148 byte] By [Jim] at [2008-1-1]
# 1

Well, Visual Studio could be considered a glorified text editor, so the answer is yes Smile

-Ryan / Kardax

RyanLamansky at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2
Ryan, Thanks so much for the response. Can you point me to any web sites or articles or just give me a quick tip on how to start writing vb script in vb express 2005? I am looking to write a few scripts to automate some network admin tasks at work. Thanks again.
Jim at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
Perhaps Ryan had something else in mind when he said "yes". The answer is no, the IDE has no functionality beyond what Notepad can provide for writing VB Script source code. Use it to write VB.NET source code, quite a different language.
nobugz at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

I think you can add a reference to WSH object library and intermix VBS and VB2005 code. By referencing libraries, you can do just about anything you can imagine using VB2005. I'm just learning VBE. I find that I can do anything I can imagine.

If you add a text file to a project and change the extension to "vbs", the IDE recognizes it and gives you options to execute it. I hav'nt tried to write a script and find out what I can do with it. But if you think you can you probably can. Don't take no for an answer.

JohnWein at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5
Thank you.
Jim at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6

Start a new project.

Add 1 multiline textbox (textbox1)

Add 1 button (button1)

Paste the text at the bottom of this post into textbox1.

Fix all the word wrapping that the form did to the text.

Then push the button and select an EXE or DLL file with

a nice looking icon.

(I didn't write the VB script, but I like it.)

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim ofd As New OpenFileDialog

ofd.Filter = "Possible Icon Extract Files(*.EXE;*.DLL|*.EXE;*.DLL"

ofd.InitialDirectory = "c:\windows\system32"

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then

Try

System.IO.File.WriteAllText("findicons.vbs", TextBox1.Text)

Process.Start("findicons.vbs", ofd.FileName)

Catch ex As Exception

MsgBox(ex.Message)

End Try

End If

End Sub

End Class

'-- ClsIcons Class
'-- Drop a file (EXE or DLL) onto this script to extract icons.
'-- See included info. file for details.
Dim cIco, FSO1, s1, Arg, iNumberIcons, iNumberExtracted, sFol
Set FSO1 = CreateObject("Scripting.FileSystemObject")
Arg = WScript.Arguments(0)
If FSO1.FileExists(Arg) = False Then
MsgBox "Drop an EXE or DLL file onto script to extract icons.", 64
Set FSO1 = Nothing
WScript.Quit
End If

sFol = "C:\" '-- EDIT THIS LINE TO PATH OF FOLDER WHERE YOU WANT ICONS EXTRACTED. -
If FSO1.FolderExists(sFol) = False Then
MsgBox "Folder path for extraction does not exist. Script must be edited at line 14 - sFol variable.", 64
Set FSO1 = Nothing
WScript.Quit
End If

Set cIco = new ClsIcons
iNumberExtracted = cIco.GetIcons(Arg, sFol, iNumberIcons)

Select Case iNumberExtracted
Case 0
s1 = "No icons found."
Case -1
s1 = "File path not valid: " & Arg
Case -2
s1 = "No resource table section found in file."
Case -3
s1 = "No icons found in resource section."
Case -4
s1 = "This is not a PE file [ 32-bit DLL, EXE, etc.]"
Case Else
s1 = "File: " & Arg & VBCrLf & "Total number of icons: " & iNumberIcons & VBCrLf
s1 = s1 & "Number of icons extracted: " & iNumberExtracted & VBCrLf & "Extraction folder: " & sFol
End Select
MsgBox s1
Set cIco = Nothing
Set FSO1 = Nothing

'- FUNCTIONS:
'
' Public GetIcons(sFilePath, sFolderPath, total) - extracts icons from PE files.
' GetIcons can extract 16x16 or 32x32 in 16-color or 256-color depth.
' sFilePath is path of file for extraction.
' sFolderPath is location to save extracted icons.
' total is total number of icons in the file.
' Return is total files extracted or a negative number error code. (0 returned if no icons in file.)
'
'
' Private GetArray(StringIn, SnipUnicode) - convert a string to an array of byte values. If SnipUnicode = True then get only every 2nd byte.
' Private GetNumFromBytes(array) - takes array of ubound 1 or 3. return numeric value for 2 or 4 bytes.
' Private Bool = GetIcoHeader(Length, sHeader, sSize) - send length of icon resource. if result is true, return header and size string.
'

Class ClsIcons
Private FSO, TS, sBullet
Private VResLoc, ResLoc, SizeRes

Private Sub Class_Initialize()
sBullet = Chr(149)
Set FSO = CreateObject("Scripting.FileSystemObject")
End Sub

Private Sub Class_Terminate()
Set TS = Nothing '-- just in case.
Set FSO = Nothing
End Sub
'-- returns an array of byte values from a string. This is a way to leave the 0-bytes alone
'-- while still being able to Read numeric values from the bytes.
Function GetArray(sStr, SnipUnicode)
Dim iA, Len1, Len2, AStr()
On Error Resume Next
Len1 = Len(sStr)
If (SnipUnicode = True) Then
ReDim AStr((Len1 \ 2) - 1)
Else
ReDim AStr(Len1 - 1)
End If
If (SnipUnicode = True) Then
For iA = 1 to Len1 step 2
AStr(iA - 1) = Asc(Mid(sStr, iA, 1))
Next
Else
For iA = 1 to Len1
AStr(iA - 1) = Asc(Mid(sStr, iA, 1))
Next
End If
GetArray = AStr
End Function

'-- return a number from 2 or 4 bytes.
Private Function GetNumFromBytes(ABytes)
Dim Num1
Err.Clear
' On Error Resume Next
GetNumFromBytes = -1
Num1 = ABytes(0) + (ABytes(1) * 256)
If (UBound(ABytes) = 3) Then
Num1 = Num1 + (ABytes(2) * 65536) + (ABytes(3) * 16777216)
End If
If (Err.number = 0) Then GetNumFromBytes = Num1
End Function

'-- GetIcons(filepath, folderpath, return-totalnumber) = extracted number --
Public Function GetIcons(sFilPath, sFolPath, NumTotal)
Dim OFol, iIcos, iRet, SRet, i4, iPos, iNumIs, sRes, iNum1, iNum2, iNum3, A1, A4(3), Boo1
Dim s, s1, sSz, ReadPt, iOffSet, iSize, sIco, IcoTableOffset
On Error Resume Next
If FSO.FileExists(sFilPath) = False Then
GetIcons = -1 'wrong file path.
Exit Function
End If

If FSO.FolderExists(sFolPath) = False Then
Set OFol = FSO.CreateFolder(sFolPath)
Set OFol = Nothing
End If
iNumIs = GetIconTableOffset(sFilPath, iPos)
If (iNumIs < 1) Then
GetIcons = iNumIs '-- error or no icons. return error code.
Exit Function
End If
NumTotal = iNumIs '-- returns number of distinct icons in file.

IcoTableOffset = (iPos - ResLoc) '-- file offset of icon directory table = offset of resource section + offset of table.
Set TS = FSO.OpenTextFile(sFilPath, 1)
TS.skip iPos
sRes = TS.read(SizeRes - IcoTableOffset) '--load entire resource section, starting at icon table, into sRes.
TS.Close
Set TS = Nothing
'-- Loop through icon listings. This is fairly easy because the sRes string now starts at the first
'-- byte of the icon resource listings. So loop through, 8 bytes at a time, for each icon. Get
'-- offset and size, then read out icon bytes and send length to get icon header.
'-- By doing it this way the whole extraction process can work with a string, rather than
'-- having to continually open and close the PE file. (Why is there no way to back up the Textstream file pointer?!!)
'--
iIcos = 0
ReadPt = 1
'-- for each icon reported to be there, go through steps to get offset and size of resource.
'-- One catch here: Icons seem to be typically numbered in resource section by icon, but
'-- the IMAGE_RESOURCE_DIRECTORY seems to list on distinct icons. In other words, though
'-- IMAGE_RESOURCE_DIRECTORY may say there are 6 numbered icons, there could be 6 sizes of each,
'-- making 36. The technique here is a bit messy, but in order to get all icons it keeps going for
'-- up to 6 times the number of reprted icons, but checks for an invalid IMAGE_RESOURCE_DIRECTORY_ENTRY
'-- with each iteration. If ID is 0 and high bit is 0 then it must be a bad value and therefore the
'-- end of icons must have been reached.

For i4 = 1 to iNumIs * 6
s1 = mid(sRes, ReadPt, 8) '-- read its directory entry.
A1 = GetArray(s1, False)
iNum3 = 1
'-- For each icon, it's now necessary to follow the pointers until reaching
'-- a pointer that does not have the high bit set. That pointer will be the
'-- final pointer to the actual offset and size info.
Do
For iNum1 = 0 to 2 '-- get offset number to next level from 2nd 4 bytes of entry structure.
A4(iNum1) = A1(iNum1 + 4)
Next
A4(3) = 0
iNum2 = GetNumFromBytes(A4)
If (iNum2 = 0) and (A1(7) = 0) Then Exit For '-- end of icons (?)
iNum2 = (iNum2 - IcoTableOffset)
If (A1(7) > 127) Then '-- high bit was set in entry offset value, so it's just a pointer to another pointer.
iNum2 = iNum2 + 17
s1 = mid(sRes, iNum2, 8)
A1 = GetArray(s1, False)
Else '-- this is the offset of icon data info. offset!
iOffSet = iNum2 + 1
Exit Do
End If
iNum3 = iNum3 + 1
If (iNum3 > 10) Then Exit Do
Loop
'-- If that all worked OK then now get the 8 bytes to find offset and size of icon resource.
If (iOffSet > 0) Then
s1 = mid(sRes, iOffSet, 8)
A1 = GetArray(s1, False)
For iNum1 = 0 to 2
A4(iNum1) = A1(iNum1)
Next
A4(3) = 0
VIOffset = GetNumFromBytes(A4)
ReadOffset = (VIOffset - VResLoc) '-- offset of icon resource.
ReadOffset = (ReadOffset - IcoTableOffset) + 1
For iNum1 = 0 to 2
A4(iNum1) = A1(iNum1 + 4)
Next
A4(3) = 0
iSize = GetNumFromBytes(A4) '-- size of icon res.
Boo1 = GetIcoHeader(iSize, s1, sSz) '-- send for icon header.
If (Boo1 = True) Then
s2 = Mid(sRes, ReadOffset, iSize) '-- get icon res.
sIco = s1 & s2 '-- icon = header plus resource data.
Set TS = FSO.CreateTextFile(sFolPath & "\icon" & CStr(iIcos) & sSz & ".ico", True)
TS.Write sIco '-- write the icon file.
TS.Close
Set TS = Nothing
iIcos = iIcos + 1

Else
'-- If Boo1 was false that means that icon is not one of the supported sizes and/or color depths.
'-- See GetIcoHeader notes below.

End If
End If
ReadPt = ReadPt + 8
Next
GetIcons = iIcos

End Function


'-- This function checks to make sure it has a PE file. It then calculates the
'-- offset and size of the resource section. Then it goes into the resource section and finds any icon
'-- resources. Finally, it returns the offset of the icon resource directory table and the number of icons
'-- containded in the file. See the File Version Info script package for a more involved description of
'-- the resource section layout and how this function works. This function was adapted from the version
'-- info function. -
Private Function GetIconTableOffset(sFilePath, iPosition)
Dim sRes, s1, s2, sB, A1, A4(3), LocRes, VLocRes, iOffSet, Boo, Pt1
Dim iNum1, iNum2, iReadPt, iNum3, LocAspack, VLocAspack, VIOffset, ReadOffset, BooAspack
' On Error Resume Next
sRes = ".rsrc"
BooAspack = False
Set TS = FSO.OpenTextFile(sFilePath, 1)
s1 = TS.Read(2048) '-- Read first 2 KB.
TS.Close
Set TS = Nothing
A1 = GetArray(Mid(s1, 61, 2), False) '-- get number value at offset 60 that points to PE signature address.
iNum1 = (GetNumFromBytes(A1) + 1) '-- get offset of "PE00"
'- GetByteString function from "Textstream Binary Ops script is condensed here.
'-- the purpose is to get a readable string to look for PE marker.-
Dim A2(), iA, iLen
ReDim A2(len(s1) - 1)
For iLen = 1 to Len(s1)
iA = Asc(Mid(s1, iLen, 1))
If iA = 0 Then iA = 149 '-- converts 0-byte to bullet
A2(iLen - 1) = Chr(iA)
Next

sB = Join(A2, "") '-- sB is now a string of first 2048 bytes, with Chr(0) replaced by a bullet character to make it readable.
'

If Mid(sB, iNum1, 4) <> "PE" & sBullet & sBullet Then
GetIconTableOffset = -4 '-- no PE signature found.
Exit Function
End If

Pt1 = InStr(1, sB, sRes) '-- find .rsrc table.
If (Pt1 = 0) Then
GetIconTableOffset = -2 'no resource table header found.
Exit Function
End If
Pt1 = Pt1 + 12 '-- size of raw data is 4 bytes at offset of 16 into the .rsrc table.
A1 = GetArray(Mid(s1, Pt1, 12), False) '-- get the same string as a numeric array to Read offset numbers.

'-- get virtual offset of .rsrc section
For iOffSet = 0 to 3
A4(iOffSet) = A1(iOffSet)
Next
VLocRes = GetNumFromBytes(A4)
VResLoc = VLocRes '-- save rsrc virtual offset.

'-- get raw data size of .rsrc section
For iOffSet = 0 to 3
A4(iOffSet) = A1(iOffSet + 4)
Next
SizeRes = GetNumFromBytes(A4) '--size of resource section in bytes.

'-- get raw data offset of .rsrc table
'-- send back resource section size to save work in case this method fails.
For iOffSet = 0 to 3
A4(iOffSet) = A1(iOffSet + 8)
Next
LocRes = GetNumFromBytes(A4) '-- offset location of resource section.
ResLoc = LocRes '-- save raw resource offset.

Pt1 = InStr(1, sB, ".aspack") '-- find .rsrc table.
If (Pt1 > 0) Then
BooAspack = True
Pt1 = Pt1 + 12 '-- size of raw data is 4 bytes at offset of 16 into the .rsrc table.
A1 = GetArray(Mid(s1, Pt1, 12), False)

'-- get virtual offset of aspack section
For iOffSet = 0 to 3
A4(iOffSet) = A1(iOffSet)
Next
VLocAspack = GetNumFromBytes(A4)
VResLoc = VLocAspack
'-- get raw data offset of aspack section
For iOffSet = 0 to 3
A4(iOffSet) = A1(iOffSet + 8)
Next
LocAspack = GetNumFromBytes(A4)
ResLoc = LocAspack
End If
' End special file compression aspack code --

'- start Read for search.
Boo = False
Set TS = FSO.OpenTextFile(sFilePath, 1)
'-- go to rsrc section and look in root directory to find number of resources.
'-- looking for a number resource that's #3.
TS.Skip LocRes + 12 '-- get number of names from bytes 13,14 in top level "Type" directory.
s1 = TS.Read(2) '-- Read bytes 13,14 to get number of named resource types.
iNum1 = Asc(s1) '-- number of names.
s1 = TS.Read(2) '-- Read bytes 15,16 to get number of numbered resource types.
iNum2 = Asc(s1) '-- number of nums.

If (iNum2 = 0) Then '-- no numbered entries. have to quit here.
TS.Close
Set TS = Nothing
GetIconTableOffset = -3 'failed to find icons in resource table.
Exit Function
End If
'-- now at end of root directory. Find icon res. entry.
If (iNum1 > 0) Then TS.Skip (iNum1 * 8) '-- Skip past named entries.
iReadPt = LocRes + 16 + (iNum1 * 8) '-- update file offset variable because this will be needed.
Boo = False

For iOffSet = 1 to iNum2
s1 = TS.Read(8)
iReadPt = iReadPt + 8
If (Asc(s1) = 3) Then '-- an icon directory. '
Boo = True
Exit For
End If
Next
If (Boo = False) Then '-- have to quit. no icon entry found.
TS.Close
Set TS = Nothing
GetIconTableOffset = -3 'failed to find icons in resource table.
Exit Function
End If

'-- this should now be 8 bytes, the last 4 of which point to icon table.
A1 = GetArray(s1, False)
For iNum1 = 0 to 2 '-- get offset number to next level from 2nd 4 bytes of entry structure.
A4(iNum1) = A1(iNum1 + 4)
Next
A4(3) = 0
iNum3 = GetNumFromBytes(A4) '-- iNum3 is now pointer to icon table.
iNum3 = ((ResLoc + iNum3) - iReadPt) + 12 '-- adjust to read number of names and number of numbers.

'-- this is now the icon resource directory table, describing the icons in the file.
TS.Skip iNum3 '-- get number of names from bytes 13,14 in top level "Type" directory.
s1 = TS.Read(2) '-- Read bytes 13,14 to get number of named resource types.
iNum1 = Asc(s1) '-- number of names.
s1 = TS.Read(2) '-- Read bytes 15,16 to get number of numbered resource types.
iNum2 = Asc(s1) '-- number of nums.

iNum1 = iNum1 + iNum2 '-- named or numbered entries in icon table? get both.
iPosition = (iReadPt + iNum3) + 4 '-- return the offset of this table so that it can be read from the file.
TS.Close
Set TS = Nothing
GetIconTableOffset = iNum1 '-- return 0 or number of icons listed.
End Function

'-- Boolean = GetIcoHeader(bytes-in-icon, return-header-string) -
'--
'-- Unfortunately, Microsoft documentation is often murky and partial, forcing people to share tips in
'-- newsgroups rather than look things up in a reference file. The structure of PE files, and the
'-- requirements of this function, are a good example. An icon is stored in PE rersources without the
'-- file header of 22 bytes. I was unable to find any documentation on where the file header might
'-- be kept, if it's kept, or how that should be dealt with.
'-- (To find out about PE file format in general, search for "PE" "COFF" "header" "portable executable")
'-- Microsoft has an article entitled "An In-Depth Look into the Win32 Portable Executable File Format"
'-- by Matt Pietrek, which is fairly informative, though it's hardly in-depth. It skips the details
'-- of the resource section layout altogether, providing only a general description of PE
'-- file format. Microsoft's "Microsoft Portable Executable and Common Object File Format Specification"
'-- downloadable file provides more info. but does not cover details at the level needed here.
'--
'-- Fortunately there was a solution that did not require depending on Microsoft documentation!!
'--
'-- This Function is called after extracting the icon resource. An icon is stored without the
'-- file header of 22 bytes. After failing to find any documentation on where the file header might
'-- be kept, this approach was arrived at: To create the icon header (see info. file for format) it's
'-- necessary to know the color depth and size of the icon. That can be known if one knows
'-- the size of the icon file in bytes. So the way this works is that the script extracts the icon
'-- resource and then sends the length in bytes to this function. If a match is found to that
'-- length then the function returns true and sHead returns the icon header to tack onto the
'-- front of the icon resource bytes in order to reconstruct the icon.
'--
Private Function GetIcoHeader(LenIco, sHead, sSize)
Dim AHead, i3, sIcoH
Select Case LenIco
Case 296 '-- 16x16 16 color.
AHead = Array(0, 0, 1, 0, 1, 0, 16, 16, 16, 0, 1, 0, 4, 0, 40, 1, 0, 0, 22, 0, 0, 0)
sSize = "-16x16C"
Case 1384 '-- 16x16 256 color.
AHead = Array(0, 0, 1, 0, 1, 0, 16, 16, 0, 0, 1, 0, 8, 0, 104, 5, 0, 0, 22, 0, 0, 0)
sSize = "-16x256C"
Case 744 '-- 32x32 16 color.
AHead = Array(0, 0, 1, 0, 1, 0, 32, 32, 16, 0, 1, 0, 4, 0, 232, 2, 0, 0, 22, 0, 0, 0)
sSize = "-32x16C"
Case 872 '-- 16x16 24-bit.
AHead = Array(0, 0, 1, 0, 1, 0, 16, 16, 0, 0, 1, 0, 24, 0, 104, 3, 0, 0, 22, 0, 0, 0)
sSize = "-16xTC"
Case 2216 '-- 32x32 256 color.
AHead = Array(0, 0, 1, 0, 1, 0, 32, 32, 0, 0, 1, 0, 8, 0, 168, 8, 0, 0, 22, 0, 0, 0)
sSize = "-32x256C"
Case 3240 '-- 32x32 24-bit true color.
AHead = Array(0, 0, 1, 0, 1, 0, 32, 32, 0, 0, 1, 0, 24, 0, 168, 12, 0, 0, 22, 0, 0, 0)
sSize = "-32xTC"
Case 1640 '-- 48x48 16 color.
AHead = Array(0, 0, 1, 0, 1, 0, 48, 48, 16, 0, 1, 0, 4, 0, 104, 6, 0, 0, 22, 0, 0, 0)
sSize = "-48x16"
Case 3752 '-- 48x48 256 color.
AHead = Array(0, 0, 1, 0, 1, 0, 48, 48, 0, 0, 1, 0, 8, 0, 168, 14, 0, 0, 22, 0, 0, 0)
sSize = "-48x256"
Case 7336 '-- 48x48 24-bit.
AHead = Array(0, 0, 1, 0, 1, 0, 48, 48, 0, 0, 1, 0, 24, 0, 168, 28, 0, 0, 22, 0, 0, 0)
sSize = "-48xTC"
Case Else
GetIcoHeader = False
Exit Function
End Select
For i3 = 0 to 21
sIcoH = sIcoH & Chr(AHead(i3))
Next
sHead = sIcoH
GetIcoHeader = True
End Function

End Class

TallDude at 2007-9-12 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...