Developing VM-Typer (new series)

The other weekend I was playing around with Virtual Server - and installing Windows Server 2003 in a virtual machine. When I got to the stage where I needed to enter a product key - I thought: "How annoying - I wish there was an easy way to just copy and paste stuff like this into the virtual machine". Three hours later I had finished putting together a program that allowed me to do just that - called "VM-Typer" (now I just have to perform about 500 more installations to get those three hours back :-). Over the next couple of weeks I will outline what was involved.

The first task was to put together some basic UI:

VMTyper UI  

And then to make sure that the application:

  1. Uses the MTA model (as discussed here: https://blogs.msdn.com/virtual_pc_guy/archive/2005/05/12/416889.aspx )
  2. Calls CoInitializeSecurity before launching the main application (as discussed here: https://blogs.msdn.com/virtual_pc_guy/archive/2005/05/05/415135.aspx )

As this is a relatively simple program which does not need any STA components - I decided to just set the entire application to use the MTA model:

Imports

System.Reflection
Imports System.Runtime.InteropServices
Imports Microsoft.VirtualServer.Interop

Public

Class Form1

Inherits System.Windows.Forms.Form

'Virtual Server Connection
Private virtualServer As VMVirtualServer = Nothing

'Constants for CoInitializeSecurity
Private Const RPC_C_AUTHN_LEVEL_PKT_PRIVACY As Long = 6
Private Const RPC_C_IMP_LEVEL_IMPERSONATE As Long = 3
Private Const EOAC_DYNAMIC_CLOAKING As Long = 64

#Region "CoInitializeSecurity"
' Create a SUB with PreserveSig:=FALSE so the COM InterOp layer will
' perform the error checking and throw an exception, instead of using
' a Function and returning an HRESULT.
<DllImport("ole32.dll", _
PreserveSig:=False, _
ExactSpelling:=True, _
EntryPoint:="CoInitializeSecurity", _
CallingConvention:=CallingConvention.StdCall, _
SetlastError:=False)> _
Private Shared Sub CoInitializeSecurity( _
ByVal pSD As IntPtr, _
ByVal cAuthSvc As Int32, _
ByVal asAuthSvc As IntPtr, _
ByVal pReserved1 As IntPtr, _
ByVal dwAuthnlevel As UInt32, _
ByVal dwImpLevel As UInt32, _
ByVal pAuthInfo As IntPtr, _
ByVal dwCapabilities As UInt32, _
ByVal pvReserved2 As IntPtr)
End Sub

#End Region

#Region " Windows Form Designer generated code "
#End Region

'Enable MTA thread apartment model
<MTAThread()> _
Shared Sub main()

   'Enable XP Style skins
Application.EnableVisualStyles()

   'CoInitializeSecurity
   CoInitializeSecurity(System.IntPtr.Zero, -1, System.IntPtr.Zero, System.IntPtr.Zero, _
Convert.ToUInt32(RPC_C_AUTHN_LEVEL_PKT_PRIVACY), _
Convert.ToUInt32(RPC_C_IMP_LEVEL_IMPERSONATE), System.IntPtr.Zero, _
Convert.ToUInt32(EOAC_DYNAMIC_CLOAKING), System.IntPtr.Zero)

   'Start the application once CoInitializeSecurity has been called
Application.Run(New Form1)

   End Sub

Next week we will take this base and start working on connecting to Virtual Server and getting the information we need.

Cheers,
Ben