Home arrow PHP arrow Page 2 - Using the .NET Assembly in PHP

Using the .NET Assembly in PHP

Using the power of COM interop, you can write code in VB.Net or C# and use it in PHP, VB6, or any other language that can access COM. In this article Jayesh shows us how.

Author Info:
By: Jayesh Jain
Rating: 4 stars4 stars4 stars4 stars4 stars / 46
October 15, 2002
  1. · Using the .NET Assembly in PHP
  2. · Creating an Assembly
  3. · Creating the PHP File
  4. · Conclusion

print this article

Using the .NET Assembly in PHP - Creating an Assembly
(Page 2 of 4 )

Open Visual Studio.Net and create a new class library project, which we will call "phpclass". Click on the OK button when you're done, as shown below:

Creating our class library

In the code window for class1 (class1.vb), type the following:

Namespace HealthRecord
Public Class patient
Private m_lmp As Date
Public Property lmp()
Return Format(m_lmp, "D")
End Get
Set(ByVal Value)
m_lmp = Value
End Set
End Property
Public ReadOnly Property edd()
'EDD is 280 days from LMP
Return Format(DateAdd(DateInterval.Day, 280, m_lmp), "D")
End Get
End Property
End Class
End Namespace

Let's assume that we're coding this app for a clinic. This VB.NET code will 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 called HealthRecord and a public class called patient, with two public properties: lmp and edd. Edd is a read-only property, which is calculated from lmp, which can be set and also read.

We have used the format function to format the date variable to a human readable format. We have also used the dateadd function to calculate the edd, which is 280 days from the lmp.

Creating a Strong Name Key File
A strong name is the full class name including the namespace, version, public key and digital signature. All assemblies installed on the system have to be uniquely identified and versioned. To make sure the assembly is not tampered with, 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 can then be used to create an assembly.

sn k mykey.snk

This utility can be found in the .NET Framework's Bin folder C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer).

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.

Here is the sample output you should be expecting:

Output from the strong name tool

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 the solution explorer and double click AssemblyInfo.vb to open the code window and add the following line:

<Assembly: AssemblyKeyFile("c:\tgol\phpclass\mykey.snk")>

Make sure you enter the full path to where you 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 adds information about the class to the system registry, so that COM clients can use the .NET class transparently.

regasm c:\tgol\phpclass\bin\phpclass.dll /tlb:phpclass.tlb

This utility can be found in C:\WINNT\Microsoft.NET\Framework\v1.0.3705 (on my computer).

Running regasm.exe

Adding the Assembly to Global Assembly Cache
In order to share the assembly with all applications, it needs to be installed in the GAC (Global Assembly Cache) with the following command from the command line:

gacutil /i c:\tgol\phpclass\bin\phpclass.dll

This utility can be found in C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin (on my computer).

Running gacutil.exe 

To view all of the assemblies installed in the GAC on your computer, open your windows explorer and look in the assembly folder under your windows folder, C:\WINNT\assembly (on my computer)
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials