using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VirtualServer.Interop;
namespace ShowVSVersion
{
/// <summary>
/// Define RPC_C_AUTHN_LEVEL_ constants
/// </summary>
public enum RpcAuthnLevel
{
Default = 0,
None,
Connect,
Call,
Pkt,
PktIntegrity,
PktPrivacy
}
/// <summary>
/// Define RPC_C_IMP_LEVEL_ constants
/// </summary>
public enum RpcImpLevel
{
Default = 0,
Anonymous,
Identify,
Impersonate,
Delegate
}
/// <summary>
/// Define EOAC_ constants
/// </summary>
public enum EoAuthnCap
{
None = 0x0000,
MutualAuth = 0x0001,
StaticCloaking= 0x0020,
DynamicCloaking= 0x0040,
AnyAuthority= 0x0080,
MakeFullSIC= 0x0100,
Default= 0x0800,
SecureRefs= 0x0002,
AccessControl= 0x0004,
AppID= 0x0008,
Dynamic= 0x0010,
RequireFullSIC= 0x0200,
AutoImpersonate= 0x0400,
NoCustomMarshal= 0x2000,
DisableAAA= 0x1000
}
/// <summary>
/// InitVS handles the special COM/DCOM startup code required by
/// the Virtual Server security model.
/// </summary>
public class InitVS
{
// Create the call with PreserveSig:=FALSE so the COM InterOp
// layer will perform the error checking and throw an
// exception instead of returning an HRESULT.
//
[ DllImport( "Ole32.dll",
ExactSpelling=true,
EntryPoint="CoInitializeSecurity",
CallingConvention=CallingConvention.StdCall,
SetLastError=false,
PreserveSig=false) ]
private static extern void CoInitializeSecurity(
IntPtr pVoid,
int cAuthSvc,
IntPtr asAuthSvc,
IntPtr pReserved1,
uint dwAuthnLevel,
uint dwImpLevel,
IntPtr pAuthList,
uint dwCapabilities,
IntPtr pReserved3 );
/// <summary>
/// Call CoInitializeSecurity with dwImpLevel set to
/// Impersonate. Required by the Virtual Server COM Interface.
/// </summary>
public InitVS()
{
CoInitializeSecurity(IntPtr.Zero,
-1,
IntPtr.Zero,
IntPtr.Zero,
(uint)RpcAuthnLevel.PktPrivacy,
(uint)RpcImpLevel.Impersonate,
IntPtr.Zero,
(uint)EoAuthnCap.DynamicCloaking,
IntPtr.Zero );
}
/// <summary>
/// Get VMVirtualServerClass instance from a remote server using DCOM
/// </summary>
/// <param name="server">Remote server name</param>
/// <returns>Remote Virtual Server object class with DCOM enabled</returns>
public VMVirtualServerClass GetVMVirtualServerClass(string server)
{
VMVirtualServerClass GetVMVirtualServerClass_result;
Type typeVSClass;
Type typeDCOM;
object objDCOM;
typeVSClass = typeof(VMVirtualServerClass);
typeDCOM = Type.GetTypeFromCLSID(typeVSClass.GUID,
server,
true);
objDCOM = Activator.CreateInstance(typeDCOM);
GetVMVirtualServerClass_result =
(VMVirtualServerClass) Marshal.CreateWrapperOfType(objDCOM, typeVSClass);
return GetVMVirtualServerClass_result;
}
/// <summary>
/// Get VMVirtualServerClass instance from local server using COM
/// </summary>
/// <returns>Local Virtual Server object class with COM enabled</returns>
public VMVirtualServerClass GetVMVirtualServerClass()
{
VMVirtualServerClass GetVMVirtualServerClass_result;
GetVMVirtualServerClass_result =
new VMVirtualServerClass();
return GetVMVirtualServerClass_result;
}
}
/// <summary>
/// This class wraps the MsgBox function from user32.dll
/// </summary>
public class MyLib
{
// Declares managed prototypes for unmanaged functions.
[ DllImport( "User32.dll",
EntryPoint="MessageBox",
CharSet=CharSet.Auto )]
public static extern int MsgBox( int hWnd,
String text,
String caption,
uint type );
}
/// <summary>
/// Main program module
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main(string[] CmdArgs)
{
// Initialize COM with Impersonate first
InitVS myApp;
myApp = new InitVS();
// Check command line arguments help flag
if (CmdArgs.Length>0)
{
if (CmdArgs[0].Length > 1 &&
(CmdArgs[0].Substring(0,1)=="-" || CmdArgs[0].Substring(0,2)=="/?"))
{
String strHelp = "USAGE: ShowVSVersion {remote_server_name}\n\n";
strHelp += "This utility checks access to the Virtual Server COM object interface. If it\n";
strHelp += "is run without arguments then the local machine is accessed, otherwise the\n";
strHelp += "remote server specified will be accessed using DCOM.";
MyLib.MsgBox( 0, strHelp, "ShowVSVersion", 0);
return;
}
}
// Connect locally or remotely
VMVirtualServer myVS;
try
{
if (CmdArgs.Length>0)
myVS = myApp.GetVMVirtualServerClass(CmdArgs[0]);
else
myVS = myApp.GetVMVirtualServerClass();
}
catch
{
MyLib.MsgBox(0,
"Cannot connect to Virtual Server",
"ShowVSVersion",
0);
return;
}
// Get Virtual Server name and version number
string sName;
string sVersion;
sName = myVS.Name;
sVersion = myVS.Version;
MyLib.MsgBox(0,
sName + " version " + sVersion,
"ShowVSVersion",
0);
}
}
}