Home  >  Article  >  Backend Development  >  How to solve the error problem of php com component

How to solve the error problem of php com component

藏色散人
藏色散人Original
2021-03-10 10:33:323259browse

Solution to the php com component error: 1. Modify the configuration "com.allow_dcom" and "php_com_dotnet.dll" in php.ini; 2. Use vs2013 to write the COM component.

How to solve the error problem of php com component

The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

Reasons why php fails to load COM components and their solutions

Personally, the most convenient way to call interfaces of other programming languages ​​​​in php is Use COM components. Recently, I just started to come into contact with PHP. When I was studying calling COM components written in C, I encountered the error "'Failed to create COM object". It took a lot of effort to finally solve it.

Higher versions of php need to manually enable COM support. Basically, modify the configuration in php.ini to com.allow_dcom = true and extension=php_com_dotnet.dll. If this is the error, it can be easily solved. In fact, I think this kind of configuration error is rarely made by people. Many people say that they can test whether the COM component support is normal by loading and testing wps or word components. However, due to permission issues, this testing method is not very practical. Easy to use. Later, I found $db = new COM("ADODB.Connection",NULL, 65001); in the http://php.net/manual/zh/class.com.php document description; after testing, it loaded normally, indicating that There must be something wrong with the COM component I wrote, and it turned out to be the case.

There were many examples of PHP loading COM components written in C on the Internet before. It may be because the PHP version is too low, and that way of creating components is no longer suitable. If you are using a version before php5.2, it may be correct. I'm using php5.5 and was misled.

My PHP version is 5.5.12, and I use vs2013 to write COM components. The following is the process:

1. Find vs2013 in the launch bar, right-click to open it with administrator rights, so use the administrator identity It is turned on because it will be convenient when registering the COM component later. Create a new ATL project in c,

How to solve the error problem of php com component

2, click Finish, right-click on the project to add a class, and select ATL COM 10.

How to solve the error problem of php com component

#3. Right-click on the project to add a class. The following is the most critical point. The ProgID in the lower right corner must be added manually. The format is "Project name. Abbreviation". The project name is the name of the generated dll. It will also be written like this when loading it in PHP later. The second item is The abbreviation, not the class name. If this place is written incorrectly, although the component can be registered successfully, it will not be recognized in PHP.

How to solve the error problem of php com component

#4. Add a method on Itest. I will not write this in detail. I added an add as follows [Recommended learning: "PHP Video tutorial》】

STDMETHODIMP Ctest::add(LONG a, LONG b, LONG* c)
 {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO:  在此添加实现代码
*c = a + b;
return S_OK;
 }

5, then compile and automatically register as a component, and restart the apache service. The following call is made in the php script:

<?php
$conn = new COM("ATLProject3.test") or die("Cannot start ADO"); $com = 2; $conn->add(10, 2, $com); echo $com;  ?>

Note that the format of "component name.abbreviation" is correct. In the online example, it is "component name.class name". It may be a version problem. Anyway, my call failed.

6. Enter your PHP project directory into the browser, and it will show that the COM component is loaded normally. The COM component is successfully used to make the output of 10 plus 2 as 12.

How to solve the error problem of php com component

The above is the detailed content of How to solve the error problem of php com component. For more information, please follow other related articles on the PHP Chinese website!

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