none
This is how to turn vbs and js files into exe files from the command line without third party tools RRS feed

  • Question

  • How to turn vbs and js files into exe files from the command line

    Any COM Automation programming language (that's all of them but especially VB6 which is what this object was designed for - it also applies to vbscript too).

    Use the MS Script Control.

    For those that don't have the great VB6 (a member of the same family as vbscript) here's how to make an exe with a script from the command line and notepad.
     
    Create text file on desktop and name it VB2Exe.vb

    The Scrpt string variable contains your script.

    <code>
    Public Module MyApplication 
    Public Sub Main() 
    Dim sc as object
    Dim Scrpt as string
    sc = createObject("MSScriptControl.ScriptControl")
    Scrpt = "msgbox " & chr(34) & "Hi there" & chr(34)
         With SC
            .Language = "VBScript"
            .UseSafeSubset = False
            .AllowUI = True
     End With

    sc.addcode(Scrpt)

    End Sub
    End Module
    </code>

    Looks fairly familar.

    The .NET framework comes with compilers.


    You need to make sure the path to the compiler is correct on your windows install.
     
    In an elevated command prompt type

    <code>
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:exe "%userprofile%\desktop\VB2Exe.vb"
    </code>


    Your first VBScript in an exe.


    David Candy



    • Edited by DavidMCandy Saturday, October 5, 2013 12:03 AM
    Friday, October 4, 2013 11:16 PM

All replies

  • Here's sample vbs code that syntax checks scripts. But the global code does run in the script control. (even though I'm using executeglobal to run it for real).

    It runs the vbs script specified against every line in a textstream (Inp) writing it to another textstream (Outp).

    Ag(1) is the script to be added. I add extra script to the users script.

    Note the syntax error  checking and messages to the user.

    Sub VBSCmd
     RawScript = Arg(1)
     'Remove ^ from quoting command line and replace : with vbcrlf so get line number if error
     Script = Replace(RawScript, "^", "")
     Script = Replace(Script, "'", chr(34))
     Script = Replace(Script, ":", vbcrlf)
     'Building the script with predefined statements and the user's code
     Script = "Dim gU" & vbcrlf & "Dim gdU" & vbcrlf & "Set gdU = CreateObject(" & chr(34) & "Scripting.Dictionary" & chr(34) & ")" & vbcrlf & "Function UF(L, LC)" & vbcrlf & "Set greU = New RegExp" & vbcrlf & "On Error Resume Next" & vbcrlf & Script & vbcrlf & "End Function" & vbcrlf
     'Testing the script for syntax errors
     On Error Resume Next
     set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
         With ScriptControl1
            .Language = "VBScript"
            .UseSafeSubset = False
            .AllowUI = True
      .AddCode Script
     End With
     With ScriptControl1.Error
      If .number <> 0 then
       Outp.WriteBlankLines(1)
       Outp.WriteLine "User function syntax error"
       Outp.WriteLine "=========================="
       Outp.WriteBlankLines(1)
       Outp.Write NumberScript(Script)
       Outp.WriteBlankLines(2)
       Outp.WriteLine "Error " & .number & " " & .description
       Outp.WriteLine "Line " & .line & " " & "Col " & .column
       Exit Sub
      End If
     End With
     ExecuteGlobal(Script)
     'Remove the first line as the parameters are the first line
     'Line=Inp.readline 
     Do Until Inp.AtEndOfStream
      Line=Inp.readline
      LineCount = Inp.Line
      
      temp = UF(Line, LineCount)
      If err.number <> 0 then
       outp.writeline ""
       outp.writeline ""
       outp.writeline "User function runtime error"
       outp.writeline "==========================="
       Outp.WriteBlankLines(1)
       Outp.Write NumberScript(Script)
       Outp.WriteBlankLines(2)
       Outp.WriteLine "Error " & err.number & " " & err.description
       Outp.WriteLine "Source " & err.source
       Outp.WriteLine "Line number and column not available for runtime errors"
       wscript.quit
      End If
      outp.writeline temp
     Loop
    End Sub

    Function NumberScript(Text)
     Text = Replace(Text, vbcr, "")
     LineArray=Split(Text,vblf)
     For i = 0 to UBound(LineArray, 1) - 1

      Count = Count + 1
      Temp = Temp & vbcrlf & Count & " " & LineArray(i)
     Next
     NumberScript = Temp
    End Function


    David Candy

    Friday, October 4, 2013 11:26 PM
  • For a Windows Forms version (no console window and we don't get around to actually creating any forms - though you can if you want).
     
    Compile line in a command prompt.

    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:winexe "%userprofile%\desktop\VBS2Exe.vb"

    Text for VBS2EXE.vb

    Imports System.Windows.Forms 
    Partial Class MyForm : Inherits Form 
    Private Sub InitializeComponent() 
     
    End Sub
    Public Sub New() 
     InitializeComponent() 
    End Sub
    Public Shared Sub Main() 
    Dim sc as object 
    Dim Scrpt as string
    sc = createObject("MSScriptControl.ScriptControl")
    Scrpt = "msgbox " & chr(34) & "Hi there I'm a form" & chr(34)
         With SC 
            .Language = "VBScript"
            .UseSafeSubset = False
            .AllowUI = True
     End With
     
    sc.addcode(Scrpt)
     
    End Sub
    End Class

    Using these optional parameters gives you an icon and manifest. A manifest allows you to specify run as normal, run elevated if admin, only run elevated.
     
    /win32icon:<file> Specifies a Win32 icon file (.ico) for the default Win32 resources.
     
    /win32manifest:<file> The provided file is embedded in the manifest section of the output PE.
     
    In theory, I have UAC off so can't test, but put this text file on the desktop and call it vbs2exe.manifest, save as UTF-8.

    The command line
     
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:winexe /win32manifest:"%userprofile%\desktop\VBS2Exe.manifest" "%userprofile%\desktop\VBS2Exe.vb"
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity
        version="1.0.0.0"
        processorArchitecture="*"
        name="VBS2EXE"
        type="win32"
    />
    <description>Script to Exe</description>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> 
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel
                    level="requireAdministrator"
                    uiAccess="false"
                />
            </requestedPrivileges>
        </security>
    </trustInfo>
    </assembly>
     

     Hopefully it will now ONLY run as admin.


    David Candy


    • Edited by DavidMCandy Saturday, October 5, 2013 9:28 AM
    Saturday, October 5, 2013 7:48 AM
  • Give Access To a Host's Objects 
     
    Here's an example giving the vbscript access to a .NET object.
     
    Imports System.Windows.Forms 
     
    Partial Class MyForm : Inherits Form 
     
    Private Sub InitializeComponent() 
     

    End Sub
     
    Public Sub New() 
     
     InitializeComponent() 
     
    End Sub
     
    Public Shared Sub Main() 
     
    Dim sc as object
    Dim Scrpt as string
     
    sc = createObject("MSScriptControl.ScriptControl")
     
    Scrpt = "msgbox " & chr(34) & "Hi there I'm a form" & chr(34) & ":msgbox meScript.state"
     
         With SC
            .Language = "VBScript"
            .UseSafeSubset = False
            .AllowUI = True
            .addobject("meScript", SC, true)
     End With
     

    sc.addcode(Scrpt)
     

    End Sub
     
    End Class

    To Embed version info

    Download vbs2exe.res file from https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121 and put on desktop.

    Download ResHacker from http://www.angusj.com/resourcehacker/

    Open vbs2exe.res file in ResHacker. Edit away. Click Compile button. Click File menu - Save.

    Type

    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:winexe /win32manifest:"%userprofile%\desktop\VBS2Exe.manifest" /win32resource:"%userprofile%\desktop\VBS2Exe.res" "%userprofile%\desktop\VBS2Exe.vb"

     


    David Candy

    Saturday, October 5, 2013 10:04 AM