Introduction

The .NET Framework is a new computing platform that simplifies application development in the highly distributed environment of the Internet. The .NET Framework is designed to provide a consistent object-oriented programming environment, a code-execution environment that minimizes software deployment and versioning conflicts, which guarantees safe execution of code and to build all communication on industry standards to ensure that code based on the .NET Framework can integrate with any other code.
An assembly is the primary building block of a .NET Framework application. It is a collection of functionality that is built, versioned, and deployed as a single implementation unit containing one or more files. Each assembly contains an assembly manifest.
The Component Object Model (COM) allows an object to expose its functionality to other components and to host applications. Interoperability with COM, or COM interop allows you to create a COM wrapper around the .NET components, which makes Windows look at them as COM objects and thus makes it available to all the calling COM applications.
PHP has built in functionality which uses COM objects, using the COM interop feature of .NET we shall create a wrapper around the .NET assembly and use it in PHP (in fact you could use the same steps to use .NET assembly for VB6 or any other COM applications)
This article assumes that you have fair knowledge of .NET framework, PHP and COM also you have Visual Studio .NET, PHP and IIS installed and working properly.
Creating a Assembly
Open Visual Studio.Net and create a new class library project, which we shall call "phpclas". Click OK.
New Project window
In the code window for class1 (class1.vb), type the following:
Namespace HealthRecord
    Public Class patient
        Private m_lmp As Date
        Public Property lmp()
            Get
                Return Format(m_lmp, "D")
            End Get
            Set(ByVal Value)
                m_lmp = Value
            End Set
        End Property
        Public ReadOnly Property edd()
            Get
              'EDD is 280 days from LMP
              Return Format(DateAdd(DateInterval.Day, 280, m_lmp), "D")
            End Get
        End Property
    End Class
End Namespace
This VB.NET code shall calculate the EDD (Estimated Delivery Date) from the specified LMP (Last Menstrual Period) date for any patient, to implement this we have created a namespace HealthRecord and a public class patient with two public property lmp and edd, edd is a readonly property, which is calculated from lmp which could be set and also read.
We have used format function to format the date variable to human readable format; also we have used dateadd function to calculate the edd, which is 280 days from the lmp.

Creating Strong Name Key file

Strong name is the full class name with namespace, version, public key and digital signature. All the assembly installed on the system has to be uniquely identified and versioned, also to make sure the assembly is not tampered it needs to be signed with a common key ( public key) to decrypt.
The Strong Name command-line tool (sn.exe) can be used to generate a new public-private key pair and to write that pair to a file which could be then used to create the assembly.
sn -k mykey.snk 
This utility could be found in the .NET Framework's Bin folder C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer)
[Note] Alternatively, you could run a .NET Command Window by selecting Start -> Programs -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools >- Visual Studio .NET Command Prompt, which sets all of the required paths for you. [End Note]
Here is the sample output you should be expecting.
sample output window
Copy this file mykey.snk to your project folder c:\tgol\phpclass (on my computer).

Adding the Strong Key to Project

We need to add the key we just created to our project, open Solution Explorer and double click AssemblyInfo.vb to open the code window and add the following
<Assembly: AssemblyKeyFile("c:\tgol\phpclass\mykey.snk")>
Please make sure you enter the full path where you have saved the key file.
Now Build the Application to create phpclass.dll (this file shall be created in the bin folder under the project folder) c:\tgol\phpclass\bin\phpclass.dll (on my computer)

Registering the Assembly

To register a .NET class with COM, you must run a command-line tool called the Assembly Registration Tool (regasm.exe). regasm.exe creates a Type Library (TBL File) and add information about the class to the system registry so COM clients can use the .NET class transparently
regasm c:\tgol\phpclass\bin\phpclass.dll /tlb:phpclass.tlb
This utility could be found in C:\WINNT\Microsoft.NET\Framework\v1.0.3705 (on my computer)
utility window

Adding Assembly to Global Assembly Cache

In order to share the assembly with all the applications it needs to be installed in the GAC (Global Assembly Cache)
gacutil /i c:\tgol\phpclass\bin\phpclass.dll
This utility could be found in C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer)
utility window example
[Note] To view all the assembly installed in the GAC on your computer, open your windows explorer and look in assembly folder under windows folder C:\WINNT\assembly ( on my computer) [End Note]

Coding the PHP File

Create a text file with the following content and save the file as phpnet.php in c:\inetpub\wwwroot folder (or your webserver http root folder)

<?php
    $MyObj 
= new COM ("phpclass.HealthRecord.patient");
    
$MyObj->lmp "05/08/2002";
    echo 
"LMP : $MyObj->lmp";
     echo 
"<br />EDD : $MyObj->edd";
?>

Examining the Code

First we shall create a COM object for the object we just installed and registered, as COM interop makes the .NET assembly look like COM objects we can use the same syntax as we use to create COM objects in PHP.

<?php
$MyObj 
= new COM ("phpclass.HealthRecord.patient");
?>
We shall now set the lmp property to a valid date, which would be used to calculate edd.

<?php
$MyObj
->lmp "05/08/2002";
?>
We shall now output the LMP property in human readable date format and shall also output the calculated EDD property to the browser.

<?php
echo "LMP : $MyObj->lmp";
echo 
"<br />EDD : $MyObj->edd";
?>

Execute the Code

Open your favorite browser and type http://localhost/phpnet.php to see the PHP file in action
PHP file in Action
Using COM Interop in VB6
You could use the same steps as shown above to create a COM wrapper around the .NET assembly and use it in VB6, here is the code for your reference.
 Set MyObj = CreateObject("phpclass.HealthRecord.patient")
 MyObj.lmp = "05/08/2002"
 MsgBox "LMP : " & MyObj.lmp
 MsgBox "EDD : " & MyObj.edd
Un-registering the Type Library
To un-register the type library, run regasm with /unregister switch and the assembly path.
regasm /unregister c:/tgol/phpclass/bin/phpclass.dll
utility window example

Removing the Assembly from Global Assembly Cache

To remove the assembly from the GAC, run gacutil with /u switch and the assembly name.
[Note] Don't specify the path to the assembly and also do not add .dll extension to the assembly name [End Note]
gacutil /u phpclass
utility Php Class

Conclusion

This article shows you how simple it is to use Interoperability with COM, or COM interop, in PHP or VB6, which enables you to use .NET assembly in your traditional COM based applications.
COM objects have been fundamental to Windows programming for many years, but .NET Platform offer many advantages over and above it, .NET Platform applications will eventually replace those developed with COM.

 
--Jayesh is an applications consultant for a health company in Auckland, New Zealand. He has several years of n-Tier development experience and is currently working with Visual Basic.NET to develop interactive client solutions.