How to use .Net components in PHP
A component is a unit of reusable code.
Components reduce our application development time and they assemble our
application source files into one executable unit.
Assemblies are building blocks of a .net application. Components are packaged in assemblies. They can exist in the form of DLL�s or exe�s. The basic difference between a .dll file and .exe file is that a .dll file cannot execute independently. They can only include in our application and reuse the functionalities provided in it. An exe file can execute independently. Another difference is that an exe file have an entry point. That is they can have a main method. A dll file does not have an entry point. They can contain classes, interfaces etc.
Assemblies are 2 types. Shared and Private. In a .net application, any component placed in the application�s bin directory is automatically available to that particular application. They can be called as private assemblies. But if you want to use that component in php successfully you must register it as shared assembly. A private assembly is only available to that particular application which places it. A shared assembly can shared between applications. They must be stored in the GAC(Global Assembly Cache). You can find GAC under C:\WINNT\Assembly\ if you have the .NET framework installed. Unlike COM, .NET does not use the Windows Registry to find components; it uses the GAC.
Here is an example of a vb.net program which calculates square of any number which is passing from a php script.
Here a namespace myExample is declared which contains a FindSquare() method which finds the square of a number which is passed to it.
Save this code as myExample.vb.
Then compile this module using following command.
Vbc /t:library myExample.vb
You can use VisualStudio.Net command prompt for executing commands like this as command line.
The /t:library tells the vb compiler in the .net framework to compile the file myExample.vb as a DLL or assembly. So now a dll file is created named myExample.dll by the result of above command.
It cannot be successful if you will just copy and paste this assembly or DLL file to GAC.
An assembly can add to the GAC either using tools like Gacutil.exe or Regasm.exe.
Every component in the GAC must be signed by a strong name using the public key cryptography.
So you need to create a key pair for your DLL file using the sn.exe tool and store it in an snk file before you add the DLL file to GAC. A key pair is a combination of a public and private key used for implementing security. This is for eliminating the problem of DLL hell.
Sn- k myAssemblykey.snk
Here we have created an snk file named myAssemblykey. After creating the strong name, we have to sign the component with the newly created strong name. This can be done by the following.
myExample.dll/Assemblykeyfile:myAssemblykey.snk
Then you use the Gacutil.exe tool to add your component to GAC.
Gacutil /I myExample.dll
Create a PHP File
The next step is to create a php file with the following code:
Here we are passing the integer value �2� to function FindSquare() inside the assembly myExample.dll which we created. Finally at the last line of php code we are printing the returned value from our DLL file.
Hope you all have enjoyed this article. Post your suggestions.
Thank you.
Assemblies are building blocks of a .net application. Components are packaged in assemblies. They can exist in the form of DLL�s or exe�s. The basic difference between a .dll file and .exe file is that a .dll file cannot execute independently. They can only include in our application and reuse the functionalities provided in it. An exe file can execute independently. Another difference is that an exe file have an entry point. That is they can have a main method. A dll file does not have an entry point. They can contain classes, interfaces etc.
Assemblies are 2 types. Shared and Private. In a .net application, any component placed in the application�s bin directory is automatically available to that particular application. They can be called as private assemblies. But if you want to use that component in php successfully you must register it as shared assembly. A private assembly is only available to that particular application which places it. A shared assembly can shared between applications. They must be stored in the GAC(Global Assembly Cache). You can find GAC under C:\WINNT\Assembly\ if you have the .NET framework installed. Unlike COM, .NET does not use the Windows Registry to find components; it uses the GAC.
Here is an example of a vb.net program which calculates square of any number which is passing from a php script.
| Code: |
| imports system
Namespace myExample Public Class math Public Function FindSquare(ByVal no As integer) dim result as integer result = no * no Return result End Function End Class End Namespace |
Here a namespace myExample is declared which contains a FindSquare() method which finds the square of a number which is passed to it.
Save this code as myExample.vb.
Then compile this module using following command.
Vbc /t:library myExample.vb
You can use VisualStudio.Net command prompt for executing commands like this as command line.
The /t:library tells the vb compiler in the .net framework to compile the file myExample.vb as a DLL or assembly. So now a dll file is created named myExample.dll by the result of above command.
It cannot be successful if you will just copy and paste this assembly or DLL file to GAC.
An assembly can add to the GAC either using tools like Gacutil.exe or Regasm.exe.
Every component in the GAC must be signed by a strong name using the public key cryptography.
So you need to create a key pair for your DLL file using the sn.exe tool and store it in an snk file before you add the DLL file to GAC. A key pair is a combination of a public and private key used for implementing security. This is for eliminating the problem of DLL hell.
Sn- k myAssemblykey.snk
Here we have created an snk file named myAssemblykey. After creating the strong name, we have to sign the component with the newly created strong name. This can be done by the following.
myExample.dll/Assemblykeyfile:myAssemblykey.snk
Then you use the Gacutil.exe tool to add your component to GAC.
Gacutil /I myExample.dll
Create a PHP File
The next step is to create a php file with the following code:
| Code: |
| <?php
$test = new COM(�myExample.math�); $VAR = $test ->FindSquare(2); echo $VAR; ?> |
Here we are passing the integer value �2� to function FindSquare() inside the assembly myExample.dll which we created. Finally at the last line of php code we are printing the returned value from our DLL file.
Hope you all have enjoyed this article. Post your suggestions.
Thank you.
Commentaires
Enregistrer un commentaire