What should I do if PHP cannot call the COM component?

藏色散人
Release: 2023-03-17 10:50:01
Original
2223 people have browsed it

Solution to the problem that php cannot call the com component: 1. Confirm the php version and required system tools; 2. Configure php.ini and enable "com.allow_dcom = true"; 3. Enable the com function of php or Just check whether "php_com_dotnet.dll" is loaded successfully.

What should I do if PHP cannot call the COM component?

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

What should I do if php cannot call the com component? PHP reference COM component pitfall record

Recently, a "odd job" appeared in the work items. It is necessary to add a function to the existing PHP project, and the project and the source code needed to implement the function There are also projects, which is a very easy thing. Although I rarely use PHP, this is not the most important "limiting factor". What gives me a headache is that the function library is implemented using the .NET framework. There are many adjustments to the data format, which is relatively complicated. There is no Functional logic manual, difficult to copy in PHP. Therefore, directly referencing .NET's dynamic library in php has become the key to completing the "task"

Many seniors on the Internet have recorded this problem, but they have failed to copy the gourd. This article is in "Liver Odd Job" "In the process, we encountered and filled in the records

Generate COM component dynamic library

COM component (COM component) is a software development technology developed by Microsoft. Its essence is some small binary executable programs, which can provide services to applications, operating systems and other components. If you want to reference a third-party dynamic library in PHP, you need to use the new COM("Component.class") method, where the Component must be a COM component

1. Build a Windows class library project

No matter which version of Framework, you should be able to find the class library project under the Windows tab

In this way, there will be a Properties\AssemblyInfo.cs file by default in the project, which records the required information for the project set. Information

2. The target framework must match

You can select the Framework when creating the project

If it is adjusted later, you can select the target in Project Properties>Application Make modifications in the framework

Note that the framework needs to be consistent when registering the dll later

3. Set the COM component to be visible

In the Properties\AssemblyInfo.cs file, Modify ComVisible as follows

[assembly: ComVisible(true)]
Copy after login

4. To set the signature and set the strong name key

Project Properties> In Signature, check "Sign the assembly" and select "Sign the assembly" below. Create a new key file in "Strong Name Key File"

and then generate it, find the corresponding dll file in the bin\Debug directory

5. If there is a dynamic library that cannot be embedded

If the project Framework version is lower, or

the referenced library lacks the features required for interoperability,

In short, the referenced library cannot be embedded with interoperability

Then , the dependent dll also needs to perform subsequent registration and other operations

Register the DLL to the server

Involves system-level operations, there are many pitfalls...

1. Confirm the php version

Personally tested version 5.3.22 is not available, version 5.4.5 is available, so try to use version 5.4 or above

Now the highest version of php is version 8.0, I don’t know if it is still the same Operation method

2. Confirm the required system tools

You need two tools, gacutil and regasm, and both need to match the Framework version

That is to say, the generated COM Which version of the framework is used for the component's dll? You also need to use the corresponding version of the tool here

Usually the windows system will definitely have a built-in regasm tool

Directory path, for example,

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe
Copy after login

If it is applied to a 64-bit system, go to the Framework64 directory, otherwise use the Framework directory and select the version number of the corresponding Framework framework, where you should be able to find the RegAsm.exe file

The gacutil tool is in Microsoft The provided windows sdk is included. If you have not installed it, you can click to download it from the official website.

After the default installation, the tool path for versions below Frameworkd 4.0.

C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\gacutil.exe
Copy after login

If you need version 4.0, the path Yes,

c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\gacutil.exe
Copy after login

3. Configure php.ini

Enable com.allow_dcom = true

Make sure there is a php_com_dotnet.dll file in your extension_dir directory

Enable or add extension=php_com_dotnet.dll

4. Caching and registering DLL

Take the 64-bit system, version 4.0 my.dll as an example

Place the first dll generated in, copy to the c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\ directory

to the directory c:\Program Files\Microsoft SDKs\Windows\v7 Execute gacutil /i my.dll

under .1\Bin\NETFX 4.0 Tools\ to the directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ and execute regasm my.dll

5. Unregister the DLL

Take the 64-bit system, version 4.0 my.dll as an example

Go to the directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ and execute regasm my.dll /unregister

Go to the directory c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\ and execute gacutil /u my

PHP calls the COM component

After the above content is configured correctly, it can be called in the PHP file. The method is as follows.

$comClass = new COM('namespace.className');
$comClass -> methodName();
Copy after login

prompts that there is no COM method, so you need to consider whether it has been enabled. php's com function, or whether php_com_dotnet.dll is loaded successfully

It prompts that there is no method to call the dll, or the dll is not registered, you need to consider whether the dll file has been registered successfully

Recommended study: "PHP Video Tutorial"

The above is the detailed content of What should I do if PHP cannot call the COM component?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template