how to use a COM object for Microsoft Virtual server

Discussion about the open-source Phalanger [?] project.

how to use a COM object for Microsoft Virtual server

Postby techscorpio@codeplex » September 4th, 2006, 12:52 pm

Hi,

i am trying to develop a php script for administration of Microsoft Virtual server 2005 r2. i am familer with VB script

Set objVS = CreateObject("VirtualServer.Application")
Set objVM = objVS.FindVirtualMachine("VM Name")
objVM.Startup()
It Worked fine
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ___
PHP Script:

$objVS = new COM("VirtualServer.Application");
$objVM = $objVS->FindVirtualMachine("Scorpio");
$objVM->Startup();

when i compiled through phalanger it throughs run time error saying
ERROR: COM Class not found !!
Thnaks in advance


techscorpio@codeplex
 
Posts: 11
Joined: January 7th, 2012, 8:57 pm

RE: how to use a COM object for Microsoft Virtual server

Postby Lada Prosek » September 4th, 2006, 5:23 pm

Hi,

Phalanger currently uses a native PHP extension to provide access to COM. Try adding

<add assembly="php_com.mng, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4ef6ed87c53048a3" section="com" />

under the <classLibrary> element in your $WINDIR$\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config file or in the web.config file of your application.

That will "import" the COM class.
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

RE: how to use a COM object for Microsoft Virtual server

Postby Lada Prosek » September 4th, 2006, 5:53 pm

I just discovered a bug that is likely to surface when you play with COM. If you encounter this

Unhandled Exception: PHP.Core.PhpNetInternalException: Guarded call ---> System.
InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
at PHP.Core.Reflection.DObject.SetPropertyDirect(Object name, Object value)
at PHP.ExtManager.PhpMarshaler.MarshalNativeToManaged(IntPtr pNativeData)

please let me know. Will be fixed in the next build.
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

RE: how to use a COM object for Microsoft Virtual server

Postby techscorpio@codeplex » September 8th, 2006, 6:50 am

Thanks I will try!!
techscorpio@codeplex
 
Posts: 11
Joined: January 7th, 2012, 8:57 pm

RE: how to use a COM object for Microsoft Virtual server

Postby techscorpio@codeplex » September 8th, 2006, 7:33 am

Hi,
I worte this application when i compiled it will through runtime exception at creating the object $objVM.plese help me.

<?php
f1();
function f1()
{
$objVS=new COM("VirtualServer.Application");
try{
$objVM=$objVS->CreateVirtualMachine("Mahantesh","D:\VMachines");
}catch(Exception $e)
{
echo("EROR:".$e);
}
$objVM->Memory=384;
echo("\n DOne");
}
Error is like this:

C:\PROGRA~1\PHALAN~1.0\Samples\VMSCRI~1\bin>creathvps.exe
Warning: (): Invoke() failed: Exception occurred.
<b>Source</b>: Unavailable <b>Description</b>: Unavailable in C:\Program Files\
Phalanger v2.0\Samples\VMScripts\creathvps.php at expression on line 7, column 3
.
Strict: Creating default object from empty value in C:\Program Files\Phalanger v
2.0\Samples\VMScripts\creathvps.php at expression on line 13, column 3.
techscorpio@codeplex
 
Posts: 11
Joined: January 7th, 2012, 8:57 pm

RE: how to use a COM object for Microsoft Virtual server

Postby Lada Prosek » September 8th, 2006, 1:50 pm

Hi, I tried this with my Virtual Server installation and I am getting the same error when the code is interpreted by PHP. Therefore it does not like a problem with Phalanger. You should check the Virtual Server object model documentation.

The only difference is that PHP 5 throws an exception instead of just crashing with the "Invoke() failed: Exception occurred." error (but the message of the exception is just as helpful as the error: "Fatal error: Uncaught exception 'comexception' with message 'Source: Unknown Description: Unknown'"). Currently we can host only the PHP 4 version of phpcom estension so you'll have to get along without exception support in COM.
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

RE: how to use a COM object for Microsoft Virtual server

Postby Lada Prosek » September 8th, 2006, 1:52 pm

.. Unless you would be willing to reimplement this extension in managed :)
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

RE: how to use a COM object for Microsoft Virtual server

Postby techscorpio@codeplex » September 11th, 2006, 6:06 am

Hi,
Thanks for a quick response
techscorpio@codeplex
 
Posts: 11
Joined: January 7th, 2012, 8:57 pm

RE: how to use a COM object for Microsoft Virtual server

Postby techscorpio@codeplex » September 12th, 2006, 7:11 am

Hi... Me again and again...

Well, Since i am unable to create a object of Virtual server in PHP. Now i want to do same using phalanger i have a C# program, is it possible to convert?

C# code is like this:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VirtualServer.Interop;

namespace ShowVSVersion
{
/// <summary>
/// Define RPCCAUTHNLEVEL constants
/// </summary>
public enum RpcAuthnLevel
{
Default = 0,
None,
Connect,
Call,
Pkt,
PktIntegrity,
PktPrivacy
}

/// <summary>
/// Define RPCCIMPLEVEL constants
/// </summary>
public enum RpcImpLevel
{
Default = 0,
Anonymous,
Identify,
Impersonate,
Delegate
}

/// <summary>
/// Define EOAC_ constants
/// </summary>
public enum EoAuthnCap
{
None = 0x00,
MutualAuth = 0x01,
StaticCloaking = 0x20,
DynamicCloaking = 0x40,
AnyAuthority = 0x80,
MakeFullSIC = 0x100,
Default = 0x800,
SecureRefs = 0x02,
AccessControl = 0x04,
AppID = 0x08,
Dynamic = 0x10,
RequireFullSIC = 0x200,
AutoImpersonate = 0x400,
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 (CmdArgs0.Length > 1 &&
(CmdArgs0.Substring(0, 1) == "-" || CmdArgs0.Substring(0, 2) == "/?"))
{
String strHelp = "USAGE: ShowVSVersion {remoteservername}\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(CmdArgs0);
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);
}
}

}

techscorpio@codeplex
 
Posts: 11
Joined: January 7th, 2012, 8:57 pm

RE: how to use a COM object for Microsoft Virtual server

Postby Lada Prosek » September 12th, 2006, 2:10 pm

P/Invokes (the DllImport attribute) are not supported by Phalanger. Why don't you write a small class library in C# containing this low-level stuff and then just consume it from Phalanger?
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

Next

Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 14 guests

User Control Panel

Login

Who is online

In total there are 14 users online :: 0 registered, 0 hidden and 14 guests (based on users active over the past 5 minutes)
Most users ever online was 297 on March 29th, 2024, 7:06 am

Users browsing this forum: No registered users and 14 guests