Experimental Technique: automating COM class creation with C# and VB.NET command line compilers 
Author Message
 Experimental Technique: automating COM class creation with C# and VB.NET command line compilers

Thanks to some invaluable advice from Mattias Sj?gren, I've finally gotten
command-line compilation of .NET classes to work easily.

It takes several steps and this particular method is crude (it involves
decompiling/recompiling the libraries) but it will work.  Consider it
experimental, though.

The process involves using a batch build for a class file.  Below are :

A - The files you need for the demo:
(A1) - the vbmake.cmd file,  which can be used with any VB.NET class.
(A2) - shellnet.vb, a VB.NET class file to compile as our test.
(A3) - test.vbs., a VBScript to test it out.

B - Instructions on using them.

==========================================
==========================================
A. - FILES FOR VB.NET COM ACCESS DEMO
==========================================
==========================================

'8<--------- START vbmake.cmd ---------

sn.exe -k %1.key
vbc /t:library /out:%1.dll %1.vb /keyfile:%1.key
regasm /tlb:%1.tlb %1.dll
gacutil -i %1.dll

Echo.
Echo If the build ran with no errors, any public classes
Echo in the source file can be instantiated by name.

.

'8<--------- END vbmake.cmd ---------

==========================================

'8<--------- START shellnet.vb ---------

Option Explicit On
Imports Environment = System.Environment

Namespace aka
 Public Class Shell
  Public ReadOnly Property MachineName() As String
   'Gets the NetBIOS name of this local computer.
   Get
    return Environment.MachineName
   End Get
  End Property

  Public ReadOnly Property CLRVersion() As String
   ' Gets a Version object that describes the
   ' major, minor, build, and revision numbers
   ' of the common language runtime.
   Get
    return Environment.Version.ToString
   End Get
  End Property

  Public ReadOnly Property UserDomainName() As String
   Get
    return Environment.UserDomainName
   End Get
  End Property

  Public ReadOnly Property UserName() As String
   'Gets the name of the user who started the thread.
   Get
    return Environment.UserName
   End Get
  End Property

  Public ReadOnly Property HasShutdownStarted() As boolean
   ' Indicates whether the CLR or app domain is
   ' shutting down or unloading.
   Get
    return Environment.HasShutdownStarted
   End Get
  End Property

  Public ReadOnly Property OSVersion() As String
   Get
    return Environment.OSVersion.ToString
   End Get
  End Property

  Public ReadOnly Property TickCount() As Integer
   Get
    return Environment.TickCount
   End Get
  End Property

  Public ReadOnly Property UserInteractive() As Boolean
   Get
    return Environment.UserInteractive
   End Get
  End Property

  Public ReadOnly Property WorkingSet() As Integer
   Get
    return Environment.WorkingSet
   End Get
  End Property

  Public ReadOnly Property CommandLine() As String
   Get
    return Environment.CommandLine
   End Get
  End Property

  public Function ExpandEnvironmentVariables( _
   sData as string) As String
   ExpandEnvironmentVariables = Environment. _
    ExpandEnvironmentVariables(sData)
  End Function

  Public Property CurrentDirectory() As String
   Get
    return Environment.CurrentDirectory
   End Get
   Set
    Environment.CurrentDirectory = value
   End Set
  End Property
 End Class

End Namespace

'8<--------- END shellnet.vb ---------

==========================================

'8<--------- START test.vbs ---------

set sh = CreateObject("aka.shell")

WScript.Echo "CLR Version", sh.CLRVersion
WScript.Echo "CommandLine", sh.CommandLine
WScript.Echo "CurrentDirectory", sh.CurrentDirectory
WScript.Echo "ExpandEnvironmentVariables", _
 sh.ExpandEnvironmentVariables("current path is %path%")
WScript.Echo "HasShutdownStarted", sh.HasShutdownStarted
WScript.Echo "MachineName", sh.MachineName
WScript.Echo "OSVersion", sh.OSVersion
WScript.Echo "TickCount", sh.TickCount
WScript.Echo "UserDomainName", sh.UserDomainName
WScript.Echo "UserInteractive", sh.UserInteractive
WScript.Echo "UserName", sh.UserName
WScript.Echo "WorkingSet", sh.WorkingSet

'8<--------- END test.vbs ---------

==========================================
==========================================
B. - USAGE INSTRUCTIONS
==========================================
==========================================

1. Save the data in appropriately named files - vbmake.cmd (or vbmake.bat if you
prefer), shellnet.vb, and test.vbs.

2. Compile shellnet.vb by running the following command line:

        vbmake shellnet

note that I did _not_ include the extension for shellnet.

<Gory_Tech_Details_Of_Possible_Interest>
You don't need to read this part to make it work, but here's what happens.  We
need a "strong name" for COM registration; sn.exe generates a key file
containing such a name.
    sn.exe -k %1.key
Then we compile the file to a DLL and sign it with the strong name so it can be
made COM-accessible:
    vbc /t:library /out:%1.dll %1.vb /keyfile:%1.key
Next we export a typelib and register it - this is the step where we are finally
theoretically COM-accessible:
    regasm /tlb:%1.tlb %1.dll
Finally, we put the file in the global assembly cache to make it accessible:
    gacutil -i %1.dll
</Gory_Tech_Details_Of_Possible_Interest>

3. Run test.vbs; it should echo out some handy information, all derived from the
.NET base library.

--
Please respond in the newsgroup so everyone may benefit.
  http://www.*-*-*.com/
(email requests for support contract information welcomed)
 ----------
 Subscribe to Microsoft's Security Bulletins:
  http://www.*-*-*.com/



Fri, 26 Aug 2005 12:45:59 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Experimental Technique: automating COM class creation with C# and VB.NET command line compilers

2. Automating/simplifying vb script creation

3. Automating/simplifying vb script creation

4. Net View command in VB script?

5. COM - accessing the correct class version in VB Script

6. Download the .Net SDK today! Including the JSCript, VB, and C# command-line compilers.

7. C# and ASP.Net

8. Download the .Net SDK today! Including the JSCript, VB, and C# command-line compilers.

9. Download the .Net SDK today! Including the JSCript, VB, and C# command-line compilers.

10. HELP NEEDED: ShowModalDialog ASP.NET (C#)

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

12. Kludge: C# classes, Object types, and VBScript Variants

 

 
Powered by phpBB® Forum Software