Solved

PHP COM problem

Posted on 2013-11-29
46
2,477 Views
Last Modified: 2013-12-26
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 ?
0
Comment
Question by:darroosh
  • 20
  • 18
  • 5
  • +1
46 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39686043
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.
0
 

Author Comment

by:darroosh
ID: 39686076
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 ?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39686087
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39686114
Please show us the rest of the PHP code, thanks.
0
 

Author Comment

by:darroosh
ID: 39686118
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
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39686220
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.
0
 

Author Comment

by:darroosh
ID: 39686542
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
0
 

Author Comment

by:darroosh
ID: 39686546
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 ?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39686560
Here's basic info on the permissions and users in IIS: http://support.microsoft.com/kb/981949
0
 

Author Comment

by:darroosh
ID: 39686731
Couldn't find anything helpful in it ..

What should I do ?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39687180
Looks like you have already clicked on "Request Attention" so I don't have anything more at this time.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39693869
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.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39693916
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
0
 

Author Comment

by:darroosh
ID: 39694082
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 ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39694134
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
0
 

Author Comment

by:darroosh
ID: 39694839
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 )??
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39694882
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
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39694969
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
?>
0
 

Author Comment

by:darroosh
ID: 39695099
- 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();
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39695113
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.");

dcom
0
 

Author Comment

by:darroosh
ID: 39695123
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}
0
 

Author Comment

by:darroosh
ID: 39695155
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 ..
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:darroosh
ID: 39695156
My email is titled :  " COM problem "
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39695197
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 :)
0
 

Author Comment

by:darroosh
ID: 39695211
Thank you , happy dreams , Hoping another expert can help me , Or I will be waiting you to wake up ..
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39695212
One thought before I crash is on the server, run winword.exe /REGISTER

now try creating the com using word.application
0
 

Author Comment

by:darroosh
ID: 39695279
I tried , With no change at all ..
0
 

Author Comment

by:darroosh
ID: 39695287
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++ ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39697162
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

0
 

Author Comment

by:darroosh
ID: 39701626
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 ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39702092
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
0
 

Author Comment

by:darroosh
ID: 39702176
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++ ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39702410
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.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39702411
Have you tried to repair/reinstall office?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39702661
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.
0
 

Author Comment

by:darroosh
ID: 39718620
But if I uninstall plesk , I will be able to create websites on my VPs ?? and they will work fine?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39719198
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.
0
 

Author Comment

by:darroosh
ID: 39719620
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  ?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39719851
Ok so  are you creating hosting accounts? It sounds like you set this up so you can host websites for your clients, right?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39720415
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?
0
 

Author Comment

by:darroosh
ID: 39734108
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,
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39734122
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
0
 

Author Comment

by:darroosh
ID: 39734129
Thank you very much for your help..
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39739354
I think we can confirm that this as answered as the testing (vb script etc) on the server indicated that it is a problem with your server setup and not the COM object.

So how would you like to close the question?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now