WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System_CodeDom

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Compile Dll from VB Source

 Keywords: Compile DLL Assembly Library VB Source Microsoft.VisualBasic.VBCodeProvider System.CodeDom System.CodeDom.Compiler System.CodeDom.Compiler.CompilerParameters OutputAssembly CompileAssemblyFromSource
System.Reflection 

Creating dotNet assemblies provides a quick and relatively easy way to extend WinBatch functionality, if you have a bit of fluency in one of the dotNet languages. They allow you to create the rough equivalent of a WIL extender without having to master the WIL extender interface or C/C++.
;***************************************************************************
;**   Compile assembly library from VB source.
;**
;** Purpose:  Compile assembly from source.
;** Inputs:   VB source code
;** Outputs:  Compiled VB DLL
;** Reference:
;**       REQUIRES WinBatch 2013A or newer & File
;**
;**
;** Developers: Deana Falk 2013.11.12
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

code = 'Imports Microsoft.VisualBasic':@LF
code = code: 'Imports System':@LF
code = code: 'Namespace ClipboardAddon':@LF
code = code: '  Public Class Utility':@LF
code = code: '    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Integer) As Integer':@LF
code = code: '    Private Declare Function EmptyClipboard Lib "user32" () As Integer':@LF
code = code: '    Private Declare Function CloseClipboard Lib "user32" () As Integer':@LF
code = code: '    Private Declare Function SetClipboardData Lib "user32"(ByVal wFormat As Integer, ByVal hMem As Integer) As Integer':@LF
code = code: '    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Integer, ByVal dwBytes As Integer) As Integer':@LF
code = code: '    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Integer) As Integer':@LF
code = code: '    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Integer) As Integer':@LF
code = code: '    Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Integer, ByVal lpString2 As String) As Integer':@LF
code = code: '':@LF
code = code: '    Public Shared Sub CopyToClipboard(ByVal text As String)':@LF
code = code: '      Dim result As Boolean = False':@LF
code = code: '      Dim mem As Integer = GlobalAlloc(&H42, text.Length + 1)':@LF
code = code: '      Dim lockedmem As Integer = GlobalLock(mem)':@LF
code = code: '      lstrcpy(lockedmem, text)':@LF
code = code: '      If GlobalUnlock(mem) = 0 Then':@LF
code = code: '        If OpenClipboard(0) Then':@LF
code = code: '          EmptyClipboard()':@LF
code = code: '          result = SetClipboardData(1, mem)':@LF
code = code: '          CloseClipboard()':@LF
code = code: '        End If':@LF
code = code: '      End If':@LF
code = code: '    End Sub':@LF
code = code: '  End Class':@LF
code = code: 'End Namespace'

;Add-Type -TypeDefinition $code -Language VisualBasic -OutputType Library -OutputAssembly c:\temp\extension.dll

If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded.
ObjectClrOption('use','System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Prompt for Input
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
assemblydll =  DirScript():'extension.dll'

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
objVB = ObjectClrNew('Microsoft.VisualBasic.VBCodeProvider')
objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
;objParams.ReferencedAssemblies.Add( 'System.dll' )
objParams.OutputAssembly  = assemblydll
;objParams.CompilerOptions = '/optimize'
objResult = objVB.CompileAssemblyFromSource(objParams, BSTR:code)
;; Check for compiler errors.
If objResult.Errors.Count > 0
   ForEach ce In objResult.Errors
      Pause('Error', ce.ToString())
   Next
   Exit
EndIf

Exit
Now that you have create the dll you can then call this dlls method from WinBatch using something like this:
;***************************************************************************
;**   Load assembly then call method
;**
;** Purpose:  Load assembly then call method
;** Inputs:  ModuleName - assembly library name
;** Outputs: Results in clipboard
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**       http://powershell.com/cs/blogs/ebookv2/archive/2012/03/27/chapter-20-loading-net-libraries-and-compiling-code.aspx
;** Developer: Deana Falk 2013.11.12
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;ModuleName = "c:\temp\extension.dll"
;ObjectClrOption( 'use', 'System.Reflection, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')  ;v4.0_4.0.0.0__b03f5f7f11d50a3a
;oAssembly = ObjectClrNew( 'System.Reflection.Assembly' )
;oAssembly = oAssembly.LoadFrom( ModuleName )
;Or
ObjectClrOption( "appbase", "C:\Temp" ) ; Directory that contains "extension.dll"
ObjectClrOption("use", "extension")

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
objUtility = ObjectClrNew("ClipboardAddon.Utility")
objUtility.CopyToClipboard("Hello World")
Exit

Article ID:   W17806
Filename:   Compile Dll from VB Source.txt
File Created: 2013:11:14:09:16:48
Last Updated: 2013:11:14:09:16:48