Home  >  Article  >  Backend Development  >  PHP calls the dll class library method developed by C#

PHP calls the dll class library method developed by C#

WBOY
WBOYOriginal
2016-07-29 09:09:081194browse

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)

PHP calls the dll class library method developed by C#

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 at Choose a strong name key file: and select the HelloWorld.snk file you just created

PHP calls the dll class library method developed by C#

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"; }
}
}

5. Find the dll folder path, and then use vs.net’s “Vsitual Studio .Net Tools"-->Vistual Studio .Net Command Prompt
Enter the dll folder and enter:


Copy the code

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 :


Copy the code

The code is as follows:gacutil /I HelloWorld.dll

At this time, your dll will be copied to the global assembly cache. In other words, no matter where This dll component can be used on any hard disk of this computer.
If strong naming signature is not performed, this step will prompt a loading failure

PHP calls the dll class library method developed by C#PHP test:


Copy the code

The code is as follows:$r=new Com("HelloWorld.Hello");

$s=$r->Write();
echo $s;
?>

Command prompt:



Copy code

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn