XII. COM と .Net (Windows)

導入

COMはComponent Object Model; の略語であり、 DCE RPC (オープンスタンダード) の最上位のオブジェクト指向レイヤーです。 COMは、コール手順を共通化し、あらゆる言語でコードを記述し、 (COMに対応した)他の言語で書かれたコードをコール、相互運用することを可能にします。 あらゆる言語で書くことを可能にするだけではなく、 同じ実行形式の一部となることすら不要です。 コードは、同じマシンで実行される他のプロセスのコードであるDLLからロード可能したり、 または、リモートマシン上の他のプロセスにあるコードをDCOM (Distributed COM) で 利用することができます。 この場合、コードの中では、コンポーネントの存在する場所を意識する必要はありません。

OLEオートメーションと呼ばれるCOMのサブセットがあります。 これは、COMオブジェクトに祖な結合を行うことができるCOMインターフェイスを 提供します。これにより、コンパイル時にオブジェクトの動作を知ることなく、 実行時にコールを行うことができるようになります。 PHP COMエクステンションは、OLEオートメーションを使用して、 スクリプトから互換性のあるオブジェクトを作成/コールすることができます。 技術的に述べると、 全てのCOMオブジェクトがOLE互換であるというわけではないため、 実際には、このエクステンションは、"PHPのOLEオートメーション エクステンション"と呼ばれるべきものです。

ところで、なぜ、COMを使用する必要があるのでしょう? COMは、 Windows環境で、アプリケーションとコンポーネントを結び付ける代表的な手法の一つで、 COMを使用してMicrosoft Wordを起動し、 ドキュメントテンプレートを埋めて、Word文書として結果を保存し、Webサイトの 訪問者に送信することができます。 また、COMを使用して、ネットワークの管理タスクを処理したり、 IISを設定することができます。これらは、最も一般的な使用法にすぎません。 COMでできることはまだまだたくさんあります。

PHP 5以降、このエクステンション(とこの文書)は、最初から書き直され、 古い紛らわしい部分は、削除されました。さらにMicrosoftにより提供された COMとの相互運用レイヤーを用いて .Netアセンブリのインスタンス化と生成をサポートしました。

PHP 5におけるこのエクステンションの変更点の概要については、 この文章 を参照してください。

要件

COM関数は、Windows版のPHPでのみ利用可能です。

.Net サポートは、PHP 5 と .Net ランタイムを必要とします。

インストール手順

これらの関数はPHPコアに含まれるため、使用す る際にインストールは不要です。

Windows版のPHPには この拡張モジュールのサポートが組み込まれています。これらの関数を使用 するために拡張モジュールを追加でロードする必要はありません。

あなたには、(MS Wordのような)使用する様々なCOMオブジェクトのインストールを正しく 行っておく責任があります。 PHPにこれら全てをバンドルすることはできません。

foreach

PHP 5以降、標準的なCOM/OLE IEnumVariantの内容について PHPのforeach項第16章命令を 使用し、反復処理を行うことができます。分かりやすく言うと、 これは、VB/ASPのコードでFor Eachを使用できる 場所には、foreachを使用できるということ意味します。

例 1. ASP におけるFor Each

<%
Set domainObject = GetObject("WinNT://Domain")
For Each obj in domainObject
  Response.Write obj.Name & "<br />"
Next
%>

例 2. PHP 4におけるwhile() ... Next()

<?php
$domainObject
= new COM("WinNT://Domain");
while (
$obj = $domainObject->Next()) {
   echo
$obj->Name . "<br />";
}
?>

例 3. PHP 5におけるforeach

<?php
$domainObject
= new COM("WinNT://Domain");
foreach (
$domainObject as $obj) {
   echo
$obj->Name . "<br />";
}
?>

配列と配列形式のCOMプロパティ

Many COM objects expose their properties as arrays, or using array-style access. In PHP 4, you may use PHP array syntax to read/write such a property, but only a single dimension is allowed. If you want to read a multi-dimensional property, you could instead make the property access into a function call, with each parameter representing each dimension of the array access, but there is no way to write to such a property.

PHP 5 introduces the following new features to make your life easier:

  • Access multi-dimensional arrays, or COM properties that require multiple parameters using PHP array syntax. You can also write or set properties using this technique.

  • Iterate SafeArrays ("true" arrays) using the foreach項第16章 control structure. This works because SafeArrays include information about their size. If an array-style property implements IEnumVariant then you can also use foreach for that property too; take a look at foreach for more information on this topic.

例外 (PHP 5)

この This extension will throw instances of the class com_exception whenever there is a potentially fatal error reported by COM. All COM exceptions have a well-defined code property that corresponds to the HRESULT return value from the various COM operations. You may use this code to make programmatic decisions on how to handle the exception.

実行用の設定

これらの関数の動作は、php.iniの設定により変化します。

表 1. COM設定オプション

名前デフォルト変更の範囲
com.allow_dcom"0"PHP_INI_SYSTEM
com.autoregister_typelib"0"PHP_INI_ALL
com.autoregister_verbose"0"PHP_INI_ALL
com.autoregister_casesensitive"1"PHP_INI_ALL
com.code_page""PHP_INI_ALL
com.typelib_file""PHP_INI_SYSTEM
PHP_INI_*定数に関する詳細と定義については、 ini_set()を参照してください。

以下に設定ディレクティブに関す る簡単な説明を示します。

com.allow_dcom

When this is turned on, PHP will be allowed to operate as a D-COM (Distributed COM) client and will allow the PHP script to instantiate COM objects on a remote server.

com.autoregister_typelib

When this is turned on, PHP will attempt to register constants from the typelibrary of objects that it instantiates, if those objects implement the interfaces required to obtain that information. The case sensitivity of the constants it registers is controlled by the com.autoregister_casesensitive configuration directive.

com.autoregister_verbose

When this is turned on, any problems with loading a typelibrary during object instantiation will be reported using the PHP error mechanism. The default is off, which does not emit any indication if there was an error finding or loading the type library.

com.autoregister_casesensitive

When this is turned on (the default), constants found in auto-loaded type libraries will be registered case sensitively. See com_load_typelib() for more details.

com.code_page

It controls the default character set code-page to use when passing strings to and from COM objects. If set to an empty string, PHP will assume that you want CP_ACP, which is the default system ANSI code page.

If the text in your scripts is encoded using a different encoding/character set by default, setting this directive will save you from having to pass the code page as a parameter to the COM class constructor. Please note that by using this directive (as with any PHP configuration directive), your PHP script becomes less portable; you should use the COM constructor parameter whenever possible.

注意: This configuration directive was introduced with PHP 5.

com.typelib_file

When set, this should hold the path to a file that contains a list of typelibraries that should be loaded on startup. Each line of the file will be treated as the type library name and loaded as though you had called com_load_typelib(). The constants will be registered persistently, so that the library only needs to be loaded once. If a type library name ends with the string #cis or #case_insensitive, then the constants from that library will be registered case insensitively.

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

CLSCTX_INPROC_SERVER (integer)

CLSCTX_INPROC_HANDLER (integer)

CLSCTX_LOCAL_SERVER (integer)

CLSCTX_REMOTE_SERVER (integer)

CLSCTX_SERVER (integer)

CLSCTX_ALL (integer)

VT_NULL (integer)

VT_EMPTY (integer)

VT_UI1 (integer)

VT_I2 (integer)

VT_I4 (integer)

VT_R4 (integer)

VT_R8 (integer)

VT_BOOL (integer)

VT_ERROR (integer)

VT_CY (integer)

VT_DATE (integer)

VT_BSTR (integer)

VT_DECIMAL (integer)

VT_UNKNOWN (integer)

VT_DISPATCH (integer)

VT_VARIANT (integer)

VT_I1 (integer)

VT_UI2 (integer)

VT_UI4 (integer)

VT_INT (integer)

VT_UINT (integer)

VT_ARRAY (integer)

VT_BYREF (integer)

CP_ACP (integer)

CP_MACCP (integer)

CP_OEMCP (integer)

CP_UTF7 (integer)

CP_UTF8 (integer)

CP_SYMBOL (integer)

CP_THREAD_ACP (integer)

VARCMP_LT (integer)

VARCMP_EQ (integer)

VARCMP_GT (integer)

VARCMP_NULL (integer)

NORM_IGNORECASE (integer)

NORM_IGNORENONSPACE (integer)

NORM_IGNORESYMBOLS (integer)

NORM_IGNOREWIDTH (integer)

NORM_IGNOREKANATYPE (integer)

NORM_IGNOREKASHIDA (integer)

DISP_E_DIVBYZERO (integer)

DISP_E_OVERFLOW (integer)

MK_E_UNAVAILABLE (integer)

以下も参照ください

For further information on COM read the COM specification or perhaps take a look at Don Box's Yet Another COM Library (YACL). You might find some additional useful information in our FAQ for 第72章. If you're thinking of using MS Office applications on the server side, you should read the information here: Considerations for Server-Side Automation of Office.

目次
COM -- COMクラス
DOTNET -- DOTNET class
VARIANT -- VARIANT クラス
com_addref --  コンポーネントリファレンスカウンタを増やす
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_get --  COMコンポーネントのプロパティの値を得る
com_invoke --  COMコンポーネントのメソッドをコールします。
com_isenum -- IEnumVariantを取得する
com_load_typelib -- Typelibをロードする
com_load --  COMコンポーネントへの新規リファレンスを作成する
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
com_propget --  COMコンポーネントのプロパティの値を得る
com_propput --  COMコンポーネントのプロパティに値を代入する
com_propset --  COMコンポーネントのプロパティに値を代入する
com_release --  コンポーネントリファレンスカウンタを減らす [廃止]
com_set --  COMコンポーネントのプロパティに値を代入する
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 and returns the result
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 and returns the result
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 and returns the result
variant_xor --  Performs a logical exclusion on two variants