HOWTO: Dynamically create, run, and delete a .NET console application from WSH 
Author Message
 HOWTO: Dynamically create, run, and delete a .NET console application from WSH

One of the oldest tricks around for high-level applications on Unix/Linux boxes
has been to allows users to extend them by creating compiled applications on the
fly.

Since the .NET redistributable include compilers for C# and VB.NET, this trick
can be exploited by WSH users as well now.  All you need is the .NET
redistributable (NOT the SDK or development framework).  Below is a simple
wrapper class that puts code you give it into a VB.NET console application,
compiles it, runs it and captures the output, then deletes the source file and
exe created.

*******************
VB.NET source code - this is already in the script below in escaped form.  It
just demos calling the GetTickCount API to give an idea of what can be done with
this technique.  It DOES make API calls pretty easy...

Module Main
    Public Declare Function GetTickCount Lib "kernel32" () As Long
    Sub Main()
        System.Console.WriteLine(GetTickCount)
    End Sub
End Module

*******************
And the script...
*******************
' VBNetWrap.vbs
' you can get the code a variety of ways, including reading a file
' this is just to wrap this all into a package...
code = "Module%20Main%0D%0APublic%20Declare%20Function%20" _
 & "GetTickCount%20Lib%20%22kernel32%22%20%28%29%20As%20Long" _
 & "%0D%0ASub%20Main%28%29%0D%0ASystem.Console.WriteLine%28" _
 & "GetTickCount%29%0D%0AEnd%20Sub%0D%0AEnd%20Module"

' there are only 3 steps to dynamically creating and using a
' .NET console application from WSH with the class below.

' 1 - instantiate the class
set vbn = new VbNetApp

' 2 - add source code for a COMPLETE console application to it
vbn.code = unescape(code)

' 3 - execute!
WScript.Echo vbn.exec

Class VbNetApp

 dim sh, fso
 dim m_base, m_code, m_cmd

 public property let code(sData)
  'sets the code to use
  m_code = sData
 end property

 public property get code()
  code = m_code
 end property

 private sub class_initialize
  Set sh = createobject("WScript.Shell")
  Set fso = createobject("Scripting.FileSystemObject")
  m_base = RandomName8
  m_cmd = "vbc /nowarn /nologo /quiet /debug- " & m_base _
   & ".vb && " & m_base & ".exe"
 end sub

 private function Cmd(cmdline)
  ' Wrapper for getting StdOut from a console command
  Dim fOut, OutF, sCmd
  fOut = fso.GetTempName
  sCmd = "%COMSPEC% /c " & cmdline & " >" & fOut
  Sh.Run sCmd, 0, True
  If fso.FileExists(fOut) Then
   If fso.GetFile(fOut).Size>0 Then
    Set OutF = fso.OpenTextFile(fOut)
    Cmd = OutF.Readall
    OutF.Close
   End If
   fso.DeleteFile(fOut)
  End If
 End Function

 Public Function Exec
  ' writing, execution, and deletion
  ' all occur in the same call to ensure
  ' that directory changes don't occur.
  WriteFile m_base & ".vb", m_code
  Exec = Cmd(m_cmd)
  fso.DeleteFile m_base & ".vb", true
  fso.DeleteFile m_base & ".exe", true
 End Function

 Private Sub WriteFile(FilePath, sData)
  'writes sData to FilePath
  With fso.OpenTextFile(FilePath, 2, True)
   .Write sData
   .Close
  End With
 End Sub

 private function RandomName8
  ' Returns unique 8-character name
  ' Following sequence allows 5,352,009,260,481 unique items
  chrList = _
   "abcdefghijklmnopqrstuvwxyz0123456789~-_"
  uLimit = Len(chrList)
  Randomize
  For i = 1 To 8
   sTmp = sTmp & Mid(chrList, ((uLimit) * Rnd + 1), 1)
  Next
  RandomName8 = sTmp
 End Function

End Class



Fri, 19 Aug 2005 23:15:44 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Check Application running on console

2. running console app in same console

3. how to delete dynamically created controls

4. how to add a net use /delete to a WSH script

5. run net.exe (net send) with external commands

6. Howto : Run .bat from WSH script -> output in same console ?

7. Run .NET script code and handle .NET events from inside IE with VsaControl

8. Running Applications with WSH

9. Dialog Box before running an application from HTML page with WSH

10. How to Quit a running application from wsh

11. Checking to see if an application is running with WSH

12. Stopping a hidden console application

 

 
Powered by phpBB® Forum Software