how to use a COM object for Microsoft Virtual server

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

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

Postby techscorpio@codeplex » September 13th, 2006, 6:01 am

Hi, How can i use a class library of C# Phalanger? can u give me some example?

Thanks
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 13th, 2006, 3:40 pm

Sure. Let' say I want to play .wav files in my PHP code. Firstly, I'll build this little C# class library:

using System;using System.Runtime.InteropServices; namespace ClassLibrary1{	public class Class1	{		[DllImport("winmm.dll", SetLastError = true)]		private static extern bool PlaySound(string pszSound,		   UIntPtr hmod, uint fdwSound); 		public static void PlayWave(string fileName)		{			PlaySound(fileName, UIntPtr.Zero, 0x00020000);		}	}}

Then I'll will compile this little PHP script:

<?  ClassLibrary1:::Class1::PlayWave("C:\\Windows\\Media\\tada.wav");?>

Using this command:

phpc /lang:clr /r:ClassLibrary1.dll myscript.php

All public types (Class1 in this case) are "imported" to PHP and can be invoked.
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 14th, 2006, 11:59 am

Hi, i am getting the following Error when i tried u r sample example

C:\PROGRA~1\PHALAN~1.0Samples\VMSCRI~1>phpc
/lang:clr /r:ClassLibrary1.dll Phtest.php
Phalanger - the PHP Language Compiler - version 2.0
for Microsoft (R) .NET Framework version 2.0
Configuration error:
Library assembly 'ClassLibrary1.dll' could not be loaded.Could not load file or asembly 'ClassLibrary1.dll' or one of its dependencies.
The system cannot find the file specified.

what i did was, i created ClassLibrary1.dll using C#Express. Then i have done following Two things using gacutil & regasm
1) C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>gacutil.exe -i "C:\Docum
ents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\virtual
server\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll"
Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

Assembly successfully added to the cache


2) C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>Regasm "C:\Documents and
Settings\Administrator\My Documents\Visual Studio 2005\Projects\virtual server\
ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll" /tlb:ClassLibrary1.tlb
Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.

Assembly exported to 'C:\Documents and Settings\Administrator\My Documents\Visua
l Studio 2005\Projects\virtual server\ClassLibrary1\ClassLibrary1\bin\Debug\Clas
sLibrary1.tlb', and the type library was registered successfully

But still not working!!!

Thanks
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 14th, 2006, 2:36 pm

I did not put the class library assembly to GAC. I just copied it to the phpc's output directory. If you add it to GAC, you have to specified full name, something like:
phpc "/r:ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4af37afe3cde05fb"
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 15th, 2006, 10:58 am

Hi,

Thnaks your Example works fine but i got run time ERROR when i tried to access my Virtual server class library.

i created a class library MSVirtual.dll like thih
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VirtualServer.Interop;

namespace MSVirtual
{
/// <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 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;
}
}
}
PHP Code:-
<?php
f1();
function f1()
{
echo("Microsoft Virtual Server");
$vm=MSVirtual:::InitVS::GetVMVirtualServerClass();
echo("Done");
}
?>
when i compiled using command
C:\PROGRA~1\PHALAN~1.\Samples\MSVIRT~1>phpc /lang:clr /r:MSVirtual.dll /r:Inter
op.Microsoft.VirtualServer.Interop.dll MSV1.php
It will through Run time Error
C:\PROGRA~1\PHALAN~1.0\Samples\MSVIRT~1\bin>MSV1.exe

Unhandled Exception: PHP.Core.PhpNetInternalException: Guarded call ---> System.
IO.FileNotFoundException: Could not load file or assembly 'MSVirtual, Version=1.
0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The sys
tem cannot find the file specified.
File name: 'MSVirtual, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at <MSV1.php>.<Script>.f1(ScriptContext context)
at <MSV1.php>.<Script>.<Main>(ScriptContext context, Dictionary`2 variables,
DObject self, DTypeDesc includer, Boolean isMain) in C:\Program Files\Phalanger
v2.0\Samples\MSVirtual\MSV1.php:line 2
at PHP.Core.ScriptContext.GuardedMain(Object mainRoutine)
at PHP.Core.ScriptContext.GuardedCall(Action routine, Object data, Boolean al
lowUserExceptions)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\M
icrosoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure lo
gging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus
ion!EnableLog].

--- End of inner exception stack trace ---
at PHP.Core.ScriptContext.GuardedCall(Action routine, Object data, Boolean al
lowUserExceptions)
at PHP.Core.ScriptContext.RunApplication(Delegate mainRoutine, String relativ
eSourcePath, String sourceRoot)
at <Global>.Run(String[] )


Thanks
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 15th, 2006, 12:32 pm

Hi,

Thnaks your Example works fine but i got run time ERROR when i tried to access my Virtual server class library.

i created a class library MSVirtual.dll like this
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VirtualServer.Interop;

namespace MSVirtual
{
/// <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 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;
}
}
}
PHP Code:-
<?php
f1();
function f1()
{
echo("Microsoft Virtual Server");
$vm=MSVirtual:::InitVS::GetVMVirtualServerClass();
echo("Done");
}
?>
when i compiled using command
C:\PROGRA~1\PHALAN~1.\Samples\MSVIRT~1>phpc /lang:clr /r:MSVirtual.dll /r:Inter
op.Microsoft.VirtualServer.Interop.dll MSV1.php
It will through Run time Error
C:\PROGRA~1\PHALAN~1.0\Samples\MSVIRT~1\bin>MSV1.exe

Unhandled Exception: PHP.Core.PhpNetInternalException: Guarded call ---> System.
IO.FileNotFoundException: Could not load file or assembly 'MSVirtual, Version=1.
0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The sys
tem cannot find the file specified.
File name: 'MSVirtual, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at <MSV1.php>.<Script>.f1(ScriptContext context)
at <MSV1.php>.<Script>.<Main>(ScriptContext context, Dictionary`2 variables,
DObject self, DTypeDesc includer, Boolean isMain) in C:\Program Files\Phalanger
v2.0\Samples\MSVirtual\MSV1.php:line 2
at PHP.Core.ScriptContext.GuardedMain(Object mainRoutine)
at PHP.Core.ScriptContext.GuardedCall(Action routine, Object data, Boolean al
lowUserExceptions)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\M
icrosoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure lo
gging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus
ion!EnableLog].

--- End of inner exception stack trace ---
at PHP.Core.ScriptContext.GuardedCall(Action routine, Object data, Boolean al
lowUserExceptions)
at PHP.Core.ScriptContext.RunApplication(Delegate mainRoutine, String relativ
eSourcePath, String sourceRoot)
at <Global>.Run(String[] )


Thanks
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 15th, 2006, 2:18 pm

.NET assembly binding must be able to find MSVirtual.dll somwhere. Copy it to the the directory where your .exe is located (i.e. bin).
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 18th, 2006, 11:40 am

Hi, I am getting some Security Error i think it is because of Threading i need to assign threading asMTAThread before calling the class can u help me please...

C:\PROGRA~1\PHALAN~1.0\Samples\MSVIRT~1\bin>MSV1.exe
Microsoft Virtual Server
Unhandled Exception: PHP.Core.PhpNetInternalException: Guarded call ---> System.
Runtime.InteropServices.COMException (0x80010119): Security must be initialized
before any interfaces are marshalled or unmarshalled. It cannot be changed once
initialized. (Exception from HRESULT: 0x80010119)
at MSVirtual.InitVS.CoInitializeSecurity(IntPtr pVoid, Int32 cAuthSvc, IntPtr
asAuthSvc, IntPtr pReserved1, UInt32 dwAuthnLevel, UInt32 dwImpLevel, IntPtr pA
uthList, UInt32 dwCapabilities, IntPtr pReserved3)
at MSVirtual.InitVS..ctor()
at <*>..ctor(Object , PhpStack )
at PHP.Core.RoutineDelegate.Invoke(Object instance, PhpStack stack)
at PHP.Core.Reflection.DRoutineDesc.Invoke(DObject instance, PhpStack stack)
at PHP.Core.Operators.NewClr(DTypeDesc clrType, ScriptContext context)
at <MSV1.php>.<Script>.<Main>(ScriptContext context, Dictionary`2 variables,
DObject self, DTypeDesc includer, Boolean isMain) in C:\Program Files\Phalanger
v2.0\Samples\MSVirtual\MSV1.php:line 5

Thanks
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 18th, 2006, 1:21 pm

This discussion has been copied to Work Item 3376. You may wish to continue further discussion there.
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 18th, 2006, 1:31 pm

Currently the startup thread is set to STA apartment state. You can however create a new thread with MTA apartment state and perform all your COM interop there.

<?	$thread = new System:::Threading:::Thread("Start");	$thread->SetApartmentState(System:::Threading:::ApartmentState::MTA);	$thread->Start();	$thread->Join(); 	function Start()	{		echo "Hello from MTA thread...";	}?>
Lada Prosek
 
Posts: 47
Joined: January 7th, 2012, 8:52 pm

PreviousNext

Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 20 guests

User Control Panel

Login

Who is online

In total there are 20 users online :: 0 registered, 0 hidden and 20 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 20 guests