Link to home
Start Free TrialLog in
Avatar of darroosh
darroosh

asked on

PHP COM problem

Hi all ,

I'm trying to use PHP COM class , working fine with me on my local server ..

But online :  On my godaddy VPS ( windows server 2008 , Plesk ) ,  When trying to use COM , the following  error appear :


PHP Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Invalid syntax ' in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php:17 Stack trace: #0 C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php(17): com->com('word.applicatio...') #1 {main} thrown in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php on line 17
Line 17 is the following:
$word = new COM("word.application") or die ("Could not initialise MS Word object.");

Open in new window


Note : PHP is running as CGI application ( PHP 5.4.15 )..
And this is copied from my PHPInfo :
com_dotnet

COM support:	enabled
DCOM support:	disabled
.Net support:	enabled


What should I do ?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I suspect that new COM("word.application") requires Microsoft Word to be installed on the computer.  I think I can pretty well guarantee that it is Not installed on your Godaddy server.
Avatar of darroosh
darroosh

ASKER

Thank you for your help ..

I Installed Microsoft word now .. The error message changed to be :

PHP Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Class not registered ' in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php:17 Stack trace: #0 C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php(17): com->com('word.applicatio...') #1 {main} thrown in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php on line 17

What may be the problem now ?
Someone else will probably have to help you.  I don't know what it takes to 'register a class'.  Click on "Request Attention" above to get more help.
Please show us the rest of the PHP code, thanks.
There is no PHP code affecting it now ...
I deleted everything in the page , Only this line is the remaining in the PHP page now :

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
It gives the same error :

PHP Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Class not registered ' in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php:2 Stack trace: #0 C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php(2): com->com('word.applicatio...') #1 {main} thrown in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php on line 2



Thanks DaveBaldwin for your help
Here's a page about PHP COM settings: http://www.php.net/manual/en/com.configuration.php   I also saw a suggestion that the COM objects may need to have the same permissions as the web server user.  They would not normally have them.
I tried many of the configurations in the referred page with no effect..

About permissions for the COM objects , this seems to be good suggestion ,  but can I give it this permission ??
I think I have to give this permission to the apache itself , how to do that ?
Note: I use windows server 2008 VPS, Control panel : Plesk 11.09
PHP run as CGI application
OOps.. I found that My server doesn't use apache , It uses Microsoft IIS ... does this affect ? And to make start with permissions as the server user ?
Here's basic info on the permissions and users in IIS: http://support.microsoft.com/kb/981949
Couldn't find anything helpful in it ..

What should I do ?
Looks like you have already clicked on "Request Attention" so I don't have anything more at this time.
You need to set the permissions in COM not IIS.  I had to do I this once to run a custom windows app through a browser.
Referenced from: http://www.figured-it-out.com/figured-out.php?sid=24

Essentially you need to give security access to the DCOM to execute the Word application via PHP.

It does take a bit of tweaking of these settings but follow what they've got here first and see how you go.  It does mention to Add "Everyone" to the permissions.  On my intranet I was fine with this, it posed a very small risk.  On the internet that's a different story so maybe add it in to get it working but then explore that it may in fact be IIS_USER or IIS_machineName

Also make sure you can run DCOM in PHP:

In PHP.ini
1. Open PHP.INI
2. search for com.allow.dcom
3. uncomment it and set it to true
4. Save and close PHP.ini
I followed its instructions with the same result , no change ..
Note that there solution is when you have the error " Access denied  "  while my error msg is " class not registered " 

Any more help ?
Yes, are you running the 64 or 32 bit version of PHP?  To access Word you'll need to run the 32-bit version as word is also 32-bit.  You can find the registration by running component services 32 bit

%windir%\syswow64\comexp.msc
How do I know which version of PHP i'm running ?
Also , I didn't get , How to register a class (This may be the solution of my problem because the error is that COM class is n't registered )??
I suspect the class is registered. When I got you to run the command before it was for 64 bit com objects. To view the 32 bit you need to run that command in my last post
%windir%\syswow64\comexp.msc
Expand dcom and you should see word
To know what you're running, create a sample PHP page:

<?php
// the integer size in bytes ie 8 bits
echo PHP_INT_SIZE; // 4 is 32-bit, 8 is 64-bit
?>
- I tried to run %windir%\syswow64\comexp.msc , then expanded : Computers ->My Computer -> DCOM config -> I found " Microsoft word 97-2003 documents ..


I checked my PHP to find it's  32 bit ...



Now what should I do to get it working ???! And why I'm getting that message :
"
exception 'com_exception' with message 'Failed to create COM object `word.application': Class not registered ' in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php:2 Stack trace: #0 C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php(2): com->com('word.applicatio...') #1 {main}
"

This is the sample page from my server:
https://68.178.129.200:8443/sitepreview/http/ajobsy.secureserver.net/?randomHash=8d787314cbd382e491f129ad8e569652

COM error is at the bottom of the page below PHPinfo();
That's good news at least as we've determined that you're running the right setup to get this working.

I had to use the application id so that your code would be

$word = new COM("{00020906-0000-0000-C000-000000000046}") or die ("Could not initialise MS Word object.");

User generated image
I did that , The error now changed to be :
exception 'com_exception' with message 'Failed to create COM object `{00020906-0000-0000-C000-000000000046}': Access is denied. ' in C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php:21 Stack trace: #0 C:\Inetpub\vhosts\ajobsy.secureserver.net\httpdocs\index.php(21): com->com('{00020906-0000-...') #1 {main}
tagit , because my VPS is just for testing com and holds no data at all ..  I sent to your email : tagit at e-e dot com , an email containing the login information to my VPS Plesk and to its remote desktop connection so you can login and check the problem yourself ..
If you could solve it , plz let me know the details of the problem and the details of the solution ..
My email is titled :  " COM problem "
ok thanks for the access... everything I've tried has failed.  I'm going to try and get it working on a virtual machine I have here as I think that would be more efficient.
I've gotta get some sleep as it's quite late here (Australia).  Maybe another expert can pick this up if need be.
Cheers :)
Thank you , happy dreams , Hoping another expert can help me , Or I will be waiting you to wake up ..
One thought before I crash is on the server, run winword.exe /REGISTER

now try creating the com using word.application
I tried , With no change at all ..
I found that post by searching for  a similar error  but using c++:


The most likely explanation: you are building your COM object as a 32-bit DLL, but the registration has been performed as a 64-bit DLL.

The treatment: open an admin privileged command window and navigate to the location of your DLL (C:\Users\wiocl2\Documents\Visual Studio 2010\Projects\CarDLL\debug). Once there, type:

c:\windows\sysWOW64\regedit <filename of .reg file whose contents are displayed above>
This will run the 32-bit version of REGEDIT, ensuring that the registry entries are created in the correct part of the hive. To verify this, you should see an entry for {d969084c-b758-43ea-a218-a48763167abd} in HKLM\Software\Wow6432Node\Classes\CLSID, not HKLM\Software\Classes\CLSID.

DllRegisterServer is a method you can implement in your COM server DLL, and is required if you want to use regsvr32 to perform the same operation you are currently using the .REG approach for. The same caveat applies: for a 32-bit DLL, you'll need to invoke c:\windows\sysWOW64\regsvr32.exe.

And Yes! COM is still mostly alive and well :) At least there is still standard support for it in VS 2012.



I'm not understanding it well but I think It's exactly the solution I need , So if you can understand it , can you translate  it to tell me what I have to do using PHP not c++ ?
ive tested this so i know it works but still get the same issue on your machine.  
I also ran the command WINWORD.EXE -REGSERVER to try and register the app again however it opened word and wanted to activate it.  I suspect this may be an issue and you should activate it before it will allow you to use it??

<?php
error_reporting(E_ALL);

echo PHP_INT_SIZE; // 4 is 32-bit, 8 is 64-bit

echo "<hr />";
echo "<hr />";

try {

$word = new COM('Word.Application') ;

$word->Visible = 1;

//open an empty document
$word->Documents->Add();

//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("c:\\Useless test.doc");

$word->Quit();
$word = null;
unset($word);

} catch (com_exception $e) {

echo 'error: '.$e;
}

?>

Open in new window

Thank you for your help ..
But the problem still exists .
Note : I use the same not activated version on my local PC and COM works fine , But any way , I activated word on the server , and the problem still persists , Any other suggestions ?
Yep mb my next thoughts are around security to the winword.exe and also the registry.
Have you restarted the server in the last 24 hours? If not do this too
I already did that .. No change ..

Me too think about the registery ..

But did you have a look at that post ( I think it may hold the solution)  :

I found that post by searching for  a similar error  but using c++:


The most likely explanation: you are building your COM object as a 32-bit DLL, but the registration has been performed as a 64-bit DLL.

The treatment: open an admin privileged command window and navigate to the location of your DLL (C:\Users\wiocl2\Documents\Visual Studio 2010\Projects\CarDLL\debug). Once there, type:

c:\windows\sysWOW64\regedit <filename of .reg file whose contents are displayed above>
This will run the 32-bit version of REGEDIT, ensuring that the registry entries are created in the correct part of the hive. To verify this, you should see an entry for {d969084c-b758-43ea-a218-a48763167abd} in HKLM\Software\Wow6432Node\Classes\CLSID, not HKLM\Software\Classes\CLSID.

DllRegisterServer is a method you can implement in your COM server DLL, and is required if you want to use regsvr32 to perform the same operation you are currently using the .REG approach for. The same caveat applies: for a 32-bit DLL, you'll need to invoke c:\windows\sysWOW64\regsvr32.exe.

And Yes! COM is still mostly alive and well :) At least there is still standard support for it in VS 2012.




I'm not understanding it well but I think It's exactly the solution I need , So if you can understand it , can you translate  it to tell me what I have to do using PHP not c++ ?
Yes I did see that post but the problem is that it's not a dll but an .exe, which is why I tried the winword.exe -REGSERVER that should do the same thing but worth trying again as I can't remember if I tried to run that with Admin permissions.  Right click the command window shortcut and run as administrator.  Then go to the winword.exe path and run the winword.exe -REGSERVER command.
Have you tried to repair/reinstall office?
Have a look on your desktop.  The class is definitely registered as I can run a vbs script (called test) that can access the word object.  I also tried set up an asp page but plesk is interferring and passing this script to the php_cgi.exe so the cgi/isapi configuration is out of whack but that might be by design.
It really looks as if plesk is configuring this so and may be stopping the script from executing.  If it's possible, uninstall plesk or hand back control to IIS to create a simple asp page and see if you can create the word object.  This will isolate or rule out plesk as the issue.
But if I uninstall plesk , I will be able to create websites on my VPs ?? and they will work fine?
Yes of course! But before you get rid of it, I have to ask, why are you using it? It's usually installed by hosting providers.
I use it only to create hosting accounts ..

And it's a good idea to uninstall it ..
But may  I ask .. If I remove it ,  then uninstall IIS , then install xampp , Will I still be able to create hosting accounts ?


If yes, can you plz send me a reference  ?
Ok so  are you creating hosting accounts? It sounds like you set this up so you can host websites for your clients, right?
The idea behind using IIS is to see if it can spawn Word.  Because using plesk and PHP is not working but the class is definitely registered as you can see from running the vbs script on the desktop.

Don't go changing too much at this stage.  The idea is to work out where the issue with creating a COM object lies and at this stage I suspect the plesk setup.  I'm not even sure if it is running it's own webserver or tied into IIS.  Can you confirm which?
My purchased VPS with godaddy ended and I didn't renew it ..
I purchased a new dedicated server running on windows server 2008 with futurehosting.com

The server doesn't have Plesk or any other  control panel , It doesn't contain any installed program at all ..

Now I want  to start preparing my server for use .. What should I do in my server ?  regarding  webserver ,  security , and any other thing I should do ..
Please help..

Regards,
Your best bet Is open a new question and close this one. It would attract the right experts, my knowledge is fairly limited in that respect.
When it comes to running php again then I'll jump o in
Thank you very much for your help..
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial