PHP calls the C # developed dll class library method,

Source: Internet
Author: User
Tags net command

PHP calls the C # developed dll class library method,

Sometimes, we need to use the dll class libraries written in other languages in php, such as the dll written in C #, which is called using the PHP new COM method, before calling the API, you must register the dll library and put the Assembly into the global cache.

1. Create a C # Class Library, Named HelloWorld
2. Open Project PropertiesClick "Application" (the first tab) on the left, and then click the Assembly Information button. In the displayed Dialog, Make assembly COM-visible must be checked at the bottom! Otherwise, the dll cannot be accessed as COM. (You can also write [ComVisible (true)] in the class declaration in the Code. The effect is the same and you need to add the using System. runtime. interopServices; reference)

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 strongly named signature file.
Open project properties, click Signing on the left to check and Sign the assembly. In Choose a strong name key file: Select <Browse> select the created HelloWorld. snk file.

4. Create a class library and compile it into a dll
Copy codeThe 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 pathAnd then use vs.net's "Vsitual Studio. Net tool" --> Vistual Studio. Net Command Prompt
Enter the dll folder and enter:
Copy codeThe Code is as follows: regasm HelloWorld. dll <press enter>

At this time, the. net assembly of this. dll will become a standard Com component, but it cannot be used yet. It must be changed to a global Com component.
Add the assembly to the Global Assembly Cache
Enter the prompt window and enter:
Copy codeThe Code is as follows: gacutil/I HelloWorld. dll <press enter>
 
At this time, your dll will be copied to the Global Assembly Cache. That is to say, you can use this dll component no matter which hard disk on this computer.
If you do not perform a strong name signature, this step will prompt loading failure.

PHP testing:
Copy codeThe Code is as follows: <? Php
$ R = new Com ("HelloWorld. Hello ");
$ S = $ r-> Write ();
Echo $ s;
?>
Command:
Copy codeThe Code is as follows: CD [/D] [drive:] [path] # enter the specified path
CD [..] # returns the parent directory


Php calls c program

I am here to give you a few functions for how to execute system commands in PHP, hoping to help you. I use Linux as an example. It is the same function as DOS. However, you must have the execution permission. Php

How does php call C language programs?

Php can be completed in two or three sentences. You are sure that your C language will publish webservices and generate standard soap files, if you think php is written in C language and can be easily called to each other, you will be wrong. The idea is good, and you want to combine the advantages of each other. In addition, php cannot meet your computing requirements, are you sure?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.