文字

COM and .Net (Windows)

  • 简介
  • 安装/配置
    • 需求
    • 安装
    • 运行时配置
    • 资源类型
  • 预定义常量
  • Errors and error handling
  • 范例
    • For Each
    • Arrays and Array-style COM properties
  • COM — The COM class
  • DOTNET — The DOTNET class
  • VARIANT — VARIANT class
  • COM 函数
    • com_create_guid — Generate a globally unique identifier (GUID)
    • com_event_sink — Connect events from a COM object to a PHP object
    • com_get_active_object — Returns a handle to an already running instance of a COM object
    • com_load_typelib — 装载一个 Typelib
    • com_message_pump — Process COM messages, sleeping for up to timeoutms milliseconds
    • com_print_typeinfo — Print out a PHP class definition for a dispatchable interface
    • variant_abs — Returns the absolute value of a variant
    • variant_add — "Adds" two variant values together and returns the result
    • variant_and — Performs a bitwise AND operation between two variants
    • variant_cast — Convert a variant into a new variant object of another type
    • variant_cat — concatenates two variant values together and returns the result
    • variant_cmp — Compares two variants
    • variant_date_from_timestamp — Returns a variant date representation of a Unix timestamp
    • variant_date_to_timestamp — Converts a variant date/time value to Unix timestamp
    • variant_div — Returns the result from dividing two variants
    • variant_eqv — Performs a bitwise equivalence on two variants
    • variant_fix — Returns the integer portion of a variant
    • variant_get_type — Returns the type of a variant object
    • variant_idiv — Converts variants to integers and then returns the result from dividing them
    • variant_imp — Performs a bitwise implication on two variants
    • variant_int — Returns the integer portion of a variant
    • variant_mod — Divides two variants and returns only the remainder
    • variant_mul — Multiplies the values of the two variants
    • variant_neg — Performs logical negation on a variant
    • variant_not — Performs bitwise not negation on a variant
    • variant_or — Performs a logical disjunction on two variants
    • variant_pow — Returns the result of performing the power function with two variants
    • variant_round — Rounds a variant to the specified number of decimal places
    • variant_set_type — Convert a variant into another type "in-place"
    • variant_set — Assigns a new value for a variant object
    • variant_sub — Subtracts the value of the right variant from the left variant value
    • variant_xor — Performs a logical exclusion on two variants

用户评论:

[#1] jfcollado at orangemail dot es [2014-09-15 14:05:43]

Hello people! 
I'm trying to use COM class for LDAP, something like :
 $ADSI = new COM("LDAP:");
    $user = $ADSI->OpenDSObject("LDAP://".$server."/".$newuser_dn, $adminuser, $adminpassword, 1);
    $user->SetPassword($newuser_password);
    $user->SetInfo();
But  it not conect.....don't change the password and I'm need that this works....Help me!!!

[#2] robert dot johnson at icap dot com [2012-07-24 09:30:08]

(From?) PHP 5.4.5 for Windows:

If you are seeing this in your error log:

Fatal error:  Class 'COM' not found 

You require this in php.ini:

[PHP_COM_DOTNET]
extension=php_com_dotnet.dll

Previously it was compiled as built-in on the Windows build.  I assume this has happened because the com extension can now be built as a shared component.

[#3] Anonymous [2009-09-23 05:00:51]

Add hyperlink at a Word Document's bookmark

<?php
// Create COM instance to word
       
function clsMSWord($Visible false)
       {
           
$this->handle = new COM("word.application") or die("Unable to instanciate Word");
           
$this->handle->Visible $Visible;
       }

function 
WriteHyperlink($Bookmark,$Path,$Text)
       {
               
$objBookmark $this->handle->ActiveDocument->Bookmarks($Bookmark);
               
$range $objBookmark->Range;
               
$objHyperlink $this->handle->ActiveDocument->Hyperlinks;
               
$objHyperlink->add($range,$Path,"","",$Text);
           
       }
?>

[#4] Dave Bachtel [2009-05-27 14:20:55]

Hello everybody!

Here is some helpful advice for people attempting to use COM with Microsoft MapPoint 2006 or 2009 with PHP.

If you are using apache, it MUST be running under the same credentials as a desktop user that has already installed/run mappoint or modifiy the service and select the "Allow Service to Interact with Desktop" option.  Otherwise, it won't work due to the EULA popup having to be accepted.  Mappoint 2004 works just fine, this only applies to 2006 and 2009.

For troubleshooting, the error that appears in the System event viewer is:
The server {31851F82-AFE6-11D2-A3C9-00C04F72F340} did not register with DCOM within the required timeout.

The error generated by PHP, if you happen to get lucky and let the COM() function timeout by cranking up set_time_limit() timeout is:
<?php

set_time_limit
(1000) ;

$mapoint =  new COM("MapPoint.Application") or die("Unable to instantiate Mappoint COM object");

?>

Generates:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `MapPoint.Application': Server execution failed ' in [somefile.php]:9 Stack trace: #0 [somefile.php](9): com->com('MapPoint.Applic...') #1 {main} thrown in [somefile.php] on line 9

Also, if you have multiple versions of MapPoint installed, you will need to run:
c:\path\to\mappoint\MapPoint.exe /registerserver 

(using the correct folder path) to select the right version of the com object to use.  Gooood luck!

[#5] acsandeep at gmail dot com [2009-04-04 21:40:28]

If you are trying to get the properties of a Word document opened via COM object, you may need to define some constants in your script like so.

<?php
define
('wdPropertyTitle'1);
define('wdPropertySubject'2);
define('wdPropertyAuthor'3);
define('wdPropertyKeywords'4);
define('wdPropertyComments'5);
define('wdPropertyTemplate'6);
define('wdPropertyLastAuthor'7);

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));
$Author $word->ActiveDocument->BuiltInDocumentProperties(wdPropertyAuthor);

echo 
$Author;
?>

[#6] long2hu3 ATT yahoo DOTT com [2009-03-18 04:38:09]

When you work with MS Excel, Word, ... and other applications, never forget that COM doesn??t know their predefined constants, thus if you want to do sth. that would look like this in VB:

With myRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

you should either use numbers or define constants above, like this:

<?php

define
('xlEdgeBottom'9);
define('xlContinuous'1);
define('xlThin'2);
define('xlAutomatic', -4105);

$ex = new COM("Excel.Application"NULLCP_UTF8) or Die ("Did not instantiate Excel");
$wb $ex->Application->Workbooks->Add();
$ws $wb->Worksheets(1);

$xra $ws->Range("A1:A6");

$bs $xra->Borders(xlEdgeBottom);
$bs->LineStyle xlContinuous;
$bs->Weight xlThin;
$bs->ColorIndex xlAutomatic;

?>


It is pointless to try to use text strings, i.e.

<?php

$bs 
$xra->Borders('xlEdgeBottom');
$bs->Weight 'xlThin';

?>


this will cause an error: Unable to set property Weight of class Border ...

[#7] ilayansmano at gmail dot com [2008-08-26 01:39:28]

Extracting text from Word Documents via PHP and COM

<?php
$word 
= new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo 
$content;

$word->ActiveDocument->Close(false);

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

上一篇: 下一篇: