Home > Article > Backend Development > PHP calls the dll class library method developed by C#
This article mainly introduces the method of PHP calling dll class library developed by C#, including a complete and detailed DLL production step and PHP calling method. Friends in need can refer to it
Sometimes, we need to use PHP To use dll class libraries written in other languages, such as dlls written in C#, the method is to use the PHP new COM method to call. Before calling, the dll library must be registered and the assembly put into the global cache.
1. Create a C# Class Library and name it: HelloWorld
2. Open the properties of the project, click "Application" on the left (the first tab), and then click the Assembly Information button. In the pop-up Dialog, Must be ticked at the bottom: Make assembly COM-visible! Otherwise, this dll will not be accessible in COM mode. (You can also write [ComVisible(true)] in the class declaration in the code, the effect is the same, you need to add using System. Runtime.InteropServices; Reference)
3. Create a strong named signature file and use
Use vs.net's "Vsitual Studio .Net Tool" -->Vistual Studio .Net command prompt, enter sn -k d: HelloWorld.snk Press Enter to create a strong name signature file
Open the properties of the project, click Signing on the left and check Sign the assembly. Select
4. Create a class library and compile it into a dll
Copy the code The code is as follows:
namespace HelloWorld
{
//[ComVisible(true)] //or check "Assembly COM-Visible" at Application -Assembly_Information dialog ;
Public class Hello
{
public string Write()
Return "Hello World"; }
}
}
The code is as follows:regasm HelloWorld.dll
At this time, The .net assembly of this .dll becomes a standard Com component, but it cannot be used yet. It must be turned into a global Com component.
Add the assembly to the global assembly cache Enter the prompt window and enter :
The code is as follows:gacutil /I HelloWorld.dll
PHP test:
The code is as follows:$r=new Com("HelloWorld.Hello");
$s=$r->Write();
echo $s;
?>
Command prompt:
The code is as follows:CD [/D] [drive:][path] #Enter the specified path
CD [..] #Return Parent directory
Original text reproduced from: http://www.jb51.net/article/52846.htm
The above introduces the method of PHP calling the dll class library developed by C#, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.