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

PHP calls the dll class library method developed by C#_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:44701browse

PHP calls the dll class library method developed by C#

This article mainly introduces the method of PHP calling the dll class library developed by C#, including a complete and detailed DLL production step and PHP calling method, friends who need it can refer to it

Sometimes, we need to use dll libraries written in other languages ​​in php, 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 program Set into the global cache.

 1. Create a C# Class Library named: 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, you must check the bottom box: 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调用C#开发的dll类库方法  帮客之家

 3. Create a strongly named signature file and use

Use vs.net’s “Vsitual Studio .Net Tool” -->Vistual Studio .Net command prompt, enter sn -k d:HelloWorld.snk and press Enter to create a strong named signature file

Open the project properties, click Signing on the left and check Sign the assembly. Select at Choose a strong name key file: Select the HelloWorld.snk file you just created

 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 Tool” -->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, this dll component can be used no matter which hard drive is on this computer.

If strong naming signature is not performed, this step will prompt loading failure

PHP test:

Copy the code. The code is as follows:

 $r=new Com("HelloWorld.Hello");

 $s=$r->Write();

echo $s;

 ?>

Command prompt:

Copy the code. The code is as follows:

 CD [/D] [drive:][path] #Enter the specified path

 CD [..] #Return to parent directory

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852822.htmlTechArticlePHP calls the dll class library method developed by C#. This article mainly introduces the method of PHP calling the dll class library developed by C#. , including a complete and detailed DLL production steps and PHP calling method, friends who need it can...
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