Description of PHP calling COM component. in PHP 4.2.0 to 4.2.3, you can use the w32api_register_function function to call an external DLL, provided that the extended php_w32api.dll needs to be opened in php. ini.
If PHP 5 is used, only the php com class is used to call the DLL.
Basic method: $ obj = new COM ("server. object ")
The following describes how to use PHP to call the COM component by using C # in the msvs. net2005 environment to create the COM component.
1. use C # to create a COM object
The COM object is a ClassLibrary class that generates DLL files. To create a simple COM object in the VS development environment, choose File> New> project> Visual C #> class library, and create a project named Comtest.
Note that calling the VC # object in COM requires the following conditions:
· The class must be public.
· Features, methods, and events must be public.
· Features and methods must be defined in class interfaces.
· Events must be defined in the event interface.
Not the public class members defined in these interfaces cannot be accessed by COM, but they can be accessed by other. NET Framework objects. To enable COM to access features and methods, we must define them in the class interface so that they have the DispId attribute and implement these features and methods in the class. The order in which these members are defined is the order in COM. To enable events in the COM domains class, you must define these events in the event interface and assign them the DispId attribute.
Each interface requires a GUID before the interface name. To generate a unique guid, run the guidgen.exe tool and select "registry format ". You can obtain it in Program Files \ Microsoft Visual Studio 2005 \ Common7 \ Tools \ guidgen.exe.
Note that you must set the following features before the class:
ClassInterface (ClassInterfaceType. None ),
ComSourceInterfaces (typeof (*** _ Events ))
ClassInterfaceType. None indicates that no class interface is generated for this class. if the interface is not explicitly implemented, the class can only be bound to access through IDispatch. You want to explicitly enable external objects to implement the functions of the class through the interface implemented by the class. this is also the recommended ClassInterfaceAttribute setting.
ComSourceInterfaces (typeof (*** _ Events) identifies many interfaces provided to external objects as COM Events.
The following is a sample code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;
Using System. Reflection;
Using System. Data. OleDb;
Using System. Data;
Using System. Collections;
Using System. Collections. Specialized;
Namespace Comtest
{
// Class interface
[Guid ("394BE3FE-18B8-4c5e-B611-75B5C5493A4E")]
Public interface ITest
{
String Test (string test );
[DispId (1)] // fixed statement, index number starting from 1
String About ();
[DispId (2)]
Int Add (int a, int B );
}
// Event interface
[Guid ("45875EE5-5C8D-4016-897A-FCC7DD5A6834"), // fixed writing method
InterfaceType (ComInterfaceType. InterfaceIsIDispatch)]
Public interface ITest_Events
{
}
// Class
[Guid ("854c2016-e7bf-41ea-8f09-b87da-8e9f8e"), // fixed writing method
ClassInterface (ClassInterfaceType. None ),
ComSourceInterfaces (typeof (ITest_Events)]
Public class Class2: ITest // This class inherits the above interface and implements the abstract method
{
Public string Test (string test)
{
Return test;
}
Public string About () {return "Welcome to http://www.35.com ";}
Public int Add (int a, int B) {return a + B ;}
}
}
Before creating a COM object, we must register the object with COM Interop. Right-click the project name in Solution Manager, click "properties" in the shortcut menu, and then click "generate" to hook "register for COM Interop. Enable AssemblyInfo. cs to set comVisible to true.
To uniquely identify the assembly, security policy, and version policy, you can create a strong name for the class library combination. Note: not required. To create a strong name, you must use the SN. EXE name. at the "start-VS2005-VS Tools-VS command prompt", enter:
Sn-k Comtest_Key.snk
Add Comtest_Key.snk to the project (under Program Files \ MS Visual Studio 2005 \ VC), open AssemblyInfo. cs, and add the following line:
[Assembly: AssemblyKeyFile ("Comtest_Key.snk")]
After the project is generated, the COM component can be obtained under bin.
2. PHP calls the COM component
Directly use the COM class in PHP for access.
The code is as follows:
$ B = new COM ("Comtest. Class2 ");
$ T1 = 126;
$ T2 = 456;
$ R = $ B-> add ($ t1, $ t2 );
$ F = $ B-> about ();
Echo $ r;
Echo $ f;
?>
In addition, you can use the COM class to access EXCEL, WORD, and database.
The following is a simple example of calling WORD:
// Start word
$ Word = new COM ("word. application") or die ("Unable to instancate Word ");
Print "Loaded Word, version {$ word-> Version} \ n ";
// Set it to the front
$ Word-> Visible = 1;
// Open an empty document
$ Word-> Documents-> Add ();
// Do whatever you want
$ Word-> Selection-> TypeText ("This is a test ...");
$ Word-> Documents [1]-> SaveAs ("Useless test.doc ");
// Close the word
$ Word-> Quit ();
// Release the object
$ Word-> Release ();
$ Word = null;
?>
The following is a simple example of accessing the MSSQL database system:
// Access the MSSQL database system
Echo 'Access MSSQL
';
$ DbConn = new COM ("ADODB. Connection") or die ("failed to create COM ");
$ ADO = 'provider = sqloledb; Data Source = 10.35.58.74; Initial Catalog = Tour; Min Pool Size = 1; Persist Security Info = False; User; password = fq15Ns @ E #(';
// "Provider = sqloledb; DataSource = 10.35.58.112; Initial Catalog = Tour; User Id = sa; Password = sa;"; fq15Ns @ E #(
// Access example: $ ADO = "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =". realpath ("mydb. mdb ");
$ DbConn-> open ($ ADO );
// If ($ dbConn) echo "create conn OK ";
$ Rs = new COM ("ADODB. RecordSet") or die ("An error occurred while creating RS ");
Echo"
";
// If ($ rs) echo "create rs OK ";
$ SQL = "Exec P_SelUser 9001111"; // "SELECT * FROM user_userInfo ";
$ Rs-> open ($ SQL, $ dbConn, 1, 1 );
While (! $ Rs-> eof ){
Echo $ rs-> fields ["Account"]-> value .":";
Echo $ rs-> fields ["PhoneID"]-> value;
Echo"
";
$ Rs-> movenext ();
}
$ Rs-> Close;
$ Rs = null;
$ DbConn-> Close;
$ DbConn = null;
?>
This indicates that if you want to use PHP to access MSSQL, you can call the COM component.