PHP calls the COM component instance _ php instance compiled by VC

Source: Internet
Author: User
Recently, the project requires PHP to parse binary data. If it is more difficult to directly use PHP, You need to compile the COM component in VC to provide PHP calls. This article introduces how to compile the COM component in VC to provide PHP call to calculate the addition of two numbers. 1. Use VC to create COM components1. Create a project in vc-> ATL Project. Enter the name ComTest and ComTest as the name of the COM component.


2. Complete the wizard without any changes to the ATL Project Wizard. After the Wizard is complete, vc will automatically generate a series of programs.

3. Switch to the Class View, right-click the ComTest item, add a class, and select an ATL simple object,

4. Go to the ATL simple object wizard and enter the new class name MyClass to complete the wizard. (Note: You must enter the ProgID, and some vs will automatically complete the configuration)

5. Return to the Class View and right-click IMyClass to add a method. The Wizard for adding a method will be displayed.

6. in the add method wizard, we want to calculate the sum of two numbers. For example, parameters a and B are both [in] Long, and c are the return parameters of COM, which are [out, retval] Long * is a pointer. Complete the wizard.
7. Find the method we just added in MyClass. cpp, add (LONG a, LONG B, LONG * c), and modify it as follows:

The Code is as follows:

STDMETHODIMP CMyClass: add (LONG a, LONG B, LONG * c)
{
* C = a + B;
Return S_ OK;
}


8. After compilation, find ComTest. dll in the generated directory and register it with the system.

The Code is as follows:

Regsvr32 ComTest. dll

Ii. PHP calls COM components

The Code is as follows:


$ Com = new COM ("ComTest. MyClass") or die ("cannot call ComTest ");
Echo $ com-> add (1, 2 );


Under normal circumstances, it will be done here.

However, by default, PHP does not enable the function of calling the COM component. When PHP executes the above code, it will report the Fatal error: Class 'com' not found error. Modify the PHP configuration as follows:

The Code is as follows:


Extension = php_com_dotnet.dll


Conclusion
This function is used by PHP to call COM components. Although it is convenient to call and development is relatively simple, it is only available for Windows machines because of the use of COM component technology.



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.