PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousccvs_voidCOMnext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

VIII. Funzioni di supporto COM per Windows

Introduzione

COM � una tecnologia che permette il riutilizzo del codice scritto in un qualsiasi linguaggio utilizzando una convenzione standard di chiamate e nascondendo dietro a delle API i dettagli di implementazione quali la macchina su cui il Componente � conservato e il file eseguibile che lo ospita. Si pu� pernsare a COM com a un meccanismo avanzato di Remote Procedure Call (RPC) con alcune basi di programmazione ad oggetti. COM separa l'implementazione dall'interfaccia.

COM incoraggia il versioning, la separazione tra interfaccia e implementazione e l'occultamento dei dettagli dell'implementazione, come la posizione dell'eseguibile e il linguaggio di programmazione.

Le funzioni COM sono disponibili solo sulle versioni Windows di PHP.

Costanti Predefinite

Queste costanti sono definite da questa estensione e sono disponibili solo se l'estensione � stata compilata nel PHP o se � stata caricata dinamicamente a runtime.

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)

Vedere Anche

Per maggiori informazioni su COM si leggano le o si guardi anche la documentazione di Don Box su

Sommario
COM -- classe COM
VARIANT -- classe VARIANT
com_addref --  Incrementa il contatore di referenze.
com_get --  Ricava il valore di una propriet� di un componente COM
com_invoke --  Chiama un metodo di un componente COM.
com_isenum -- Recupera un IEnumVariant
com_load_typelib -- Carica una Typelib
com_load --  Crea un nuovo riferimento a un componente COM
com_propget --  Ricava il valore di una propriet� di un componente COM
com_propput --  Assegna un valore a una propriet� di un oggetto COM
com_propset --  Assegna un valore a una propriet� di un oggetto COM
com_release --  Decrementa il contaotre di referenze.
com_set --  Assegna un valore a una propriet� di un oggetto COM
User Contributed Notes
Funzioni di supporto COM per Windows
add a note about notes
[email protected]
20-Dec-2000 07:45

As Jason ([email protected]) as demonstrated in his code example, there is a PHP database wrapper class library that can connect to ADO available called ADODB.

It is available from and supports many databases both on Windows and Linux.

Modelled after ADO, so it should be easy for Windows programmers to learn.

[email protected]
01-Apr-2001 12:37

I thought i'd share with those of you unfamiliar with one of the cool things
about developing php on win32 systems..



This is a good article, but i don't think the author hit the nail on the
head showing how useful this can be.

Now, checkout this article:


Notice how he describes 1) how to build a com object & 2) how to call and
use the com object from ASP.

In php, this is how you would call the same object:

<?
$instance = new COM("Checkyear.LeapYear");
$isleapyear = $instance->IsLeapYear($year);
$instance->close();
if($isleapyear) {
echo "The <b>$year</b> is a leap year";
}
else {
echo "The <b>$year</b> is not a leap year";
}
?>

I hope this helps someone.. you can contact me at [email protected] if
you would like to discuss this further.

[email protected]
28-Feb-2002 04:11

now in PHP >=4.0.6
programming in window can use the
ADO through the COM like this:
$dbconn=new COM ("ADODB.Connection") or die ("connection create fail");
$dbconn->Open("Provider=sqloledb;Data Source=ndht;Initial Catalog=printers;User Id=printers;Password=printers;");
$rec=new COM("ADODB.Recordset") or die ("create Recordset error");
while (!$rec->EOF)
{
echo $rec->fields["fieldname"]->value."
";
$rec->movenext();
}
$rec->close();
$dbconn->close();

but there's still a little question of working with the image field of mssql server.

[email protected]
07-Mar-2002 07:59

I thought this excel chart example could be useful.

Note the use of Excel.application vs Excel.sheet.

<pre>
<?php
print "Hi";
#Instantiate the spreadsheet component.
# $ex = new COM("Excel.sheet") or Die ("Did not connect");
$exapp = new COM("Excel.application") or Die ("Did not connect");

#Get the application name and version
print "Application name:{$ex->Application->value}
" ;
print "Loaded version: {$ex->Application->version}
";

$wkb=$exapp->Workbooks->add();
#$wkb = $ex->Application->ActiveWorkbook or Die ("Did not open workbook");
print "we opened workbook
";

$ex->Application->Visible = 1; #Make Excel visible.
print "we made excell visible
";

$sheets = $wkb->Worksheets(1); #Select the sheet
print "selected a sheet
";
$sheets->activate; #Activate it
print "activated sheet
";

#This is a new sheet
$sheets2 = $wkb->Worksheets->add(); #Add a sheet
print "added a new sheet
";
$sheets2->activate; #Activate it
print "activated sheet
";

$sheets2->name="Report Second page";

$sheets->name="Report First page";
print "We set a name to the sheet: $sheets->name
";

# fills a columns
$maxi=20;
for ($i=1;$i<$maxi;$i++) {
$cell = $sheets->Cells($i,5) ; #Select the cell (Row Column number)
$cell->activate; #Activate the cell
$cell->value = $i*$i; #Change it to 15000
}

$ch = $sheets->chartobjects->add(50, 40, 400, 100); # make a chartobject

$chartje = $ch->chart; # place a chart in the chart object
$chartje->activate; #activate chartobject
$chartje->ChartType=63;
$selected = $sheets->range("E1:E$maxi"); # set the data the chart uses
$chartje->setsourcedata($selected); # set the data the chart uses
print "set the data the chart uses
";

$file_name="D:/apache/Apache/htdocs/alm/tmp/final14.xls";
if (file_exists($file_name)) {unlink($file_name);}
#$ex->Application->ActiveWorkbook->SaveAs($file_name); # saves sheet as final.xls
$wkb->SaveAs($file_name); # saves sheet as final.xls
print "saved
";

#$ex->Application->ActiveWorkbook->Close("False");
$exapp->Quit();
unset($exapp);
?>

</pre>

Alex Madon

[email protected]
02-Apr-2002 11:01

An easy way to send e-mail using your default Outlook account:
<?
$objApp = new COM("Outlook.Application");
$myItem = $objApp->CreateItem(olMailItem);
$a=$myItem->Recipients->Add("[email protected]");
$myItem->Subject="Subject";
$myItem->Body="This is a Body Section now.....!";
$myItem->Display();
$myItem->Send();
?>

[email protected]
19-Apr-2002 11:34

an easy way to convert your file from .doc to .html

// starting word
$word = new COM("word.application") or die("Unable to instanciate Word");

// if you want see thw World interface the value must be '1' else '0'
$word->Visible = 1;

//doc file location
$word->Documents->Open("E:\\first.doc");

//html file location '8' mean HTML format
$word->Documents[1]->SaveAs("E:\\test_doc.html",8);

//closing word
$word->Quit();

//free the object from the memory
$word->Release();
$word = null;

[email protected]
28-Jun-2002 05:48

Complementing Alex's Excell Example, let's print the SpreadSheet to a PDF file using Acrobat Distiller:

$wkb->PrintOut(NULL, NULL, NULL, NULL, "Acrobat Distiller");

There you go!!!

nospam @ bol .com.br
21-Aug-2002 10:30

This is a sample to make a parametrized query using ADO via COM, this sample was used with Foxpro but will work's with most ADO complient databases. This was tested on IIS with WIN XP pro.

<?
// Create the main connection
$dbc = new COM("ADODB.Connection") or die ("connection create fail");
$dbc->Provider = "MSDASQL";
$dbc->Open("FoxDatabase");
// Creates a temporary record set
$RSet = new COM("ADODB.Recordset");
// Create one ADO command
$cm = new COM("ADODB.Command");
$cm->Activeconnection = $dbc;
// the ? inside values will be the parameters $par01 and $par02
$cm->CommandText = "Insert Into testtable ( mycharfield,mymemofield) VALUES (?,?)" ;
$cm->Prepared = TRUE;
// Creates and append 2 parameters
$par01 = $cm->CreateParameter('ppar01',129,1,50,'ABCDEFGHIKL');
$cm->Parameters->Append($par01);
$par02 = $cm->CreateParameter('ppar01',129,1,50,'123456789012346789');
$cm->Parameters->Append($par02);
// Call 10 times the exec comand to show 10 different queries
for ($i = 1; $i <= 10; $i++) {
$par01->Value = 'Count '.$i;
$par02->Value = 'Passing here'.$i.' times';
$RSet = $cm->Execute;
}
$RSet = $dbc->Execute("select * from testtable");
while (!$RSet->EOF){
echo $RSet->fields["mycharfield"]->value.' ' ;
echo $RSet->fields["mymemofield"]->value ;
echo '
';
$RSet->movenext();
}
$RSet->close;
$dbc->close;
$cm->close;
$RSet = null;
$dbc = null;
$cm = null;
?>

[email protected]
27-Aug-2002 10:31

When first writing applications instancing COM objects I always faced the problem of the instanced program not terminating correctly although everything else worked fine. In fact the process had to be killed manually by using the task manager. When experimenting a bit I was able to isolate the cause for this peculiar behaviour to be illustrated by the sample code below:

<?php

//Accessing arrays by retrieving elements via function ArrayName(Index) works, but certainly is neither efficient nor good style
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();

$book=$excel->Workbooks(1);
$sheet=$book->Worksheets(1);

$sheet->Name="Debug-Test";
$book->saveas("C:\\debug.xls");

$book->Close(false);
unset($sheet);
unset($book);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);

//Accessing arrays as usual (using the [] operator) works, buts leads to the application not being able to terminate by itself
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();

$excel->Workbooks[1]->Worksheets[1]->Name="Debug-Test";
$excel->Workbooks[1]->saveas("C:\\debug.xls");

$excel->Workbooks[1]->Close(false);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);

?>

The sample code performs the same action twice and each time the file is properly created. Yet for some mysterious reason the instanced excel process won't terminate once you've accessed an array the way most programmers (especially those who do a lot of oop in c++) do! Therefore until the PHP developers become aware of this problem we'll have to use the inefficient coding style illustrated in the first example.

add a note about notes
previousccvs_voidCOMnext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Sep 6 12:35:50 2002 CEST