Bug #43506 [Opn->Nab]: com_get_active_object always fails

From: Date: Fri, 07 Feb 2020 11:08:51 +0000
Subject: Bug #43506 [Opn->Nab]: com_get_active_object always fails
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225423@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=43506&edit=1

 ID:                 43506
 Updated by:         cmb@php.net
 Reported by:        bvandermerwe at kbcat dot com
 Summary:            com_get_active_object always fails
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            COM related
 Operating System:   Windows XP
 PHP Version:        5.2.5
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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>


Previous Comments:
------------------------------------------------------------------------
[2009-01-05 15:38:38] 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?

------------------------------------------------------------------------
[2009-01-03 21:47:22] 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.

------------------------------------------------------------------------
[2008-11-06 11:46:02] 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

------------------------------------------------------------------------
[2007-12-05 18:14:58] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=43506&edit=1


Thread (5 messages)

« previous php.bugs (#225423) next »