php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43506 com_get_active_object always fails
Submitted: 2007-12-05 18:14 UTC Modified: 2020-02-07 11:08 UTC
Votes:11
Avg. Score:4.1 ± 1.0
Reproduced:10 of 10 (100.0%)
Same Version:6 (60.0%)
Same OS:7 (70.0%)
From: bvandermerwe at kbcat dot com Assigned: cmb (profile)
Status: Not a bug Package: COM related
PHP Version: 5.2.5 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bvandermerwe at kbcat dot com
New email:
PHP Version: OS:

 

 [2007-12-05 18:14 UTC] bvandermerwe at kbcat dot com
Description:
------------
com_get_active_object always returns "Operation Unavailable " even when it should work for sure. Let me demonstrate:

Start up Microsoft Word (for example) on the server machine where Apache and PHP are running. Then put the following text in a file called x.vbs:

Dim app
Set app = GetObject(,"Word.Application")
if app is nothing then
   wscript.echo "Got nothing"
else
   wscript.echo "Got it!"
end if

Execute it by typing: cscript x.vbs.
Note that it works fine. Yet the following line in a PHP script always returns "Operation Unavailable ":

$obj = com_get_active_object("Word.Application"); 

Using: $obj = new COM("Word.Application") works (meaning PHP COM is working).

I just upgraded Apache to 2.2.6 and PHP 5.2.5 (using the Windows installation executable binaries with pretty much default settings, except PHP is in c:\PHP525 and I checked the options for MS and MYSQL databases). Bugzilla and several PHP applications all work fine. 

But it seems com_get_active_object *always* fails. I have Googled and I can not find any examples of it out there or any security or other settings related to it.

If it just calls GetObject, then how come calling GetObject from VBScript works but in PHP does not? I did discover that some GetObject calls are disabled under IIS for security reasons, but I am using Apache and there is no reference to any setting that needs to be turned on before this will work.

Reproduce code:
---------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
</head>
<body>
<?php
	  echo("<p>Attempting to retrieve COM Object</p>");
	  $obj = com_get_active_object("Word.Application");  //Fails! 	          if ($obj) {
	    echo("<p>Object Found</p>");
	  } else {
	    echo("<p>Object NOT Found</p>");
	  }
?>
</body>
</html>


Expected result:
----------------
No error. You should see:

Attempting to retrieve COM Object

Object Found

Actual result:
--------------
You see:

Attempting to retrieve COM Object

If PHP error tracing is enabled you also see:

Fatal error: Uncaught exception 'com_exception' with message 'Operation unavailable ' in C:\ApacheDocumentRoot\test_com.php:10 Stack trace: #0 C:\ApacheDocumentRoot\test_com.php(10): com_get_active_object('Word.Application') #1 {main} thrown in C:\ApacheDocumentRoot\test_com.php on line 10

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-06 11:46 UTC] tom dot neil dot bell at gmail dot com
Suffered this issue today trying enumerate a internet explorer 
window. Turns out Internet Explorer doesn't register it self with the 
"Running Object Table" so this function returns that error. 

Work arounds are to either create a Browser Helper Object that will 
add the IE instance to the ROT, or enumerate all windows via the 
Shell.Application COM and iterate through them looking for 
iexplore.exe . 

Tom
 [2009-01-03 21:47 UTC] gerrit at timingteam dot nl
I experienced the same problem. Googling the internet I found a clue. Somebody had a likewise problem. But he noticed that it only happened when Apache was started as a service and not when Apache was started in a console. I checked this in my situation. And indeed, all works fine when you start Apache in a console. And you get the reported problem when you start Apache as a service.
Hope this gives some clue to resolving the problem.
Greets, Gerrit.
 [2009-01-05 15:38 UTC] bvandermerwe at kbcat dot com
Yes.... when Apache runs as a service it runs under a different user than when you start it from the command line. You can change the user the service uses, though that does not fix it. We have had the same issues with .NET services. You just get these permission type errors no matter how much you Google for the proper settings. And Microsoft's official position on this situation is that you should not be using Excel/Word from a service. Gotta love Microsoft... hahaha.

Of course in our case we are not using Excel/Word really, I use them as an example hoping that:
- Someone can post how you fix/set these permissions to get around this, if that is the cause.
- Perhaps PHP can be enhanced to return a more complete error message. "Operation unavailable" is useless (it could mean anything, e.g. this type of operation is not available because PHP no longer supports it or because PHP module xx is not installed, who knows). Rather something like "Unable to instantiation COM Object due to insufficient user permission" would help?
 [2020-02-07 11:08 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-02-07 11:08 UTC] cmb@php.net
I think the documentation[1] is (at least nowadays) pretty
clear on that:

| com_get_active_object() is similar to creating a new instance of
| a COM object, except that it will only return an object to your
| script if the object is already running. OLE applications use
| something known as the "Running Object Table" to allow well-known
| applications to be launched only once; this function exposes the
| COM library function GetActiveObject() to get a handle on a
| running instance.

> Rather something like "Unable to instantiation COM Object due to
> insufficient user permission" would help?

That would be guesswork on behalf of PHP, though.  You get exactly
the same result from GetActiveObject() if you have permission to
create a new Word.Application, but no instance of Word.Application
is running.

So, unless you are sure that a Word.Application is running, use
something like the following:

<?php
try {
    $obj = com_get_active_object("Word.Application");
} catch (com_exception $ex) {
    // perhaps no Word.Application running?
    $obj = new COM("Word.Application");
}
?>

[1] <https://www.php.net/com_get_active_object>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 04:01:29 2024 UTC