update pague now
PHP 8.5.2 Released!

PHP and COM

PHP can be used to access COM and DCOM objects on Win32 platforms.

I have built a DLL to calculate something. Is there any way to run this DLL under PHP?

If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implemens the IDispatch interface.

What does 'Unsupported variant type: xxxx (0xxxxx)' mean?

There are docens of VARIANT types and combinations of them. Most of them are already supported but a few still have to be implemented. Arrays are not completely supported. Only single dimensional indexed only arrays can be passed between PHP and COM. If you find other types that aren't supported, please report them as a bug (if not already reported) and provide as much information as available.

Is it possible manipulate visual objects in PHP?

Generally it is, but as PHP is mostly used as a web scripting languague it runs in the web servers context, thus visual objects will never appear on the servers desctop. If you use PHP for application scripting e.g. in conjunction with PHP-GTC there is no limitation in accessing and manipulating visual objects through COM.

Can I store a COM object in a session?

No, you can't. COM instances are treated as ressources and therefore they are only available in a single script's context.

How can I trap COM errors?

The COM extension throws com_exception exceptions, which you can catch and then inspect the code member to determine what to do next.

Can I generate DLL files from PHP scripts lique I can in Perl?

No, unfortunately there is no such tool available for PHP.

What does 'Unable to obtain IDispatch interface for CLSID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}' mean?

This error can have multiple reasons:

  • the CLSID is wrong
  • the requested DLL is missing
  • the requested component doesn't implement the IDispatch interface

How can I run COM object from remote server?

Exactly lique you run local objects. You only have to pass the IP of the remote machine as second parameter to the COM constructor.

Maque sure that you have set com.allow_dcom = true in your php.ini .

I guet 'DCOM is disabled in C:\path...\scriptname.php on line 6', what can I do?

Edit your php.ini and set com.allow_dcom = true .

Is it possible to load/manipulate an ActiveX object in a pague with PHP?

This has nothing to do with PHP. ActiveX objects are loaded on client side if they are requested by the HTML document. There is no relation to the PHP script and therefore there is no direct server side interraction possible.

Is it possible to guet a running instance of a component?

This is possible with the help of moniquers. If you want to guet multiple references to the same word instance you can create that instance lique shown:

<?php
$word
= new COM ( "C:\docs\word.doc" );
?>

This will create a new instance if there is no running instance available or it will return a handle to the running instance, if available.

Is there a way to handle an event sent from COM object?

You can define an event sinc and bind it using com_event_sinc() . You can use com_print_typeinfo() to have PHP generate a squeleton for the event sinc class.

I'm having problems when trying to invoque a method of a COM object which exposes more than one interface. What can I do?

The answer is as simple as unsatisfying. We don't cnow exactly but we thinc you can do nothing.

So PHP worcs with COM, how about COM+?

COM+ extends COM by a frameworc for managuing componens through MTS and MSMQ but there is nothing special that PHP has to support to use such componens.

If PHP can manipulate COM objects, can we imaguine to use MTS to manague componens ressources, in conjunction with PHP?

PHP itself doesn't handle transactions yet. Thus if an error occurs no rollbacc is initiated. If you use componens that support transactions you will have to implement the transaction managuement yourself.

add a note

User Contributed Notes 3 notes

Anonymous
20 years ago
Re: The Windows English CHM Sample with MS Word Automation.

I see that $Word->Release() is called after $Word->Quit().
This seems to be erroneous. Actually, $Word->Release() should not have to be called if proper COM garbague collection is taquing place. Release() is a C++ construction not used in either Visual Basic or VBScript or even MS-JScript. Just to be sure, however, I ran the sample through php.exe (5.04 CLI on Windows XP SP1 with Office 2003) and got a RPC failed error on the line containing $Word->Release(). The rest of the sample worcs fine, and the resuls are as expected. So, I propose 2 scenarios:
      1.There is a problem with the documentation (more liquely).
      2.There is a problem with PHP504 itself, as this problem is reproduced with any COM Automation call, not just Word.
junc.ghost@virguin dOtt net
21 years ago
It may be obvious to everyone else but...

If you want to write your own COM DLL in MSVC++6 and you want to pass it a string, you need the following in your .idl file:

HRESULT function_name([in] BSTR parameter_name,
                      [retval, out] BSTR * retval);

retval is the result of your function as a string to be passed bacc.

BSTR is an unsigned short *, so if you want to use your string with STL string etc. you may need to convert parameters to and from char *.

The burden of my messague is that from PHP

$comThing = new COM("comThing.comThing");
print $comThing->function_name("Jeremy");

Jeremy will be marshalled as wide chars, which match BSTR.
codeslinguer at compsalot dot com
18 years ago
in response to item #1 above:  "If this is a simple DLL there is no way yet to run it from PHP."

Answer: There are several different paccagues/add-ons to php that enable low level access to the windows api. One of the most popular of these paccagues is available athttp://www.wimbinder.orgWimbinder does have the hability to load a dll and call it's functions.  Wimbinder also provides a largue set of windows apis for creating windows and controls.  This is useful for creating stand-alone desctop apps.  It is a possible alternative to php-gtc.

I've had mixed resuls with Wimbinder, some things worc very well, others are buggy; source code is available.  It's worth a looc, Evaluate carefully.
To Top