Jim Lawless' Blog


WSH2EXE - The Final Chapter

Originally published on: Sun, 31 May 2009 22:24:50 +0000

I've decided to change the path that I had started in the first two installments of this WSH2EXE series to take advantage of some modern conveniences with the .NET Common Language Runtime (CLR).

The first two installments in this series can be read here:

http://www.mailsend-online.com/blog?p=4 http://www.mailsend-online.com/blog?p=5

The first advantage is that each .NET CLR contains working compilers that can be invoked from within a .NET program by using classes in the System.CodeDom.Compiler hierarchy. These classes can generate full EXE's ( assemblies ) with embedded Windows Resource files.

The next advantage is that the System.Resources.ResourceWriter class can be used to dynamically create a resource file that can be embedded into an assembly with the Compiler classes above.

To finish up WSH2EXE, we'll discard the idea of using a stub WSH script with the main code in an environment variable and will just write the full WSH script to a temporary file and will then invoke it with the appropriate WSH interpreter EXE ( Cscript or WScript ).

The following list of options illustrate the command-line parameters WSH2EXE will accept:


wsh2exe v2.00
by Jim Lawless
Usage:
        wsh2exe [options]
Where options are:
   -i scriptName ; such as c:\test.js (required)
   -o ExeName ; output EXE name (required)
   -w ; Invoke with wscript.exe
   -c ; [default] Invoke with cscript.exe
   -useTemp ; [default] Use a temporary directory for WSH script
   -useCurrent ; Use current directory for WSH script
   -useAbs path ; Use an absolute directory path for WSH script
   -cleanUp ; [default] Delete the script when EXE finishes.
   -noCleanUp ; Leave the script intact when EXE finishes.
   -cmd parms ; additional cscript/wscript parms such as /nologo

The program WSH2EXE will write several different parameters ( and the complete script source ) as individual strings to a temporary resources file. The output EXE program ( the end result of compilng wsh2exestub.cs and embedding the resources ) will then read these resources, will write the WSH script to a work directory and will execute it with the appropriate WSH interpreter.

The stub EXE C# source code appears below:

wsh2exestub.cs


// wsh2exestub.cs
// stub for Wsh2Exe

// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System ;
using System.Diagnostics;
using System.IO ;
using System.Resources;
using System.Reflection;
using System.Windows.Forms;

namespace Wsh2ExeStub
{
   class Config
   {
      public string ScriptName;
      public string ActualScript;
      public string WSHHost;
      public bool UseTemp;
      public bool UseCurrent;
      public string AbsolutePath;
      public bool ShouldCleanUp;
      public string ExtraParms;
   }


public class Wsh2ExeStub
   {
      public static void Main(string[] args)
      {
         new Wsh2ExeStub();
      }
      public Wsh2ExeStub()
      {
         try
         {
            Assembly resassembly=this.GetType().Assembly;
            Config config=new Config();
            string cmdString;
            string scriptDir;
            ResourceManager res=new ResourceManager("wsh2exestub",resassembly);
            config.ScriptName=res.GetString("ScriptName");
            config.ActualScript=res.GetString("ActualScript");
            config.WSHHost=res.GetString("WSHHost");
            config.UseTemp=(res.GetString("UseTemp")=="1" );
            config.UseCurrent=(res.GetString("UseCurrent")=="1");
            config.AbsolutePath=res.GetString("AbsolutePath");
            config.ShouldCleanUp=(res.GetString("ShouldCleanUp")=="1");
            config.ExtraParms=res.GetString("ExtraParms");
            
            if(config.UseTemp)
               scriptDir=System.IO.Path.GetTempPath();
            else
            if(config.UseCurrent)
               scriptDir="";
            else
               scriptDir=config.AbsolutePath;

            TextWriter tw=new StreamWriter(scriptDir+config.ScriptName);
            tw.Write(config.ActualScript);
            tw.Close();
            cmdString=config.ExtraParms + "\"" + scriptDir + config.ScriptName + "\"";
            Process p=Process.Start(config.WSHHost,cmdString);
p.WaitForExit();
if(config.ShouldCleanUp)
File.Delete(scriptDir+config.ScriptName);

         }
         catch(Exception e)
         {
            MessageBox.Show("Error :" + e.ToString());
         }
      }
   }
}

This file wsh2exestub.cs must be present in the same directory as WSH2EXE when the target EXE is created.

The main code to build the resources file and then compile the stub ( embedding in the just-generated resources ) appears below:

wsh2exe.cs


// wsh2exe - Embed JavaScript or VBScript scripts into an EXE.

// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Resources;

namespace Wsh2Exe
{

   class Config
   {
      public string ScriptName;
      public string ActualScript;
      public string ExeName;
      public string WSHHost;
      public bool UseTemp;
      public bool UseCurrent;
      public string AbsolutePath;
      public bool ShouldCleanUp;
      public string ExtraParms;
   }

   class Wsh2Exe
   {
      public static string Version = "wsh2exe v2.00";
      public static void Main(string[] args)
      {
         CodeDomProvider provider = null;
         Config config;
         try
         {
            config=ProcessCommandLine(args);
            if((config.ScriptName==null)||(config.ExeName==null))
               Environment.Exit(1);
            if (CodeDomProvider.IsDefinedLanguage("cs"))
               provider = CodeDomProvider.CreateProvider("cs");
            if (provider == null)
               Console.WriteLine("There is no CodeDomProvider for the input language.");
            else {
               config.ActualScript=ReadInputScript(config.ScriptName);
               WriteResources(config);
               if (CompileCode(provider, "wsh2exestub.cs", config.ExeName ))
                  Console.WriteLine("Compilation successful.");
            }
         }
         catch(Exception e)
         {
            Console.Error.WriteLine(e.ToString());
            Environment.Exit(1);
         }
         Environment.Exit(0);
      }
      
      public static void WriteResources(Config config)
      {
         IResourceWriter writer = new ResourceWriter("wsh2exestub.resources");
         writer.AddResource("ScriptName",config.ScriptName);
         writer.AddResource("ActualScript", config.ActualScript);
         writer.AddResource("WSHHost",config.WSHHost);
         writer.AddResource("UseTemp",config.UseTemp ? "1" : "0" );
         writer.AddResource("UseCurrent",config.UseCurrent ? "1" : "0" );
         writer.AddResource("AbsolutePath",config.AbsolutePath);
         writer.AddResource("ShouldCleanUp",config.ShouldCleanUp ? "1" : "0");
         writer.AddResource("ExtraParms",config.ExtraParms);
         writer.Close();
      }
      
      public static string ReadInputScript(string scriptName)
      {
         string s;
         TextReader tr = new StreamReader(scriptName);
         s=tr.ReadToEnd();
         tr.Close();
         return s;
      }
      
      public static bool CompileCode(CodeDomProvider provider,
         String sourceFile, String exeFile)
      {
         CompilerParameters cparms = new CompilerParameters();
         cparms.GenerateExecutable = true;
         cparms.OutputAssembly = exeFile;
         cparms.IncludeDebugInformation = false;
         cparms.ReferencedAssemblies.Add( "System.dll" );
         cparms.ReferencedAssemblies.Add( "System.Windows.Forms.dll" );
         cparms.GenerateInMemory = false;
         cparms.WarningLevel = 3;
         cparms.TreatWarningsAsErrors = false;
         cparms.CompilerOptions = "/optimize /target:winexe";
         cparms.TempFiles = new TempFileCollection(".", false);
         if (provider.Supports(GeneratorSupport.EntryPointMethod))
             cparms.MainClass = "Wsh2ExeStub.Wsh2ExeStub";
         if (provider.Supports(GeneratorSupport.Resources))
             cparms.EmbeddedResources.Add("wsh2exestub.resources");

         CompilerResults cres = provider.CompileAssemblyFromFile(cparms, sourceFile);
         if(cres.Errors.Count > 0)
         {
            Console.WriteLine("Errors building wsh2exestub.");
            foreach(CompilerError ce in cres.Errors)
            {
               Console.WriteLine(" {0}", ce.ToString());
               Console.WriteLine();
            }
         }
         return (cres.Errors.Count ==0);
      }

      public static Config ProcessCommandLine(string[] args)
      {
         int i;
         Config config=new Config();
         config.WSHHost="cscript.exe";
         config.UseTemp=true;
         config.UseCurrent=false;
         config.ShouldCleanUp=true;
         config.AbsolutePath="";
         config.ExtraParms="";
         
         for(i=0;i<args.Length;i++) {
            if(string.Compare(args[i],"-i",true)==0) {
               config.ScriptName=args[++i];
            }
            else
            if(string.Compare(args[i],"-o",true)==0) {
               config.ExeName=args[++i];
            }
            else
            if(string.Compare(args[i],"-w",true)==0) {
               config.WSHHost="wscript.exe";
            }
            else
            if(string.Compare(args[i],"-c",true)==0) {
               config.WSHHost="cscript.exe";
            }
            else
            if(string.Compare(args[i],"-useTemp",true)==0) {
               config.UseTemp=true;
            }
            else
            if(string.Compare(args[i],"-useCurrent",true)==0) {
               config.UseCurrent=true;
               config.UseTemp=false;
            }
            else
            if(string.Compare(args[i],"-useAbs",true)==0) {
               config.AbsolutePath=args[++i];
               config.UseTemp=false;
            }
            else
            if(string.Compare(args[i],"-cleanUp",true)==0) {
               config.ShouldCleanUp=true;
            }
            else
            if(string.Compare(args[i],"-noCleanUp",true)==0) {
               config.ShouldCleanUp=false;
            }
            else
            if(string.Compare(args[i],"-cmd",true)==0) {
               config.ExtraParms=args[++i];
            }
         }

         if((config.ScriptName==null)||(config.ExeName==null)) {
            Console.WriteLine(Wsh2Exe.Version + "\nby Jim Lawless");
            Console.WriteLine("Usage:\n\twsh2exe [options]\nWhere options are:");
            Console.WriteLine(" -i scriptName ; such as c:\\test.js (required)");
            Console.WriteLine(" -o ExeName ; output EXE name (required)");
            Console.WriteLine(" -w ; Invoke with wscript.exe");
            Console.WriteLine(" -c ; [default] Invoke with cscript.exe");
            Console.WriteLine(" -useTemp ; [default] Use a temporary directory for WSH script");
            Console.WriteLine(" -useCurrent ; Use current directory for WSH script");
            Console.WriteLine(" -useAbs path ; Use an absolute directory path for WSH script");
            Console.WriteLine(" -cleanUp ; [default] Delete the script when EXE finishes. ");
            Console.WriteLine(" -noCleanUp ; Leave the script intact when EXE finishes. ");
            Console.WriteLine(" -cmd parms ; additional cscript/wscript parms such as /nologo");
         }
         return config;
      }
   }
}

To try out the WSH2EXE, write a sample VBScript or JavaScript WSH program:

test.js


WScript.Echo("test");

Then, issue the following command-line: wsh2exe -i test.js -o test.exe -useTemp -w

Then, execute test.exe. You should see a message box with the text "test".

Please note that I've provided a few paramters so that you can control the work directory used to temporarily house the WSH script as it runs and have provided a parameter that will ( by default ) delete the WSH file after execution.

You may download the source and .NET 2.0 EXE for WSH2EXE here:

http://www.mailsend-online.com/wp/wsh2exe.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Twimmando: A Command-line Twitter Client
Next post:Site Tracking with Perl


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Changing the C64 Text Color in C

Checking Shift States with DEBUG

Setting Windows Console Text Colors in C

JRuby as a Java Obfuscation Utility

An SMTP Server Simulator in Perl

Scrolling GIF Banners with PerlMagick

A Command-Line MP3 Player for Windows

Choose your own Adventure with Sinatra

Preserving my Favorite HN Links

A Command-Line CD Tray Opener


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service