]> granicus.if.org Git - php/commitdiff
- Make sure that COM and VARIANT resources are returned as resources
authorWez Furlong <wez@php.net>
Tue, 21 May 2002 18:58:11 +0000 (18:58 +0000)
committerWez Furlong <wez@php.net>
Tue, 21 May 2002 18:58:11 +0000 (18:58 +0000)
  rather than longs.
- Make the IDispatch implementation a bit more generic (and
  fix my mess of pointers).
- Add new com_message_pump() function that acts like an interruptible
  usleep() that processes COM calls/events.
- Add new com_print_typeinfo() function for "decompiling" the typeinfo
  for an interface into PHP script.  This is useful for generating a
  skeleton for use as an event sink.
- Add new com_event_sink() function for sinking events from COM
  objects.  Usage is like this:

<?php

class IEEventSinker {
var $terminated = false;

function ProgressChange($progress, $progressmax) {
echo "Download progress: $progress / $progressmax\n";
}
function DocumentComplete(&$dom, $url) {
echo "Document $url complete\n";
}
function OnQuit() {
echo "Quit!\n";
$this->terminated = true;
}
}

$ie = new COM("InternetExplorer.Application");

$sink =& new IEEventSinker();
com_event_sink($ie, $sink, "DWebBrowserEvents2");

$ie->Visible = true;
$ie->Navigate("http://www.php.net");

while(!$sink->terminated) {
com_message_pump(4000);
}
$ie = null;
?>

ext/com/COM.c
ext/com/com.h
ext/com/dispatch.c
ext/com/php_COM.h
ext/com/variant.h
ext/rpc/com/com_wrapper.c
ext/rpc/com/com_wrapper.h
ext/rpc/com/dispatch.c
ext/rpc/com/php_com.h
ext/rpc/com/variant.h

index 2e57eb1bd6ff23810413a228cbcea17fd1f1837f..01517a82bd70d3cadd4169351707e3fcdfcc179c 100644 (file)
@@ -57,6 +57,7 @@
 
 #include <iostream.h>
 #include <math.h>
+#include <ocidl.h>
 
 #include "php.h"
 #include "php_ini.h"
@@ -69,6 +70,8 @@ static int do_COM_offget(VARIANT *result, comval *array, pval *property, int cle
 static int do_COM_propget(VARIANT *var_result, comval *obj, pval *arg_property, int cleanup TSRMLS_DC);
 static void php_register_COM_class(TSRMLS_D);
 static void php_COM_init(int module_number TSRMLS_DC);
+static char *php_string_from_clsid(const CLSID *clsid TSRMLS_DC);
+static int com_enable_events(comval *obj, int enable);
 
 static int le_comval;
 static int codepage;
@@ -77,6 +80,9 @@ static int codepage;
 int resourcecounter = 1;
 #endif
 
+static unsigned char arg1and2_force_ref[] =
+                       { 2, BYREF_FORCE, BYREF_FORCE };
+
 function_entry COM_functions[] = {
        PHP_FE(com_load,                                NULL)
        PHP_FE(com_invoke,                              NULL)
@@ -86,6 +92,9 @@ function_entry COM_functions[] = {
        PHP_FE(com_propput,                             NULL)
        PHP_FE(com_load_typelib,                        NULL)
        PHP_FE(com_isenum,                              NULL)
+       PHP_FE(com_event_sink,                                                  arg1and2_force_ref)
+       PHP_FE(com_message_pump,                        NULL)
+       PHP_FE(com_print_typeinfo,                      NULL)
 
        PHP_FALIAS(com_get,         com_propget,        NULL)
        PHP_FALIAS(com_propset,     com_propput,        NULL)
@@ -260,7 +269,7 @@ PHPAPI HRESULT php_COM_set(comval *obj, IDispatch FAR* FAR* ppDisp, int cleanup
        C_REFCOUNT(obj) = 1;
        C_DISPATCH(obj) = pDisp;
        C_HASTLIB(obj) = SUCCEEDED(C_DISPATCH_VT(obj)->GetTypeInfo(C_DISPATCH(obj), 0, LANG_NEUTRAL, &C_TYPEINFO(obj)));
-
+       
        dispparams.rgvarg = NULL;
        dispparams.rgdispidNamedArgs = NULL;
        dispparams.cArgs = 0;
@@ -357,6 +366,10 @@ PHPAPI HRESULT php_COM_destruct(comval *obj TSRMLS_DC)
 {
        HRESULT hr = S_OK;
 
+       com_enable_events(obj, FALSE);
+       if (obj->sinkdispatch)
+               obj->sinkdispatch->lpVtbl->Release(obj->sinkdispatch);
+       
        if (C_ISREFD(obj)) {
                C_REFCOUNT(obj) = 1;
                hr = php_COM_release(obj TSRMLS_CC);
@@ -708,7 +721,7 @@ PHP_FUNCTION(com_load)
                }
        }
 
-       RETURN_LONG(zend_list_insert(obj, IS_COM));
+       RETURN_RESOURCE(zend_list_insert(obj, IS_COM));
 }
 /* }}} */
 
@@ -1003,6 +1016,403 @@ PHP_FUNCTION(com_addref)
 }
 /* }}} */
 
+/* {{{ proto bool com_message_pump([int timeoutms])
+   Process COM messages, sleeping for up to timeoutms milliseconds */
+PHP_FUNCTION(com_message_pump)
+{
+       long timeoutms = 0;
+       MSG msg;
+       DWORD result;
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &timeoutms) == FAILURE)
+               RETURN_FALSE;
+       
+       result = MsgWaitForMultipleObjects(0, NULL, FALSE, timeoutms, QS_ALLINPUT);
+
+       if (result == WAIT_OBJECT_0) {
+               while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
+                       TranslateMessage(&msg);
+                       DispatchMessage(&msg);
+               }
+               /* we processed messages */
+               RETVAL_TRUE;
+       } else {
+               /* we did not process messages (timed out) */
+               RETVAL_FALSE;
+       }
+}
+/* }}} */
+
+
+static int com_enable_events(comval *obj, int enable)
+{
+       if (obj->sinkdispatch) {
+               IConnectionPointContainer *cont;
+               IConnectionPoint *point;
+               
+               if (SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IConnectionPointContainer, (void**)&cont))) {
+                       
+                       if (SUCCEEDED(cont->lpVtbl->FindConnectionPoint(cont, &obj->sinkid, &point))) {
+
+                               if (enable) {
+                                       point->lpVtbl->Advise(point, (IUnknown*)obj->sinkdispatch, &obj->sinkcookie);
+                               } else {
+                                       point->lpVtbl->Unadvise(point, obj->sinkcookie);
+                               }
+                               point->lpVtbl->Release(point);
+                       }
+                       cont->lpVtbl->Release(cont);
+               }
+       }
+       return 0;
+}
+
+static const struct {
+       VARTYPE vt;
+       const char *name;
+} vt_names[] = {
+       { VT_NULL,              "VT_NULL" },
+       { VT_EMPTY,             "VT_EMPTY" },
+       { VT_UI1,               "VT_UI1" },
+       { VT_I2,                "VT_I2" },
+       { VT_I4,                "VT_I4" },
+       { VT_R4,                "VT_R4" },
+       { VT_R8,                "VT_R8" },
+       { VT_BOOL,              "VT_BOOL" },
+       { VT_ERROR,             "VT_ERROR" },
+       { VT_CY,                "VT_CY" },
+       { VT_DATE,              "VT_DATE" },
+       { VT_BSTR,              "VT_BSTR" },
+       { VT_DECIMAL,   "VT_DECIMAL" },
+       { VT_UNKNOWN,   "VT_UNKNOWN" },
+       { VT_DISPATCH,  "VT_DISPATCH" },
+       { VT_VARIANT,   "VT_VARIANT" },
+       { VT_I1,                "VT_I1" },
+       { VT_UI2,               "VT_UI2" },
+       { VT_UI4,               "VT_UI4" },
+       { VT_INT,               "VT_INT" },
+       { VT_UINT,              "VT_UINT" },
+       { VT_ARRAY,             "VT_ARRAY" },
+       { VT_BYREF,             "VT_BYREF" },
+       { VT_VOID,              "VT_VOID" },
+       { VT_PTR,               "VT_PTR" },
+       { 0, NULL }
+};
+
+static inline const char *vt_to_string(VARTYPE vt)
+{
+       int i;
+       for (i = 0; vt_names[i].name != NULL; i++) {
+               if (vt_names[i].vt == vt)
+                       return vt_names[i].name;
+       }
+       return "?";
+}
+
+static int process_typeinfo(ITypeInfo *typeinfo, HashTable *id_to_name, int printdef, GUID *guid TSRMLS_DC)
+{
+       TYPEATTR *attr;
+       FUNCDESC *func;
+       int i;
+       OLECHAR *olename;
+       char *ansiname = NULL;
+       unsigned int ansinamelen;
+       int ret = 0;
+
+       if (FAILED(typeinfo->lpVtbl->GetTypeAttr(typeinfo, &attr)))
+               return 0;
+
+       /* verify that it is suitable */
+       if (attr->typekind == TKIND_DISPATCH) {
+
+               if (guid)
+                       memcpy(guid, &attr->guid, sizeof(GUID));
+               
+               if (printdef) {
+                       char *guidstring;
+
+                       typeinfo->lpVtbl->GetDocumentation(typeinfo, MEMBERID_NIL, &olename, NULL, NULL, NULL);
+                       ansiname = php_OLECHAR_to_char(olename, &ansinamelen, codepage TSRMLS_CC);
+                       SysFreeString(olename);
+
+                       guidstring = php_string_from_clsid(&attr->guid TSRMLS_CC);
+                       php_printf("class %s { /* GUID=%s */\n", ansiname, guidstring);
+                       efree(guidstring);
+
+                       efree(ansiname);
+               }
+
+               if (id_to_name)
+                       zend_hash_init(id_to_name, 0, NULL, ZVAL_PTR_DTOR, 0);
+
+               /* So we've got the dispatch interface; lets list the event methods */
+               for (i = 0; i < attr->cFuncs; i++) {
+                       zval *tmp;
+
+                       if (FAILED(typeinfo->lpVtbl->GetFuncDesc(typeinfo, i, &func)))
+                               break;
+
+                       typeinfo->lpVtbl->GetDocumentation(typeinfo, func->memid, &olename, NULL, NULL, NULL);
+                       ansiname = php_OLECHAR_to_char(olename, &ansinamelen, codepage TSRMLS_CC);
+                       SysFreeString(olename);
+
+                       if (printdef) {
+                               int j;
+                               char *funcdesc;
+                               unsigned int funcdesclen, cnames = 0;
+                               BSTR *names;
+
+                               names = (BSTR*)emalloc((func->cParams + 1) * sizeof(BSTR));
+
+                               typeinfo->lpVtbl->GetNames(typeinfo, func->memid, names, func->cParams + 1, &cnames);
+
+                               /* first element is the function name */
+                               SysFreeString(names[0]);
+
+                               php_printf("\t/* DISPID=%d */\n\t", func->memid);
+
+                               if (func->elemdescFunc.tdesc.vt != VT_VOID) {
+                                       php_printf(" /* %s [%d] */ ",
+                                                       vt_to_string(func->elemdescFunc.tdesc.vt),
+                                                       func->elemdescFunc.tdesc.vt
+                                                       );
+                               }
+
+                               /* TODO: handle prop put and get */
+                               
+                               php_printf("function %s(\n", ansiname);
+
+                               for (j = 0; j < func->cParams; j++) {
+                                       ELEMDESC *elem = &func->lprgelemdescParam[j];
+
+                                       php_printf("\t\t/* %s [%d] ", vt_to_string(elem->tdesc.vt), elem->tdesc.vt);
+
+                                       if (elem->paramdesc.wParamFlags & PARAMFLAG_FIN)
+                                               php_printf("[in]");
+                                       if (elem->paramdesc.wParamFlags & PARAMFLAG_FIN)
+                                               php_printf("[out]");
+
+                                       if (elem->tdesc.vt == VT_PTR) {
+                                               /* what does it point to ? */
+                                               php_printf(" --> %s [%d] ",
+                                                               vt_to_string(elem->tdesc.lptdesc->vt),
+                                                               elem->tdesc.lptdesc->vt
+                                                               );
+                                       }
+
+                                       /* when we handle prop put and get, this will look nicer */
+                                       if (j+1 < (int)cnames) {
+                                               funcdesc = php_OLECHAR_to_char(names[j+1], &funcdesclen, codepage TSRMLS_CC);
+                                               SysFreeString(names[j+1]);
+                                       } else {
+                                               funcdesc = "???";
+                                       }
+
+                                       php_printf(" */ %s%s%c\n",
+                                                       elem->tdesc.vt == VT_PTR ? "&$" : "$",
+                                                       funcdesc,
+                                                       j == func->cParams - 1 ? ' ' : ','
+                                                       );
+
+                                       if (j+1 < (int)cnames)
+                                               efree(funcdesc);
+                               }
+
+                               php_printf("\t\t)\n\t{\n");
+
+                               typeinfo->lpVtbl->GetDocumentation(typeinfo, func->memid, NULL, &olename, NULL, NULL);
+                               if (olename) {
+                                       funcdesc = php_OLECHAR_to_char(olename, &funcdesclen, codepage TSRMLS_CC);
+                                       SysFreeString(olename);
+                                       php_printf("\t\t/* %s */\n", funcdesc);
+                                       efree(funcdesc);
+                               }
+
+                               php_printf("\t}\n");
+
+                               efree(names);
+                       }
+
+                       if (id_to_name) {
+                               zend_str_tolower(ansiname, ansinamelen);
+                               MAKE_STD_ZVAL(tmp);
+                               ZVAL_STRINGL(tmp, ansiname, ansinamelen, 0);
+                               zend_hash_index_update(id_to_name, func->memid, (void*)&tmp, sizeof(zval *), NULL);
+                       }
+
+                       typeinfo->lpVtbl->ReleaseFuncDesc(typeinfo, func);
+
+               }
+
+               if (printdef)
+                       php_printf("}\n");
+
+               ret = 1;
+       } else {
+               zend_error(E_WARNING, "Thats not a dispatchable interface!!\n");
+       }
+
+       typeinfo->lpVtbl->ReleaseTypeAttr(typeinfo, attr);
+
+       return ret;
+}
+
+static ITypeInfo *locate_typeinfo(char *typelibname, comval *obj, char *dispname, int sink TSRMLS_DC)
+{
+       ITypeInfo *typeinfo = NULL;
+       ITypeLib *typelib = NULL;
+       int gotguid = 0;
+       GUID iid;
+
+       if (obj) {
+               if (dispname == NULL && sink) {
+                       /* Looking for the default sink interface */
+                       IProvideClassInfo2 *pci2;
+                       IProvideClassInfo *pci;
+
+                       if (SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IProvideClassInfo2, (void**)&pci2))) {
+                               gotguid = SUCCEEDED(pci2->lpVtbl->GetGUID(pci2, GUIDKIND_DEFAULT_SOURCE_DISP_IID, &iid));
+                               pci2->lpVtbl->Release(pci2);
+                       }
+                       if (!gotguid && SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IProvideClassInfo, (void**)&pci))) {
+                               /* examine the available interfaces */
+                               /* TODO: write some code here */
+                               pci->lpVtbl->Release(pci);
+                       }
+               } else if (dispname) {
+                       unsigned int idx;
+
+                       /* get the library from the object; the rest will be dealt with later */
+                       C_TYPEINFO_VT(obj)->GetContainingTypeLib(C_TYPEINFO(obj), &typelib, &idx);      
+               }
+       } else if (typelibname) {
+               /* Fetch the typelibrary and use that to look things up */
+               typelib = php_COM_find_typelib(typelibname, CONST_CS TSRMLS_CC);
+       } else if (dispname) {
+               /* TODO: assume that the name is actually a progid */
+       }
+
+       if (!gotguid && dispname && typelib) {
+               unsigned short cfound;
+               MEMBERID memid;
+               OLECHAR *olename = php_char_to_OLECHAR(dispname, strlen(dispname), CP_ACP TSRMLS_CC);
+               
+               cfound = 1;
+               typelib->lpVtbl->FindName(typelib, olename, 0, &typeinfo, &memid, &cfound);
+
+               efree(olename);
+       } else if (gotguid) {
+               typelib->lpVtbl->GetTypeInfoOfGuid(typelib, &iid, &typeinfo);
+       }
+
+       if (typelib)
+               typelib->lpVtbl->Release(typelib);
+
+       return typeinfo;
+}
+
+/* {{{ proto bool com_print_typeinfo(mixed comobject | string typelib, string dispinterface, bool wantsink)
+   Print out a PHP class definition for a dispatchable interface */
+PHP_FUNCTION(com_print_typeinfo)
+{
+       zval *arg1;
+       char *ifacename;
+       char *typelibname = NULL;
+       int ifacelen;
+       zend_bool wantsink;
+       comval *obj = NULL;
+       ITypeInfo *typeinfo;
+       
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zsb", &arg1, &ifacename,
+                               &ifacelen, &wantsink)) {
+               RETURN_FALSE;
+       }
+
+       if (Z_TYPE_P(arg1) == IS_OBJECT && (Z_OBJCE_P(arg1) == &COM_class_entry || !strcmp(Z_OBJCE_P(arg1)->name, "COM"))) {
+               zval **tmp;
+               zend_hash_index_find(Z_OBJPROP_P(arg1), 0, (void**)&tmp);
+               ZEND_FETCH_RESOURCE(obj, comval*, tmp, -1, "comval", IS_COM);
+       } else if (Z_TYPE_P(arg1) == IS_RESOURCE) {
+               ZEND_FETCH_RESOURCE(obj, comval*, &arg1, -1, "comval", IS_COM);
+       } else {
+               convert_to_string_ex(&arg1);
+               typelibname = Z_STRVAL_P(arg1);
+       }
+
+       typeinfo = locate_typeinfo(typelibname, obj, ifacename, wantsink TSRMLS_CC);
+       if (typeinfo) {
+               process_typeinfo(typeinfo, NULL, 1, NULL TSRMLS_CC);
+               typeinfo->lpVtbl->Release(typeinfo);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+/* }}} */
+
+
+/* {{{ proto bool com_event_sink(mixed comobject, object sinkobject [, mixed sinkinterface])
+   Connect events from a COM object to a PHP object */
+PHP_FUNCTION(com_event_sink)
+{
+       zval *object, *sinkobject, *sink=NULL;
+       char *dispname = NULL, *typelibname = NULL;
+       zend_bool gotguid = 0;
+       comval *obj;
+       ITypeInfo *typeinfo = NULL;
+
+       RETVAL_FALSE;
+       
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &object, &sinkobject, &sink)) {
+               RETURN_FALSE;
+       }
+
+       if (Z_TYPE_P(object) == IS_OBJECT && (Z_OBJCE_P(object) == &COM_class_entry || !strcmp(Z_OBJCE_P(object)->name, "COM"))) {
+               zval **tmp;
+               zend_hash_index_find(Z_OBJPROP_P(object), 0, (void**)&tmp);
+               ZEND_FETCH_RESOURCE(obj, comval*, tmp, -1, "comval", IS_COM);
+       } else {
+               ZEND_FETCH_RESOURCE(obj, comval*, &object, -1, "comval", IS_COM);
+       }
+
+       if (sink && Z_TYPE_P(sink) == IS_ARRAY) {
+               /* 0 => typelibname, 1 => dispname */
+               zval **tmp;
+
+               if (zend_hash_index_find(Z_ARRVAL_P(sink), 0, (void**)&tmp) == SUCCESS)
+                       typelibname = Z_STRVAL_PP(tmp);
+               if (zend_hash_index_find(Z_ARRVAL_P(sink), 1, (void**)&tmp) == SUCCESS)
+                       dispname = Z_STRVAL_PP(tmp);
+       } else if (sink != NULL) {
+               convert_to_string_ex(&sink);
+               dispname = Z_STRVAL_P(sink);
+       }
+       
+       typeinfo = locate_typeinfo(typelibname, obj, dispname, 1 TSRMLS_CC);
+
+       if (typeinfo) {
+               HashTable *id_to_name;
+               
+               ALLOC_HASHTABLE(id_to_name);
+               
+               if (process_typeinfo(typeinfo, id_to_name, 0, &obj->sinkid TSRMLS_CC)) {
+
+                       /* Create the COM wrapper for this sink */
+                       obj->sinkdispatch = php_COM_export_as_sink(sinkobject, &obj->sinkid, id_to_name TSRMLS_CC);
+
+                       /* Now hook it up to the source */
+                       com_enable_events(obj, TRUE);
+                       RETVAL_TRUE;
+
+               } else {
+                       FREE_HASHTABLE(id_to_name);
+               }
+       }
+       
+       if (typeinfo)
+               typeinfo->lpVtbl->Release(typeinfo);
+       
+}
+/* }}} */
 
 static int do_COM_offget(VARIANT *result, comval *array, pval *property, int cleanup TSRMLS_DC)
 {
@@ -1539,6 +1949,10 @@ static ITypeLib *php_COM_find_typelib(char *search_string, int mode TSRMLS_DC)
         */
 
        search_string = php_strtok_r(search_string, ",", &strtok_buf);
+
+       if (search_string == NULL)
+               return NULL;
+       
        major = php_strtok_r(NULL, ",", &strtok_buf);
        minor = php_strtok_r(NULL, ",", &strtok_buf);
 
@@ -1767,7 +2181,6 @@ PHPAPI int php_COM_load_typelib(ITypeLib *TypeLib, int mode TSRMLS_DC)
        return SUCCESS;
 }
 
-
 /* {{{ proto bool com_isenum(object com_module)
    Grabs an IEnumVariant */
 PHP_FUNCTION(com_isenum)
index 2930f86fd9285d7895546fa7dc514e6414d3413b..5ec791dd43d19e297070e051323fa21c9d959b82 100644 (file)
@@ -19,6 +19,9 @@ typedef struct comval_ {
                ITypeInfo *typeinfo;
                IEnumVARIANT *enumvariant;
        } i;
+       IDispatch *sinkdispatch;
+       GUID sinkid;
+       DWORD sinkcookie;
 } comval;
 
 END_EXTERN_C()
@@ -37,7 +40,7 @@ END_EXTERN_C()
                        if (handle == NULL) {                                                                                                   \
                                MAKE_STD_ZVAL(handle);                                                                                          \
                        }                                                                                                                                               \
-                       ZVAL_LONG(handle, zend_list_insert((o), IS_COM));                                               \
+                       ZVAL_RESOURCE(handle, zend_list_insert((o), IS_COM));                                           \
                                                                                                                                                                        \
                        zval_copy_ctor(handle);                                                                                                 \
                        zend_hash_index_update(properties, 0, &handle, sizeof(zval *), NULL);   \
@@ -48,7 +51,7 @@ END_EXTERN_C()
 #define RETURN_COM(o)  RETVAL_COM(o)                                                                                           \
                                                return;
 
-#define ALLOC_COM(z)   (z) = (comval *) emalloc(sizeof(comval));                                       \
+#define ALLOC_COM(z)   (z) = (comval *) ecalloc(1, sizeof(comval));                                    \
                                                C_REFCOUNT(z) = 0;
 
 #define FREE_COM(z)            php_COM_destruct(z TSRMLS_CC);
index 802c4f871ee4cbeea589db857d61ec63a907b98f..8d775a86624c8fc9acdd06cff844fabf4f6fd9b2 100644 (file)
@@ -45,9 +45,11 @@ typedef struct {
        zval *object;                   /* the object exported */
        LONG refcount;                  /* COM reference count */
 
-       HashTable dispid_to_name;       /* keep track of dispid -> name mappings */
-       HashTable name_to_dispid;       /* keep track of name -> dispid mappings */
+       HashTable *dispid_to_name;      /* keep track of dispid -> name mappings */
+       HashTable *name_to_dispid;      /* keep track of name -> dispid mappings */
 
+       GUID sinkid;    /* iid that we "implement" for event sinking */
+       
        int id;
 } php_dispatchex;
 
@@ -101,7 +103,8 @@ static HRESULT STDMETHODCALLTYPE disp_queryinterface(
 
        if (IsEqualGUID(&IID_IUnknown, riid) ||
                        IsEqualGUID(&IID_IDispatch, riid) ||
-                       IsEqualGUID(&IID_IDispatchEx, riid)) {
+                       IsEqualGUID(&IID_IDispatchEx, riid) ||
+                       IsEqualGUID(&disp->sinkid, riid)) {
                *ppvObject = This;
                InterlockedIncrement(&disp->refcount);
                return S_OK;
@@ -172,16 +175,16 @@ static HRESULT STDMETHODCALLTYPE disp_getidsofnames(
        for (i = 0; i < cNames; i++) {
                char *name;
                unsigned int namelen;
-               zval *tmp;
+               zval **tmp;
                
                name = php_OLECHAR_to_char(rgszNames[i], &namelen, CP_ACP TSRMLS_CC);
                
                /* Lookup the name in the hash */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == FAILURE) {
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == FAILURE) {
                        ret = DISP_E_UNKNOWNNAME;
                        rgDispId[i] = 0;
                } else {
-                       rgDispId[i] = Z_LVAL_P(tmp);
+                       rgDispId[i] = Z_LVAL_PP(tmp);
                }
 
                efree(name);
@@ -216,15 +219,15 @@ static HRESULT STDMETHODCALLTYPE disp_getdispid(
        HRESULT ret = DISP_E_UNKNOWNNAME;
        char *name;
        unsigned int namelen;
-       zval *tmp;
+       zval **tmp;
        TSRMLS_FETCH();
        FETCH_DISP("GetDispID");
 
        name = php_OLECHAR_to_char(bstrName, &namelen, CP_ACP TSRMLS_CC);
 
        /* Lookup the name in the hash */
-       if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS) {
-               *pid = Z_LVAL_P(tmp);
+       if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS) {
+               *pid = Z_LVAL_PP(tmp);
                ret = S_OK;
        }
 
@@ -243,69 +246,96 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
        /* [out] */ EXCEPINFO *pei,
        /* [unique][in] */ IServiceProvider *pspCaller)
 {
-       zval *name;
+       zval **name;
        UINT i;
        int codepage = CP_ACP;
-       zval **retval = NULL;
+       zval *retval = NULL;
        zval ***params = NULL;
+       HRESULT ret = DISP_E_MEMBERNOTFOUND;
        TSRMLS_FETCH();
        FETCH_DISP("InvokeEx");
 
-       if (SUCCESS == zend_hash_index_find(&disp->dispid_to_name, id, (void**)&name)) {
+       if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
                /* TODO: add support for overloaded objects */
 
-               trace("-- Invoke: %d %s\n", id, Z_STRVAL_P(name));
+               trace("-- Invoke: %d %20s flags=%08x args=%d\n", id, Z_STRVAL_PP(name), wFlags, pdp->cArgs);
                
                /* convert args into zvals.
                 * Args are in reverse order */
                params = (zval ***)emalloc(sizeof(zval **) * pdp->cArgs);
                for (i = 0; i < pdp->cArgs; i++) {
-                       ALLOC_INIT_ZVAL(*params[i]);
+                       VARIANT *arg;
+                       zval *zarg;
+                       
+                       arg = &pdp->rgvarg[ pdp->cArgs - 1 - i];
+
+                       trace("alloc zval for arg %d VT=%08x\n", i, V_VT(arg));
 
-                       if (V_VT(&pdp->rgvarg[pdp->cArgs-i]) == VT_DISPATCH) {
-                               php_COM_object_from_dispatch(V_DISPATCH(&pdp->rgvarg[pdp->cArgs-i]), *params[i] TSRMLS_CC);
+                       ALLOC_INIT_ZVAL(zarg);
+                       
+                       if (V_VT(arg) == VT_DISPATCH) {
+                               trace("arg %d is dispatchable\n", i);
+                               if (NULL == php_COM_object_from_dispatch(V_DISPATCH(arg), zarg TSRMLS_CC)) {
+                                       trace("failed to convert arg %d to zval\n", i);
+                                       ZVAL_NULL(zarg);
+                               }
                        } else {
-                               ZVAL_VARIANT(*params[i], &pdp->rgvarg[pdp->cArgs-i]);
+                               if (FAILURE == php_variant_to_pval(arg, zarg, codepage TSRMLS_CC)) {
+                                       trace("failed to convert arg %d to zval\n", i);
+                                       ZVAL_NULL(zarg);
+                               }
                        }
+                       
+                       params[i] = &zarg;
                }
+
+               trace("arguments processed, prepare to do some work\n");        
                
                if (wFlags & DISPATCH_PROPERTYGET) {
-                       zend_hash_find(Z_OBJPROP_P(disp->object), Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&retval);
+                       trace("trying to get a property\n");
+                       zend_hash_find(Z_OBJPROP_P(disp->object), Z_STRVAL_PP(name), Z_STRLEN_PP(name)+1, (void**)&retval);
                } else if (wFlags & DISPATCH_PROPERTYPUT) {
-                       add_property_zval(disp->object, Z_STRVAL_P(name), *params[0]);
+                       trace("trying to set a property\n");
+                       add_property_zval(disp->object, Z_STRVAL_PP(name), *params[0]);
                } else if (wFlags & DISPATCH_METHOD) {
-                       
-                       call_user_function_ex(EG(function_table), &disp->object, name,
-                                       retval, pdp->cArgs, params, 1, NULL TSRMLS_CC);
+                       trace("Trying to call user function\n");
+                       if (SUCCESS == call_user_function_ex(EG(function_table), &disp->object, *name,
+                                       &retval, pdp->cArgs, params, 1, NULL TSRMLS_CC)) {
+                               ret = S_OK;
+                       } else {
+                               ret = DISP_E_EXCEPTION;
+                       }
+               } else {
+                       trace("Don't know how to handle this invocation %08x\n", wFlags);
                }
        
                /* release arguments */
                for (i = 0; i < pdp->cArgs; i++)
                        zval_ptr_dtor(params[i]);
+               efree(params);
                
                /* return value */
                if (retval) {
                        if (pvarRes) {
-                               if (Z_TYPE_PP(retval) == IS_OBJECT) {
+                               if (Z_TYPE_P(retval) == IS_OBJECT) {
                                        /* export the object using a dispatch like ourselves */
                                        VariantInit(pvarRes);
                                        V_VT(pvarRes) = VT_DISPATCH;
-                                       V_DISPATCH(pvarRes) = php_COM_export_object(*retval TSRMLS_CC);
+                                       V_DISPATCH(pvarRes) = php_COM_export_object(retval TSRMLS_CC);
                                } else {
-                                       php_pval_to_variant(*retval, pvarRes, codepage TSRMLS_CC);
+                                       php_pval_to_variant(retval, pvarRes, codepage TSRMLS_CC);
                                }
                        }
-                       zval_ptr_dtor(retval);
+                       zval_ptr_dtor(&retval);
                } else if (pvarRes) {
                        VariantInit(pvarRes);
                }
                
-               return S_OK;
        } else {
                trace("InvokeEx: I don't support DISPID=%d\n", id);
        }
 
-       return DISP_E_MEMBERNOTFOUND;
+       return ret;
 }
 
 static HRESULT STDMETHODCALLTYPE disp_deletememberbyname( 
@@ -347,7 +377,7 @@ static HRESULT STDMETHODCALLTYPE disp_getmembername(
        TSRMLS_FETCH();
        FETCH_DISP("GetMemberName");
 
-       if (SUCCESS == zend_hash_index_find(&disp->dispid_to_name, id, (void**)&name)) {
+       if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
                OLECHAR *olestr = php_char_to_OLECHAR(Z_STRVAL_P(name), Z_STRLEN_P(name), CP_ACP TSRMLS_CC);
                *pbstrName = SysAllocString(olestr);
                efree(olestr);
@@ -366,10 +396,10 @@ static HRESULT STDMETHODCALLTYPE disp_getnextdispid(
        ulong next = id+1;
        FETCH_DISP("GetNextDispID");
 
-       while(!zend_hash_index_exists(&disp->dispid_to_name, next))
+       while(!zend_hash_index_exists(disp->dispid_to_name, next))
                next++;
 
-       if (zend_hash_index_exists(&disp->dispid_to_name, next)) {
+       if (zend_hash_index_exists(disp->dispid_to_name, next)) {
                *pid = next;
                return S_OK;
        }
@@ -407,7 +437,7 @@ static struct IDispatchExVtbl php_dispatch_vtbl = {
 
 /* enumerate functions and properties of the object and assign
  * dispatch ids */
-static void update_dispids(php_dispatchex *disp TSRMLS_DC)
+static void generate_dispids(php_dispatchex *disp TSRMLS_DC)
 {
        HashPosition pos;
        char *name = NULL;
@@ -416,6 +446,13 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
        int keytype;
        ulong pid;
 
+       if (disp->dispid_to_name == NULL) {
+               ALLOC_HASHTABLE(disp->dispid_to_name);
+               ALLOC_HASHTABLE(disp->name_to_dispid);
+               zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
+               zend_hash_init(disp->dispid_to_name, 0, NULL, ZVAL_PTR_DTOR, 0);
+       }
+
        /* properties */
        zend_hash_internal_pointer_reset_ex(Z_OBJPROP_PP(&disp->object), &pos);
        while (HASH_KEY_NON_EXISTANT != (keytype =
@@ -430,17 +467,17 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
                zend_hash_move_forward_ex(Z_OBJPROP_PP(&disp->object), &pos);
                
                /* Find the existing id */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
                        continue;
                
                /* add the mappings */
                MAKE_STD_ZVAL(tmp);
                ZVAL_STRINGL(tmp, name, namelen, 1);
-               zend_hash_index_update(&disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
 
                MAKE_STD_ZVAL(tmp);
                ZVAL_LONG(tmp, pid);
-               zend_hash_update(&disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_update(disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
 
        }
        
@@ -459,23 +496,25 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
                zend_hash_move_forward_ex(Z_OBJPROP_PP(&disp->object), &pos);
                
                /* Find the existing id */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
                        continue;
                
                /* add the mappings */
                MAKE_STD_ZVAL(tmp);
                ZVAL_STRINGL(tmp, name, namelen, 1);
-               zend_hash_index_update(&disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
 
                MAKE_STD_ZVAL(tmp);
                ZVAL_LONG(tmp, pid);
-               zend_hash_update(&disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_update(disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
        }
 }
 
 static php_dispatchex *disp_constructor(zval *object TSRMLS_DC)
 {
        php_dispatchex *disp = (php_dispatchex*)CoTaskMemAlloc(sizeof(php_dispatchex));
+
+       trace("constructing a COM proxy\n");
        
        if (disp == NULL)
                return NULL;
@@ -486,15 +525,11 @@ static php_dispatchex *disp_constructor(zval *object TSRMLS_DC)
        disp->lpVtbl = &php_dispatch_vtbl;
        disp->refcount = 1;
 
-       zend_hash_init(&disp->dispid_to_name, 0, NULL, NULL, TRUE);
-       zend_hash_init(&disp->name_to_dispid, 0, NULL, NULL, TRUE);
 
        if (object)
                ZVAL_ADDREF(object);
        disp->object = object;
 
-       update_dispids(disp TSRMLS_CC);
-
        disp->id = zend_list_insert(disp, le_dispatch);
        
        return disp;
@@ -511,9 +546,11 @@ static void disp_destructor(php_dispatchex *disp)
        if (disp->refcount > 0)
                CoDisconnectObject((IUnknown*)disp, 0);
 
-       zend_hash_destroy(&disp->dispid_to_name);
-       zend_hash_destroy(&disp->name_to_dispid);
-
+       zend_hash_destroy(disp->dispid_to_name);
+       zend_hash_destroy(disp->name_to_dispid);
+       FREE_HASHTABLE(disp->dispid_to_name);
+       FREE_HASHTABLE(disp->name_to_dispid);
+                       
        if (disp->object)
                zval_ptr_dtor(&disp->object);
 
@@ -521,13 +558,51 @@ static void disp_destructor(php_dispatchex *disp)
        CoTaskMemFree(disp);
 }
 
+PHPAPI IDispatch *php_COM_export_as_sink(zval *val, GUID *sinkid, HashTable *id_to_name TSRMLS_DC)
+{
+       php_dispatchex *disp = disp_constructor(val TSRMLS_CC);
+       HashPosition pos;
+       char *name = NULL;
+       zval *tmp, **ntmp;
+       int namelen;
+       int keytype;
+       ulong pid;
+
+       disp->dispid_to_name = id_to_name;
+
+       memcpy(&disp->sinkid, sinkid, sizeof(disp->sinkid));
+       
+       /* build up the reverse mapping */
+       ALLOC_HASHTABLE(disp->name_to_dispid);
+       zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
+       
+       zend_hash_internal_pointer_reset_ex(id_to_name, &pos);
+       while (HASH_KEY_NON_EXISTANT != (keytype =
+                               zend_hash_get_current_key_ex(id_to_name, &name, &namelen, &pid, 0, &pos))) {
+
+               if (keytype == HASH_KEY_IS_LONG) {
+
+                       zend_hash_get_current_data_ex(id_to_name, (void**)&ntmp, &pos);
+                       
+                       MAKE_STD_ZVAL(tmp);
+                       ZVAL_LONG(tmp, pid);
+                       zend_hash_update(disp->name_to_dispid, Z_STRVAL_PP(ntmp), Z_STRLEN_PP(ntmp)+1, (void*)&tmp, sizeof(zval *), NULL);
+               }
+
+               zend_hash_move_forward_ex(id_to_name, &pos);
+       }
+
+       return (IDispatch*)disp;
+}
 
 PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC)
 {
+       php_dispatchex *disp = NULL;
+
        if (Z_TYPE_P(val) != IS_OBJECT)
                return NULL;
 
-       if (Z_OBJCE_P(val) == &COM_class_entry) {
+       if (Z_OBJCE_P(val) == &COM_class_entry || !strcmp(Z_OBJCE_P(val)->name, "COM")) {
                /* pass back it's IDispatch directly */
                zval **tmp;
                comval *obj;
@@ -541,7 +616,11 @@ PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC)
                C_DISPATCH(obj)->lpVtbl->AddRef(C_DISPATCH(obj));
                return C_DISPATCH(obj);
        }
-       return (IDispatch*)disp_constructor(val TSRMLS_CC);
+
+       disp = disp_constructor(val TSRMLS_CC);
+       generate_dispids(disp TSRMLS_CC);
+
+       return (IDispatch*)disp;
 }
 
 
index 396df9c93c6126de57196f44b113164513b6cc0d..d26441f6a3eab2e47303db7831e3024d3b6bfdd3 100644 (file)
@@ -18,6 +18,9 @@ PHP_FUNCTION(com_propget);
 PHP_FUNCTION(com_propput);
 PHP_FUNCTION(com_load_typelib);
 PHP_FUNCTION(com_isenum);
+PHP_FUNCTION(com_event_sink);
+PHP_FUNCTION(com_message_pump);
+PHP_FUNCTION(com_print_typeinfo);
 
 PHPAPI HRESULT php_COM_invoke(comval *obj, DISPID dispIdMember, WORD wFlags, DISPPARAMS FAR*  pDispParams, VARIANT FAR* pVarResult, char **ErrString TSRMLS_DC);
 PHPAPI HRESULT php_COM_get_ids_of_names(comval *obj, OLECHAR FAR* FAR* rgszNames, DISPID FAR* rgDispId TSRMLS_DC);
@@ -36,6 +39,7 @@ PHPAPI int php_COM_load_typelib(ITypeLib *TypeLib, int mode TSRMLS_DC);
 
 /* dispatch.c */
 PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC);
+PHPAPI IDispatch *php_COM_export_as_sink(zval *val, GUID *sinkid, HashTable *id_to_name TSRMLS_DC);
 int php_COM_dispatch_init(int module_number TSRMLS_DC);
 
 zend_module_entry COM_module_entry;
index 3fd11f32e47135265eee6947ebafadf7bfce5597..31ea342d66bb3fd78802eb8c1b3602a784c6ccf9 100644 (file)
@@ -16,7 +16,7 @@
                                                                        comval *obj;                                                                            \
                                                                        ALLOC_COM(obj);                                                                         \
                                                                        php_COM_set(obj, &V_DISPATCH(v), TRUE TSRMLS_CC);       \
-                                                                       ZVAL_LONG((z), zend_list_insert(obj, IS_COM));          \
+                                                                       ZVAL_RESOURCE((z), zend_list_insert(obj, IS_COM));              \
                                                                } else {                                                                                                \
                                                                        php_variant_to_pval((v), (z), codepage TSRMLS_CC);      \
                                                                        FREE_VARIANT(v);                                                                        \
index 2e57eb1bd6ff23810413a228cbcea17fd1f1837f..01517a82bd70d3cadd4169351707e3fcdfcc179c 100644 (file)
@@ -57,6 +57,7 @@
 
 #include <iostream.h>
 #include <math.h>
+#include <ocidl.h>
 
 #include "php.h"
 #include "php_ini.h"
@@ -69,6 +70,8 @@ static int do_COM_offget(VARIANT *result, comval *array, pval *property, int cle
 static int do_COM_propget(VARIANT *var_result, comval *obj, pval *arg_property, int cleanup TSRMLS_DC);
 static void php_register_COM_class(TSRMLS_D);
 static void php_COM_init(int module_number TSRMLS_DC);
+static char *php_string_from_clsid(const CLSID *clsid TSRMLS_DC);
+static int com_enable_events(comval *obj, int enable);
 
 static int le_comval;
 static int codepage;
@@ -77,6 +80,9 @@ static int codepage;
 int resourcecounter = 1;
 #endif
 
+static unsigned char arg1and2_force_ref[] =
+                       { 2, BYREF_FORCE, BYREF_FORCE };
+
 function_entry COM_functions[] = {
        PHP_FE(com_load,                                NULL)
        PHP_FE(com_invoke,                              NULL)
@@ -86,6 +92,9 @@ function_entry COM_functions[] = {
        PHP_FE(com_propput,                             NULL)
        PHP_FE(com_load_typelib,                        NULL)
        PHP_FE(com_isenum,                              NULL)
+       PHP_FE(com_event_sink,                                                  arg1and2_force_ref)
+       PHP_FE(com_message_pump,                        NULL)
+       PHP_FE(com_print_typeinfo,                      NULL)
 
        PHP_FALIAS(com_get,         com_propget,        NULL)
        PHP_FALIAS(com_propset,     com_propput,        NULL)
@@ -260,7 +269,7 @@ PHPAPI HRESULT php_COM_set(comval *obj, IDispatch FAR* FAR* ppDisp, int cleanup
        C_REFCOUNT(obj) = 1;
        C_DISPATCH(obj) = pDisp;
        C_HASTLIB(obj) = SUCCEEDED(C_DISPATCH_VT(obj)->GetTypeInfo(C_DISPATCH(obj), 0, LANG_NEUTRAL, &C_TYPEINFO(obj)));
-
+       
        dispparams.rgvarg = NULL;
        dispparams.rgdispidNamedArgs = NULL;
        dispparams.cArgs = 0;
@@ -357,6 +366,10 @@ PHPAPI HRESULT php_COM_destruct(comval *obj TSRMLS_DC)
 {
        HRESULT hr = S_OK;
 
+       com_enable_events(obj, FALSE);
+       if (obj->sinkdispatch)
+               obj->sinkdispatch->lpVtbl->Release(obj->sinkdispatch);
+       
        if (C_ISREFD(obj)) {
                C_REFCOUNT(obj) = 1;
                hr = php_COM_release(obj TSRMLS_CC);
@@ -708,7 +721,7 @@ PHP_FUNCTION(com_load)
                }
        }
 
-       RETURN_LONG(zend_list_insert(obj, IS_COM));
+       RETURN_RESOURCE(zend_list_insert(obj, IS_COM));
 }
 /* }}} */
 
@@ -1003,6 +1016,403 @@ PHP_FUNCTION(com_addref)
 }
 /* }}} */
 
+/* {{{ proto bool com_message_pump([int timeoutms])
+   Process COM messages, sleeping for up to timeoutms milliseconds */
+PHP_FUNCTION(com_message_pump)
+{
+       long timeoutms = 0;
+       MSG msg;
+       DWORD result;
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &timeoutms) == FAILURE)
+               RETURN_FALSE;
+       
+       result = MsgWaitForMultipleObjects(0, NULL, FALSE, timeoutms, QS_ALLINPUT);
+
+       if (result == WAIT_OBJECT_0) {
+               while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
+                       TranslateMessage(&msg);
+                       DispatchMessage(&msg);
+               }
+               /* we processed messages */
+               RETVAL_TRUE;
+       } else {
+               /* we did not process messages (timed out) */
+               RETVAL_FALSE;
+       }
+}
+/* }}} */
+
+
+static int com_enable_events(comval *obj, int enable)
+{
+       if (obj->sinkdispatch) {
+               IConnectionPointContainer *cont;
+               IConnectionPoint *point;
+               
+               if (SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IConnectionPointContainer, (void**)&cont))) {
+                       
+                       if (SUCCEEDED(cont->lpVtbl->FindConnectionPoint(cont, &obj->sinkid, &point))) {
+
+                               if (enable) {
+                                       point->lpVtbl->Advise(point, (IUnknown*)obj->sinkdispatch, &obj->sinkcookie);
+                               } else {
+                                       point->lpVtbl->Unadvise(point, obj->sinkcookie);
+                               }
+                               point->lpVtbl->Release(point);
+                       }
+                       cont->lpVtbl->Release(cont);
+               }
+       }
+       return 0;
+}
+
+static const struct {
+       VARTYPE vt;
+       const char *name;
+} vt_names[] = {
+       { VT_NULL,              "VT_NULL" },
+       { VT_EMPTY,             "VT_EMPTY" },
+       { VT_UI1,               "VT_UI1" },
+       { VT_I2,                "VT_I2" },
+       { VT_I4,                "VT_I4" },
+       { VT_R4,                "VT_R4" },
+       { VT_R8,                "VT_R8" },
+       { VT_BOOL,              "VT_BOOL" },
+       { VT_ERROR,             "VT_ERROR" },
+       { VT_CY,                "VT_CY" },
+       { VT_DATE,              "VT_DATE" },
+       { VT_BSTR,              "VT_BSTR" },
+       { VT_DECIMAL,   "VT_DECIMAL" },
+       { VT_UNKNOWN,   "VT_UNKNOWN" },
+       { VT_DISPATCH,  "VT_DISPATCH" },
+       { VT_VARIANT,   "VT_VARIANT" },
+       { VT_I1,                "VT_I1" },
+       { VT_UI2,               "VT_UI2" },
+       { VT_UI4,               "VT_UI4" },
+       { VT_INT,               "VT_INT" },
+       { VT_UINT,              "VT_UINT" },
+       { VT_ARRAY,             "VT_ARRAY" },
+       { VT_BYREF,             "VT_BYREF" },
+       { VT_VOID,              "VT_VOID" },
+       { VT_PTR,               "VT_PTR" },
+       { 0, NULL }
+};
+
+static inline const char *vt_to_string(VARTYPE vt)
+{
+       int i;
+       for (i = 0; vt_names[i].name != NULL; i++) {
+               if (vt_names[i].vt == vt)
+                       return vt_names[i].name;
+       }
+       return "?";
+}
+
+static int process_typeinfo(ITypeInfo *typeinfo, HashTable *id_to_name, int printdef, GUID *guid TSRMLS_DC)
+{
+       TYPEATTR *attr;
+       FUNCDESC *func;
+       int i;
+       OLECHAR *olename;
+       char *ansiname = NULL;
+       unsigned int ansinamelen;
+       int ret = 0;
+
+       if (FAILED(typeinfo->lpVtbl->GetTypeAttr(typeinfo, &attr)))
+               return 0;
+
+       /* verify that it is suitable */
+       if (attr->typekind == TKIND_DISPATCH) {
+
+               if (guid)
+                       memcpy(guid, &attr->guid, sizeof(GUID));
+               
+               if (printdef) {
+                       char *guidstring;
+
+                       typeinfo->lpVtbl->GetDocumentation(typeinfo, MEMBERID_NIL, &olename, NULL, NULL, NULL);
+                       ansiname = php_OLECHAR_to_char(olename, &ansinamelen, codepage TSRMLS_CC);
+                       SysFreeString(olename);
+
+                       guidstring = php_string_from_clsid(&attr->guid TSRMLS_CC);
+                       php_printf("class %s { /* GUID=%s */\n", ansiname, guidstring);
+                       efree(guidstring);
+
+                       efree(ansiname);
+               }
+
+               if (id_to_name)
+                       zend_hash_init(id_to_name, 0, NULL, ZVAL_PTR_DTOR, 0);
+
+               /* So we've got the dispatch interface; lets list the event methods */
+               for (i = 0; i < attr->cFuncs; i++) {
+                       zval *tmp;
+
+                       if (FAILED(typeinfo->lpVtbl->GetFuncDesc(typeinfo, i, &func)))
+                               break;
+
+                       typeinfo->lpVtbl->GetDocumentation(typeinfo, func->memid, &olename, NULL, NULL, NULL);
+                       ansiname = php_OLECHAR_to_char(olename, &ansinamelen, codepage TSRMLS_CC);
+                       SysFreeString(olename);
+
+                       if (printdef) {
+                               int j;
+                               char *funcdesc;
+                               unsigned int funcdesclen, cnames = 0;
+                               BSTR *names;
+
+                               names = (BSTR*)emalloc((func->cParams + 1) * sizeof(BSTR));
+
+                               typeinfo->lpVtbl->GetNames(typeinfo, func->memid, names, func->cParams + 1, &cnames);
+
+                               /* first element is the function name */
+                               SysFreeString(names[0]);
+
+                               php_printf("\t/* DISPID=%d */\n\t", func->memid);
+
+                               if (func->elemdescFunc.tdesc.vt != VT_VOID) {
+                                       php_printf(" /* %s [%d] */ ",
+                                                       vt_to_string(func->elemdescFunc.tdesc.vt),
+                                                       func->elemdescFunc.tdesc.vt
+                                                       );
+                               }
+
+                               /* TODO: handle prop put and get */
+                               
+                               php_printf("function %s(\n", ansiname);
+
+                               for (j = 0; j < func->cParams; j++) {
+                                       ELEMDESC *elem = &func->lprgelemdescParam[j];
+
+                                       php_printf("\t\t/* %s [%d] ", vt_to_string(elem->tdesc.vt), elem->tdesc.vt);
+
+                                       if (elem->paramdesc.wParamFlags & PARAMFLAG_FIN)
+                                               php_printf("[in]");
+                                       if (elem->paramdesc.wParamFlags & PARAMFLAG_FIN)
+                                               php_printf("[out]");
+
+                                       if (elem->tdesc.vt == VT_PTR) {
+                                               /* what does it point to ? */
+                                               php_printf(" --> %s [%d] ",
+                                                               vt_to_string(elem->tdesc.lptdesc->vt),
+                                                               elem->tdesc.lptdesc->vt
+                                                               );
+                                       }
+
+                                       /* when we handle prop put and get, this will look nicer */
+                                       if (j+1 < (int)cnames) {
+                                               funcdesc = php_OLECHAR_to_char(names[j+1], &funcdesclen, codepage TSRMLS_CC);
+                                               SysFreeString(names[j+1]);
+                                       } else {
+                                               funcdesc = "???";
+                                       }
+
+                                       php_printf(" */ %s%s%c\n",
+                                                       elem->tdesc.vt == VT_PTR ? "&$" : "$",
+                                                       funcdesc,
+                                                       j == func->cParams - 1 ? ' ' : ','
+                                                       );
+
+                                       if (j+1 < (int)cnames)
+                                               efree(funcdesc);
+                               }
+
+                               php_printf("\t\t)\n\t{\n");
+
+                               typeinfo->lpVtbl->GetDocumentation(typeinfo, func->memid, NULL, &olename, NULL, NULL);
+                               if (olename) {
+                                       funcdesc = php_OLECHAR_to_char(olename, &funcdesclen, codepage TSRMLS_CC);
+                                       SysFreeString(olename);
+                                       php_printf("\t\t/* %s */\n", funcdesc);
+                                       efree(funcdesc);
+                               }
+
+                               php_printf("\t}\n");
+
+                               efree(names);
+                       }
+
+                       if (id_to_name) {
+                               zend_str_tolower(ansiname, ansinamelen);
+                               MAKE_STD_ZVAL(tmp);
+                               ZVAL_STRINGL(tmp, ansiname, ansinamelen, 0);
+                               zend_hash_index_update(id_to_name, func->memid, (void*)&tmp, sizeof(zval *), NULL);
+                       }
+
+                       typeinfo->lpVtbl->ReleaseFuncDesc(typeinfo, func);
+
+               }
+
+               if (printdef)
+                       php_printf("}\n");
+
+               ret = 1;
+       } else {
+               zend_error(E_WARNING, "Thats not a dispatchable interface!!\n");
+       }
+
+       typeinfo->lpVtbl->ReleaseTypeAttr(typeinfo, attr);
+
+       return ret;
+}
+
+static ITypeInfo *locate_typeinfo(char *typelibname, comval *obj, char *dispname, int sink TSRMLS_DC)
+{
+       ITypeInfo *typeinfo = NULL;
+       ITypeLib *typelib = NULL;
+       int gotguid = 0;
+       GUID iid;
+
+       if (obj) {
+               if (dispname == NULL && sink) {
+                       /* Looking for the default sink interface */
+                       IProvideClassInfo2 *pci2;
+                       IProvideClassInfo *pci;
+
+                       if (SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IProvideClassInfo2, (void**)&pci2))) {
+                               gotguid = SUCCEEDED(pci2->lpVtbl->GetGUID(pci2, GUIDKIND_DEFAULT_SOURCE_DISP_IID, &iid));
+                               pci2->lpVtbl->Release(pci2);
+                       }
+                       if (!gotguid && SUCCEEDED(C_DISPATCH_VT(obj)->QueryInterface(C_DISPATCH(obj), &IID_IProvideClassInfo, (void**)&pci))) {
+                               /* examine the available interfaces */
+                               /* TODO: write some code here */
+                               pci->lpVtbl->Release(pci);
+                       }
+               } else if (dispname) {
+                       unsigned int idx;
+
+                       /* get the library from the object; the rest will be dealt with later */
+                       C_TYPEINFO_VT(obj)->GetContainingTypeLib(C_TYPEINFO(obj), &typelib, &idx);      
+               }
+       } else if (typelibname) {
+               /* Fetch the typelibrary and use that to look things up */
+               typelib = php_COM_find_typelib(typelibname, CONST_CS TSRMLS_CC);
+       } else if (dispname) {
+               /* TODO: assume that the name is actually a progid */
+       }
+
+       if (!gotguid && dispname && typelib) {
+               unsigned short cfound;
+               MEMBERID memid;
+               OLECHAR *olename = php_char_to_OLECHAR(dispname, strlen(dispname), CP_ACP TSRMLS_CC);
+               
+               cfound = 1;
+               typelib->lpVtbl->FindName(typelib, olename, 0, &typeinfo, &memid, &cfound);
+
+               efree(olename);
+       } else if (gotguid) {
+               typelib->lpVtbl->GetTypeInfoOfGuid(typelib, &iid, &typeinfo);
+       }
+
+       if (typelib)
+               typelib->lpVtbl->Release(typelib);
+
+       return typeinfo;
+}
+
+/* {{{ proto bool com_print_typeinfo(mixed comobject | string typelib, string dispinterface, bool wantsink)
+   Print out a PHP class definition for a dispatchable interface */
+PHP_FUNCTION(com_print_typeinfo)
+{
+       zval *arg1;
+       char *ifacename;
+       char *typelibname = NULL;
+       int ifacelen;
+       zend_bool wantsink;
+       comval *obj = NULL;
+       ITypeInfo *typeinfo;
+       
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zsb", &arg1, &ifacename,
+                               &ifacelen, &wantsink)) {
+               RETURN_FALSE;
+       }
+
+       if (Z_TYPE_P(arg1) == IS_OBJECT && (Z_OBJCE_P(arg1) == &COM_class_entry || !strcmp(Z_OBJCE_P(arg1)->name, "COM"))) {
+               zval **tmp;
+               zend_hash_index_find(Z_OBJPROP_P(arg1), 0, (void**)&tmp);
+               ZEND_FETCH_RESOURCE(obj, comval*, tmp, -1, "comval", IS_COM);
+       } else if (Z_TYPE_P(arg1) == IS_RESOURCE) {
+               ZEND_FETCH_RESOURCE(obj, comval*, &arg1, -1, "comval", IS_COM);
+       } else {
+               convert_to_string_ex(&arg1);
+               typelibname = Z_STRVAL_P(arg1);
+       }
+
+       typeinfo = locate_typeinfo(typelibname, obj, ifacename, wantsink TSRMLS_CC);
+       if (typeinfo) {
+               process_typeinfo(typeinfo, NULL, 1, NULL TSRMLS_CC);
+               typeinfo->lpVtbl->Release(typeinfo);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+/* }}} */
+
+
+/* {{{ proto bool com_event_sink(mixed comobject, object sinkobject [, mixed sinkinterface])
+   Connect events from a COM object to a PHP object */
+PHP_FUNCTION(com_event_sink)
+{
+       zval *object, *sinkobject, *sink=NULL;
+       char *dispname = NULL, *typelibname = NULL;
+       zend_bool gotguid = 0;
+       comval *obj;
+       ITypeInfo *typeinfo = NULL;
+
+       RETVAL_FALSE;
+       
+       if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &object, &sinkobject, &sink)) {
+               RETURN_FALSE;
+       }
+
+       if (Z_TYPE_P(object) == IS_OBJECT && (Z_OBJCE_P(object) == &COM_class_entry || !strcmp(Z_OBJCE_P(object)->name, "COM"))) {
+               zval **tmp;
+               zend_hash_index_find(Z_OBJPROP_P(object), 0, (void**)&tmp);
+               ZEND_FETCH_RESOURCE(obj, comval*, tmp, -1, "comval", IS_COM);
+       } else {
+               ZEND_FETCH_RESOURCE(obj, comval*, &object, -1, "comval", IS_COM);
+       }
+
+       if (sink && Z_TYPE_P(sink) == IS_ARRAY) {
+               /* 0 => typelibname, 1 => dispname */
+               zval **tmp;
+
+               if (zend_hash_index_find(Z_ARRVAL_P(sink), 0, (void**)&tmp) == SUCCESS)
+                       typelibname = Z_STRVAL_PP(tmp);
+               if (zend_hash_index_find(Z_ARRVAL_P(sink), 1, (void**)&tmp) == SUCCESS)
+                       dispname = Z_STRVAL_PP(tmp);
+       } else if (sink != NULL) {
+               convert_to_string_ex(&sink);
+               dispname = Z_STRVAL_P(sink);
+       }
+       
+       typeinfo = locate_typeinfo(typelibname, obj, dispname, 1 TSRMLS_CC);
+
+       if (typeinfo) {
+               HashTable *id_to_name;
+               
+               ALLOC_HASHTABLE(id_to_name);
+               
+               if (process_typeinfo(typeinfo, id_to_name, 0, &obj->sinkid TSRMLS_CC)) {
+
+                       /* Create the COM wrapper for this sink */
+                       obj->sinkdispatch = php_COM_export_as_sink(sinkobject, &obj->sinkid, id_to_name TSRMLS_CC);
+
+                       /* Now hook it up to the source */
+                       com_enable_events(obj, TRUE);
+                       RETVAL_TRUE;
+
+               } else {
+                       FREE_HASHTABLE(id_to_name);
+               }
+       }
+       
+       if (typeinfo)
+               typeinfo->lpVtbl->Release(typeinfo);
+       
+}
+/* }}} */
 
 static int do_COM_offget(VARIANT *result, comval *array, pval *property, int cleanup TSRMLS_DC)
 {
@@ -1539,6 +1949,10 @@ static ITypeLib *php_COM_find_typelib(char *search_string, int mode TSRMLS_DC)
         */
 
        search_string = php_strtok_r(search_string, ",", &strtok_buf);
+
+       if (search_string == NULL)
+               return NULL;
+       
        major = php_strtok_r(NULL, ",", &strtok_buf);
        minor = php_strtok_r(NULL, ",", &strtok_buf);
 
@@ -1767,7 +2181,6 @@ PHPAPI int php_COM_load_typelib(ITypeLib *TypeLib, int mode TSRMLS_DC)
        return SUCCESS;
 }
 
-
 /* {{{ proto bool com_isenum(object com_module)
    Grabs an IEnumVariant */
 PHP_FUNCTION(com_isenum)
index 2930f86fd9285d7895546fa7dc514e6414d3413b..5ec791dd43d19e297070e051323fa21c9d959b82 100644 (file)
@@ -19,6 +19,9 @@ typedef struct comval_ {
                ITypeInfo *typeinfo;
                IEnumVARIANT *enumvariant;
        } i;
+       IDispatch *sinkdispatch;
+       GUID sinkid;
+       DWORD sinkcookie;
 } comval;
 
 END_EXTERN_C()
@@ -37,7 +40,7 @@ END_EXTERN_C()
                        if (handle == NULL) {                                                                                                   \
                                MAKE_STD_ZVAL(handle);                                                                                          \
                        }                                                                                                                                               \
-                       ZVAL_LONG(handle, zend_list_insert((o), IS_COM));                                               \
+                       ZVAL_RESOURCE(handle, zend_list_insert((o), IS_COM));                                           \
                                                                                                                                                                        \
                        zval_copy_ctor(handle);                                                                                                 \
                        zend_hash_index_update(properties, 0, &handle, sizeof(zval *), NULL);   \
@@ -48,7 +51,7 @@ END_EXTERN_C()
 #define RETURN_COM(o)  RETVAL_COM(o)                                                                                           \
                                                return;
 
-#define ALLOC_COM(z)   (z) = (comval *) emalloc(sizeof(comval));                                       \
+#define ALLOC_COM(z)   (z) = (comval *) ecalloc(1, sizeof(comval));                                    \
                                                C_REFCOUNT(z) = 0;
 
 #define FREE_COM(z)            php_COM_destruct(z TSRMLS_CC);
index 802c4f871ee4cbeea589db857d61ec63a907b98f..8d775a86624c8fc9acdd06cff844fabf4f6fd9b2 100644 (file)
@@ -45,9 +45,11 @@ typedef struct {
        zval *object;                   /* the object exported */
        LONG refcount;                  /* COM reference count */
 
-       HashTable dispid_to_name;       /* keep track of dispid -> name mappings */
-       HashTable name_to_dispid;       /* keep track of name -> dispid mappings */
+       HashTable *dispid_to_name;      /* keep track of dispid -> name mappings */
+       HashTable *name_to_dispid;      /* keep track of name -> dispid mappings */
 
+       GUID sinkid;    /* iid that we "implement" for event sinking */
+       
        int id;
 } php_dispatchex;
 
@@ -101,7 +103,8 @@ static HRESULT STDMETHODCALLTYPE disp_queryinterface(
 
        if (IsEqualGUID(&IID_IUnknown, riid) ||
                        IsEqualGUID(&IID_IDispatch, riid) ||
-                       IsEqualGUID(&IID_IDispatchEx, riid)) {
+                       IsEqualGUID(&IID_IDispatchEx, riid) ||
+                       IsEqualGUID(&disp->sinkid, riid)) {
                *ppvObject = This;
                InterlockedIncrement(&disp->refcount);
                return S_OK;
@@ -172,16 +175,16 @@ static HRESULT STDMETHODCALLTYPE disp_getidsofnames(
        for (i = 0; i < cNames; i++) {
                char *name;
                unsigned int namelen;
-               zval *tmp;
+               zval **tmp;
                
                name = php_OLECHAR_to_char(rgszNames[i], &namelen, CP_ACP TSRMLS_CC);
                
                /* Lookup the name in the hash */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == FAILURE) {
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == FAILURE) {
                        ret = DISP_E_UNKNOWNNAME;
                        rgDispId[i] = 0;
                } else {
-                       rgDispId[i] = Z_LVAL_P(tmp);
+                       rgDispId[i] = Z_LVAL_PP(tmp);
                }
 
                efree(name);
@@ -216,15 +219,15 @@ static HRESULT STDMETHODCALLTYPE disp_getdispid(
        HRESULT ret = DISP_E_UNKNOWNNAME;
        char *name;
        unsigned int namelen;
-       zval *tmp;
+       zval **tmp;
        TSRMLS_FETCH();
        FETCH_DISP("GetDispID");
 
        name = php_OLECHAR_to_char(bstrName, &namelen, CP_ACP TSRMLS_CC);
 
        /* Lookup the name in the hash */
-       if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS) {
-               *pid = Z_LVAL_P(tmp);
+       if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS) {
+               *pid = Z_LVAL_PP(tmp);
                ret = S_OK;
        }
 
@@ -243,69 +246,96 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
        /* [out] */ EXCEPINFO *pei,
        /* [unique][in] */ IServiceProvider *pspCaller)
 {
-       zval *name;
+       zval **name;
        UINT i;
        int codepage = CP_ACP;
-       zval **retval = NULL;
+       zval *retval = NULL;
        zval ***params = NULL;
+       HRESULT ret = DISP_E_MEMBERNOTFOUND;
        TSRMLS_FETCH();
        FETCH_DISP("InvokeEx");
 
-       if (SUCCESS == zend_hash_index_find(&disp->dispid_to_name, id, (void**)&name)) {
+       if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
                /* TODO: add support for overloaded objects */
 
-               trace("-- Invoke: %d %s\n", id, Z_STRVAL_P(name));
+               trace("-- Invoke: %d %20s flags=%08x args=%d\n", id, Z_STRVAL_PP(name), wFlags, pdp->cArgs);
                
                /* convert args into zvals.
                 * Args are in reverse order */
                params = (zval ***)emalloc(sizeof(zval **) * pdp->cArgs);
                for (i = 0; i < pdp->cArgs; i++) {
-                       ALLOC_INIT_ZVAL(*params[i]);
+                       VARIANT *arg;
+                       zval *zarg;
+                       
+                       arg = &pdp->rgvarg[ pdp->cArgs - 1 - i];
+
+                       trace("alloc zval for arg %d VT=%08x\n", i, V_VT(arg));
 
-                       if (V_VT(&pdp->rgvarg[pdp->cArgs-i]) == VT_DISPATCH) {
-                               php_COM_object_from_dispatch(V_DISPATCH(&pdp->rgvarg[pdp->cArgs-i]), *params[i] TSRMLS_CC);
+                       ALLOC_INIT_ZVAL(zarg);
+                       
+                       if (V_VT(arg) == VT_DISPATCH) {
+                               trace("arg %d is dispatchable\n", i);
+                               if (NULL == php_COM_object_from_dispatch(V_DISPATCH(arg), zarg TSRMLS_CC)) {
+                                       trace("failed to convert arg %d to zval\n", i);
+                                       ZVAL_NULL(zarg);
+                               }
                        } else {
-                               ZVAL_VARIANT(*params[i], &pdp->rgvarg[pdp->cArgs-i]);
+                               if (FAILURE == php_variant_to_pval(arg, zarg, codepage TSRMLS_CC)) {
+                                       trace("failed to convert arg %d to zval\n", i);
+                                       ZVAL_NULL(zarg);
+                               }
                        }
+                       
+                       params[i] = &zarg;
                }
+
+               trace("arguments processed, prepare to do some work\n");        
                
                if (wFlags & DISPATCH_PROPERTYGET) {
-                       zend_hash_find(Z_OBJPROP_P(disp->object), Z_STRVAL_P(name), Z_STRLEN_P(name)+1, (void**)&retval);
+                       trace("trying to get a property\n");
+                       zend_hash_find(Z_OBJPROP_P(disp->object), Z_STRVAL_PP(name), Z_STRLEN_PP(name)+1, (void**)&retval);
                } else if (wFlags & DISPATCH_PROPERTYPUT) {
-                       add_property_zval(disp->object, Z_STRVAL_P(name), *params[0]);
+                       trace("trying to set a property\n");
+                       add_property_zval(disp->object, Z_STRVAL_PP(name), *params[0]);
                } else if (wFlags & DISPATCH_METHOD) {
-                       
-                       call_user_function_ex(EG(function_table), &disp->object, name,
-                                       retval, pdp->cArgs, params, 1, NULL TSRMLS_CC);
+                       trace("Trying to call user function\n");
+                       if (SUCCESS == call_user_function_ex(EG(function_table), &disp->object, *name,
+                                       &retval, pdp->cArgs, params, 1, NULL TSRMLS_CC)) {
+                               ret = S_OK;
+                       } else {
+                               ret = DISP_E_EXCEPTION;
+                       }
+               } else {
+                       trace("Don't know how to handle this invocation %08x\n", wFlags);
                }
        
                /* release arguments */
                for (i = 0; i < pdp->cArgs; i++)
                        zval_ptr_dtor(params[i]);
+               efree(params);
                
                /* return value */
                if (retval) {
                        if (pvarRes) {
-                               if (Z_TYPE_PP(retval) == IS_OBJECT) {
+                               if (Z_TYPE_P(retval) == IS_OBJECT) {
                                        /* export the object using a dispatch like ourselves */
                                        VariantInit(pvarRes);
                                        V_VT(pvarRes) = VT_DISPATCH;
-                                       V_DISPATCH(pvarRes) = php_COM_export_object(*retval TSRMLS_CC);
+                                       V_DISPATCH(pvarRes) = php_COM_export_object(retval TSRMLS_CC);
                                } else {
-                                       php_pval_to_variant(*retval, pvarRes, codepage TSRMLS_CC);
+                                       php_pval_to_variant(retval, pvarRes, codepage TSRMLS_CC);
                                }
                        }
-                       zval_ptr_dtor(retval);
+                       zval_ptr_dtor(&retval);
                } else if (pvarRes) {
                        VariantInit(pvarRes);
                }
                
-               return S_OK;
        } else {
                trace("InvokeEx: I don't support DISPID=%d\n", id);
        }
 
-       return DISP_E_MEMBERNOTFOUND;
+       return ret;
 }
 
 static HRESULT STDMETHODCALLTYPE disp_deletememberbyname( 
@@ -347,7 +377,7 @@ static HRESULT STDMETHODCALLTYPE disp_getmembername(
        TSRMLS_FETCH();
        FETCH_DISP("GetMemberName");
 
-       if (SUCCESS == zend_hash_index_find(&disp->dispid_to_name, id, (void**)&name)) {
+       if (SUCCESS == zend_hash_index_find(disp->dispid_to_name, id, (void**)&name)) {
                OLECHAR *olestr = php_char_to_OLECHAR(Z_STRVAL_P(name), Z_STRLEN_P(name), CP_ACP TSRMLS_CC);
                *pbstrName = SysAllocString(olestr);
                efree(olestr);
@@ -366,10 +396,10 @@ static HRESULT STDMETHODCALLTYPE disp_getnextdispid(
        ulong next = id+1;
        FETCH_DISP("GetNextDispID");
 
-       while(!zend_hash_index_exists(&disp->dispid_to_name, next))
+       while(!zend_hash_index_exists(disp->dispid_to_name, next))
                next++;
 
-       if (zend_hash_index_exists(&disp->dispid_to_name, next)) {
+       if (zend_hash_index_exists(disp->dispid_to_name, next)) {
                *pid = next;
                return S_OK;
        }
@@ -407,7 +437,7 @@ static struct IDispatchExVtbl php_dispatch_vtbl = {
 
 /* enumerate functions and properties of the object and assign
  * dispatch ids */
-static void update_dispids(php_dispatchex *disp TSRMLS_DC)
+static void generate_dispids(php_dispatchex *disp TSRMLS_DC)
 {
        HashPosition pos;
        char *name = NULL;
@@ -416,6 +446,13 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
        int keytype;
        ulong pid;
 
+       if (disp->dispid_to_name == NULL) {
+               ALLOC_HASHTABLE(disp->dispid_to_name);
+               ALLOC_HASHTABLE(disp->name_to_dispid);
+               zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
+               zend_hash_init(disp->dispid_to_name, 0, NULL, ZVAL_PTR_DTOR, 0);
+       }
+
        /* properties */
        zend_hash_internal_pointer_reset_ex(Z_OBJPROP_PP(&disp->object), &pos);
        while (HASH_KEY_NON_EXISTANT != (keytype =
@@ -430,17 +467,17 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
                zend_hash_move_forward_ex(Z_OBJPROP_PP(&disp->object), &pos);
                
                /* Find the existing id */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
                        continue;
                
                /* add the mappings */
                MAKE_STD_ZVAL(tmp);
                ZVAL_STRINGL(tmp, name, namelen, 1);
-               zend_hash_index_update(&disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
 
                MAKE_STD_ZVAL(tmp);
                ZVAL_LONG(tmp, pid);
-               zend_hash_update(&disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_update(disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
 
        }
        
@@ -459,23 +496,25 @@ static void update_dispids(php_dispatchex *disp TSRMLS_DC)
                zend_hash_move_forward_ex(Z_OBJPROP_PP(&disp->object), &pos);
                
                /* Find the existing id */
-               if (zend_hash_find(&disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
+               if (zend_hash_find(disp->name_to_dispid, name, namelen+1, (void**)&tmp) == SUCCESS)
                        continue;
                
                /* add the mappings */
                MAKE_STD_ZVAL(tmp);
                ZVAL_STRINGL(tmp, name, namelen, 1);
-               zend_hash_index_update(&disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_index_update(disp->dispid_to_name, pid, (void*)&tmp, sizeof(zval *), NULL);
 
                MAKE_STD_ZVAL(tmp);
                ZVAL_LONG(tmp, pid);
-               zend_hash_update(&disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
+               zend_hash_update(disp->name_to_dispid, name, namelen+1, (void*)&tmp, sizeof(zval *), NULL);
        }
 }
 
 static php_dispatchex *disp_constructor(zval *object TSRMLS_DC)
 {
        php_dispatchex *disp = (php_dispatchex*)CoTaskMemAlloc(sizeof(php_dispatchex));
+
+       trace("constructing a COM proxy\n");
        
        if (disp == NULL)
                return NULL;
@@ -486,15 +525,11 @@ static php_dispatchex *disp_constructor(zval *object TSRMLS_DC)
        disp->lpVtbl = &php_dispatch_vtbl;
        disp->refcount = 1;
 
-       zend_hash_init(&disp->dispid_to_name, 0, NULL, NULL, TRUE);
-       zend_hash_init(&disp->name_to_dispid, 0, NULL, NULL, TRUE);
 
        if (object)
                ZVAL_ADDREF(object);
        disp->object = object;
 
-       update_dispids(disp TSRMLS_CC);
-
        disp->id = zend_list_insert(disp, le_dispatch);
        
        return disp;
@@ -511,9 +546,11 @@ static void disp_destructor(php_dispatchex *disp)
        if (disp->refcount > 0)
                CoDisconnectObject((IUnknown*)disp, 0);
 
-       zend_hash_destroy(&disp->dispid_to_name);
-       zend_hash_destroy(&disp->name_to_dispid);
-
+       zend_hash_destroy(disp->dispid_to_name);
+       zend_hash_destroy(disp->name_to_dispid);
+       FREE_HASHTABLE(disp->dispid_to_name);
+       FREE_HASHTABLE(disp->name_to_dispid);
+                       
        if (disp->object)
                zval_ptr_dtor(&disp->object);
 
@@ -521,13 +558,51 @@ static void disp_destructor(php_dispatchex *disp)
        CoTaskMemFree(disp);
 }
 
+PHPAPI IDispatch *php_COM_export_as_sink(zval *val, GUID *sinkid, HashTable *id_to_name TSRMLS_DC)
+{
+       php_dispatchex *disp = disp_constructor(val TSRMLS_CC);
+       HashPosition pos;
+       char *name = NULL;
+       zval *tmp, **ntmp;
+       int namelen;
+       int keytype;
+       ulong pid;
+
+       disp->dispid_to_name = id_to_name;
+
+       memcpy(&disp->sinkid, sinkid, sizeof(disp->sinkid));
+       
+       /* build up the reverse mapping */
+       ALLOC_HASHTABLE(disp->name_to_dispid);
+       zend_hash_init(disp->name_to_dispid, 0, NULL, ZVAL_PTR_DTOR, 0);
+       
+       zend_hash_internal_pointer_reset_ex(id_to_name, &pos);
+       while (HASH_KEY_NON_EXISTANT != (keytype =
+                               zend_hash_get_current_key_ex(id_to_name, &name, &namelen, &pid, 0, &pos))) {
+
+               if (keytype == HASH_KEY_IS_LONG) {
+
+                       zend_hash_get_current_data_ex(id_to_name, (void**)&ntmp, &pos);
+                       
+                       MAKE_STD_ZVAL(tmp);
+                       ZVAL_LONG(tmp, pid);
+                       zend_hash_update(disp->name_to_dispid, Z_STRVAL_PP(ntmp), Z_STRLEN_PP(ntmp)+1, (void*)&tmp, sizeof(zval *), NULL);
+               }
+
+               zend_hash_move_forward_ex(id_to_name, &pos);
+       }
+
+       return (IDispatch*)disp;
+}
 
 PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC)
 {
+       php_dispatchex *disp = NULL;
+
        if (Z_TYPE_P(val) != IS_OBJECT)
                return NULL;
 
-       if (Z_OBJCE_P(val) == &COM_class_entry) {
+       if (Z_OBJCE_P(val) == &COM_class_entry || !strcmp(Z_OBJCE_P(val)->name, "COM")) {
                /* pass back it's IDispatch directly */
                zval **tmp;
                comval *obj;
@@ -541,7 +616,11 @@ PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC)
                C_DISPATCH(obj)->lpVtbl->AddRef(C_DISPATCH(obj));
                return C_DISPATCH(obj);
        }
-       return (IDispatch*)disp_constructor(val TSRMLS_CC);
+
+       disp = disp_constructor(val TSRMLS_CC);
+       generate_dispids(disp TSRMLS_CC);
+
+       return (IDispatch*)disp;
 }
 
 
index 396df9c93c6126de57196f44b113164513b6cc0d..d26441f6a3eab2e47303db7831e3024d3b6bfdd3 100644 (file)
@@ -18,6 +18,9 @@ PHP_FUNCTION(com_propget);
 PHP_FUNCTION(com_propput);
 PHP_FUNCTION(com_load_typelib);
 PHP_FUNCTION(com_isenum);
+PHP_FUNCTION(com_event_sink);
+PHP_FUNCTION(com_message_pump);
+PHP_FUNCTION(com_print_typeinfo);
 
 PHPAPI HRESULT php_COM_invoke(comval *obj, DISPID dispIdMember, WORD wFlags, DISPPARAMS FAR*  pDispParams, VARIANT FAR* pVarResult, char **ErrString TSRMLS_DC);
 PHPAPI HRESULT php_COM_get_ids_of_names(comval *obj, OLECHAR FAR* FAR* rgszNames, DISPID FAR* rgDispId TSRMLS_DC);
@@ -36,6 +39,7 @@ PHPAPI int php_COM_load_typelib(ITypeLib *TypeLib, int mode TSRMLS_DC);
 
 /* dispatch.c */
 PHPAPI IDispatch *php_COM_export_object(zval *val TSRMLS_DC);
+PHPAPI IDispatch *php_COM_export_as_sink(zval *val, GUID *sinkid, HashTable *id_to_name TSRMLS_DC);
 int php_COM_dispatch_init(int module_number TSRMLS_DC);
 
 zend_module_entry COM_module_entry;
index 3fd11f32e47135265eee6947ebafadf7bfce5597..31ea342d66bb3fd78802eb8c1b3602a784c6ccf9 100644 (file)
@@ -16,7 +16,7 @@
                                                                        comval *obj;                                                                            \
                                                                        ALLOC_COM(obj);                                                                         \
                                                                        php_COM_set(obj, &V_DISPATCH(v), TRUE TSRMLS_CC);       \
-                                                                       ZVAL_LONG((z), zend_list_insert(obj, IS_COM));          \
+                                                                       ZVAL_RESOURCE((z), zend_list_insert(obj, IS_COM));              \
                                                                } else {                                                                                                \
                                                                        php_variant_to_pval((v), (z), codepage TSRMLS_CC);      \
                                                                        FREE_VARIANT(v);                                                                        \