root/ext/com_dotnet/com_com.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PHP_FUNCTION
  2. PHP_FUNCTION
  3. php_com_invoke_helper
  4. php_com_get_id_of_name
  5. php_com_do_invoke_byref
  6. php_com_do_invoke_by_id
  7. php_com_do_invoke
  8. PHP_FUNCTION
  9. PHP_FUNCTION
  10. PHP_FUNCTION
  11. PHP_FUNCTION
  12. PHP_FUNCTION

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Wez Furlong  <wez@thebrainroom.com>                          |
  16    +----------------------------------------------------------------------+
  17  */
  18 
  19 /* $Id$ */
  20 
  21 #ifdef HAVE_CONFIG_H
  22 #include "config.h"
  23 #endif
  24 
  25 #include "php.h"
  26 #include "php_ini.h"
  27 #include "ext/standard/info.h"
  28 #include "php_com_dotnet.h"
  29 #include "php_com_dotnet_internal.h"
  30 #include "Zend/zend_exceptions.h"
  31 
  32 /* {{{ com_create_instance - ctor for COM class */
  33 PHP_FUNCTION(com_create_instance)
  34 {
  35         zval *object = getThis();
  36         zval *server_params = NULL;
  37         php_com_dotnet_object *obj;
  38         char *module_name, *typelib_name = NULL, *server_name = NULL;
  39         char *user_name = NULL, *domain_name = NULL, *password = NULL;
  40         size_t module_name_len = 0, typelib_name_len = 0, server_name_len = 0,
  41                 user_name_len, domain_name_len, password_len;
  42         OLECHAR *moniker;
  43         CLSID clsid;
  44         CLSCTX ctx = CLSCTX_SERVER;
  45         HRESULT res = E_FAIL;
  46         int mode = COMG(autoreg_case_sensitive) ? CONST_CS : 0;
  47         ITypeLib *TL = NULL;
  48         COSERVERINFO    info;
  49         COAUTHIDENTITY  authid = {0};
  50         COAUTHINFO              authinfo = {
  51                 RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  52                 RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,
  53                 &authid, EOAC_NONE
  54         };
  55 
  56         php_com_initialize();
  57         obj = CDNO_FETCH(object);
  58 
  59         if (FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  60                         ZEND_NUM_ARGS(), "s|s!ls",
  61                         &module_name, &module_name_len, &server_name, &server_name_len,
  62                         &obj->code_page, &typelib_name, &typelib_name_len) &&
  63                 FAILURE == zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
  64                         ZEND_NUM_ARGS(), "sa|ls",
  65                         &module_name, &module_name_len, &server_params, &obj->code_page,
  66                         &typelib_name, &typelib_name_len)) {
  67 
  68                 php_com_throw_exception(E_INVALIDARG, "Could not create COM object - invalid arguments!");
  69                 return;
  70         }
  71 
  72         if (server_name) {
  73                 ctx = CLSCTX_REMOTE_SERVER;
  74         } else if (server_params) {
  75                 zval *tmp;
  76 
  77                 /* decode the data from the array */
  78 
  79                 if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
  80                                 "Server", sizeof("Server")-1))) {
  81                         convert_to_string_ex(tmp);
  82                         server_name = Z_STRVAL_P(tmp);
  83                         server_name_len = Z_STRLEN_P(tmp);
  84                         ctx = CLSCTX_REMOTE_SERVER;
  85                 }
  86 
  87                 if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
  88                                 "Username", sizeof("Username")-1))) {
  89                         convert_to_string_ex(tmp);
  90                         user_name = Z_STRVAL_P(tmp);
  91                         user_name_len = Z_STRLEN_P(tmp);
  92                 }
  93 
  94                 if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
  95                                 "Password", sizeof("Password")-1))) {
  96                         convert_to_string_ex(tmp);
  97                         password = Z_STRVAL_P(tmp);
  98                         password_len = Z_STRLEN_P(tmp);
  99                 }
 100 
 101                 if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
 102                                 "Domain", sizeof("Domain")-1))) {
 103                         convert_to_string_ex(tmp);
 104                         domain_name = Z_STRVAL_P(tmp);
 105                         domain_name_len = Z_STRLEN_P(tmp);
 106                 }
 107 
 108                 if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
 109                                 "Flags", sizeof("Flags")-1))) {
 110                         convert_to_long_ex(tmp);
 111                         ctx = (CLSCTX)Z_LVAL_P(tmp);
 112                 }
 113         }
 114 
 115         if (server_name && !COMG(allow_dcom)) {
 116                 php_com_throw_exception(E_ERROR, "DCOM has been disabled by your administrator [com.allow_dcom=0]");
 117                 return;
 118         }
 119 
 120         moniker = php_com_string_to_olestring(module_name, module_name_len, obj->code_page);
 121 
 122         /* if instantiating a remote object, either directly, or via
 123          * a moniker, fill in the relevant info */
 124         if (server_name) {
 125                 info.dwReserved1 = 0;
 126                 info.dwReserved2 = 0;
 127                 info.pwszName = php_com_string_to_olestring(server_name, server_name_len, obj->code_page);
 128 
 129                 if (user_name) {
 130                         authid.User = php_com_string_to_olestring(user_name, -1, obj->code_page);
 131                         authid.UserLength = (ULONG)user_name_len;
 132 
 133                         if (password) {
 134                                 authid.Password = (OLECHAR*)password;
 135                                 authid.PasswordLength = (ULONG)password_len;
 136                         } else {
 137                                 authid.Password = (OLECHAR*)"";
 138                                 authid.PasswordLength = 0;
 139                         }
 140 
 141                         if (domain_name) {
 142                                 authid.Domain = (OLECHAR*)domain_name;
 143                                 authid.DomainLength = (ULONG)domain_name_len;
 144                         } else {
 145                                 authid.Domain = (OLECHAR*)"";
 146                                 authid.DomainLength = 0;
 147                         }
 148                         authid.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
 149                         info.pAuthInfo = &authinfo;
 150                 } else {
 151                         info.pAuthInfo = NULL;
 152                 }
 153         }
 154 
 155         if (FAILED(CLSIDFromString(moniker, &clsid))) {
 156                 /* try to use it as a moniker */
 157                 IBindCtx *pBindCtx = NULL;
 158                 IMoniker *pMoniker = NULL;
 159                 ULONG ulEaten;
 160                 BIND_OPTS2 bopt = {0};
 161 
 162                 if (SUCCEEDED(res = CreateBindCtx(0, &pBindCtx))) {
 163                         if (server_name) {
 164                                 /* fill in the remote server info.
 165                                  * MSDN docs indicate that this might be ignored in
 166                                  * current win32 implementations, but at least we are
 167                                  * doing the right thing in readiness for the day that
 168                                  * it does work */
 169                                 bopt.cbStruct = sizeof(bopt);
 170                                 IBindCtx_GetBindOptions(pBindCtx, (BIND_OPTS*)&bopt);
 171                                 bopt.pServerInfo = &info;
 172                                 /* apparently, GetBindOptions will only ever return
 173                                  * a regular BIND_OPTS structure.  My gut feeling is
 174                                  * that it will modify the size field to reflect that
 175                                  * so lets be safe and set it to the BIND_OPTS2 size
 176                                  * again */
 177                                 bopt.cbStruct = sizeof(bopt);
 178                                 IBindCtx_SetBindOptions(pBindCtx, (BIND_OPTS*)&bopt);
 179                         }
 180 
 181                         if (SUCCEEDED(res = MkParseDisplayName(pBindCtx, moniker, &ulEaten, &pMoniker))) {
 182                                 res = IMoniker_BindToObject(pMoniker, pBindCtx,
 183                                         NULL, &IID_IDispatch, (LPVOID*)&V_DISPATCH(&obj->v));
 184 
 185                                 if (SUCCEEDED(res)) {
 186                                         V_VT(&obj->v) = VT_DISPATCH;
 187                                 }
 188 
 189                                 IMoniker_Release(pMoniker);
 190                         }
 191                 }
 192                 if (pBindCtx) {
 193                         IBindCtx_Release(pBindCtx);
 194                 }
 195         } else if (server_name) {
 196                 MULTI_QI                qi;
 197 
 198                 qi.pIID = &IID_IDispatch;
 199                 qi.pItf = NULL;
 200                 qi.hr = S_OK;
 201 
 202                 res = CoCreateInstanceEx(&clsid, NULL, ctx, &info, 1, &qi);
 203 
 204                 if (SUCCEEDED(res)) {
 205                         res = qi.hr;
 206                         V_DISPATCH(&obj->v) = (IDispatch*)qi.pItf;
 207                         V_VT(&obj->v) = VT_DISPATCH;
 208                 }
 209         } else {
 210                 res = CoCreateInstance(&clsid, NULL, CLSCTX_SERVER, &IID_IDispatch, (LPVOID*)&V_DISPATCH(&obj->v));
 211                 if (SUCCEEDED(res)) {
 212                         V_VT(&obj->v) = VT_DISPATCH;
 213                 }
 214         }
 215 
 216         if (server_name) {
 217                 if (info.pwszName) efree(info.pwszName);
 218                 if (authid.User) efree(authid.User);
 219         }
 220 
 221         efree(moniker);
 222 
 223         if (FAILED(res)) {
 224                 char *werr, *msg;
 225 
 226                 werr = php_win32_error_to_msg(res);
 227                 spprintf(&msg, 0, "Failed to create COM object `%s': %s", module_name, werr);
 228                 LocalFree(werr);
 229 
 230                 php_com_throw_exception(res, msg);
 231                 efree(msg);
 232                 return;
 233         }
 234 
 235         /* we got the object and it lives ! */
 236 
 237         /* see if it has TypeInfo available */
 238         if (FAILED(IDispatch_GetTypeInfo(V_DISPATCH(&obj->v), 0, LANG_NEUTRAL, &obj->typeinfo)) && typelib_name) {
 239                 /* load up the library from the named file */
 240                 int cached;
 241 
 242                 TL = php_com_load_typelib_via_cache(typelib_name, obj->code_page, &cached);
 243 
 244                 if (TL) {
 245                         if (COMG(autoreg_on) && !cached) {
 246                                 php_com_import_typelib(TL, mode, obj->code_page);
 247                         }
 248 
 249                         /* cross your fingers... there is no guarantee that this ITypeInfo
 250                          * instance has any relation to this IDispatch instance... */
 251                         ITypeLib_GetTypeInfo(TL, 0, &obj->typeinfo);
 252                         ITypeLib_Release(TL);
 253                 }
 254         } else if (obj->typeinfo && COMG(autoreg_on)) {
 255                 UINT idx;
 256 
 257                 if (SUCCEEDED(ITypeInfo_GetContainingTypeLib(obj->typeinfo, &TL, &idx))) {
 258                         /* check if the library is already in the cache by getting its name */
 259                         BSTR name;
 260 
 261                         if (SUCCEEDED(ITypeLib_GetDocumentation(TL, -1, &name, NULL, NULL, NULL))) {
 262                                 typelib_name = php_com_olestring_to_string(name, &typelib_name_len, obj->code_page);
 263 
 264                                 if (NULL != zend_ts_hash_str_add_ptr(&php_com_typelibraries, typelib_name, typelib_name_len, TL)) {
 265                                         php_com_import_typelib(TL, mode, obj->code_page);
 266 
 267                                         /* add a reference for the hash */
 268                                         ITypeLib_AddRef(TL);
 269                                 }
 270 
 271                         } else {
 272                                 /* try it anyway */
 273                                 php_com_import_typelib(TL, mode, obj->code_page);
 274                         }
 275 
 276                         ITypeLib_Release(TL);
 277                 }
 278         }
 279 
 280 }
 281 /* }}} */
 282 
 283 /* {{{ proto object com_get_active_object(string progid [, int code_page ])
 284    Returns a handle to an already running instance of a COM object */
 285 PHP_FUNCTION(com_get_active_object)
 286 {
 287         CLSID clsid;
 288         char *module_name;
 289         size_t module_name_len;
 290         zend_long code_page = COMG(code_page);
 291         IUnknown *unk = NULL;
 292         IDispatch *obj = NULL;
 293         HRESULT res;
 294         OLECHAR *module = NULL;
 295 
 296         php_com_initialize();
 297         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l",
 298                                 &module_name, &module_name_len, &code_page)) {
 299                 php_com_throw_exception(E_INVALIDARG, "Invalid arguments!");
 300                 return;
 301         }
 302 
 303         module = php_com_string_to_olestring(module_name, module_name_len, (int)code_page);
 304 
 305         res = CLSIDFromString(module, &clsid);
 306 
 307         if (FAILED(res)) {
 308                 php_com_throw_exception(res, NULL);
 309         } else {
 310                 res = GetActiveObject(&clsid, NULL, &unk);
 311 
 312                 if (FAILED(res)) {
 313                         php_com_throw_exception(res, NULL);
 314                 } else {
 315                         res = IUnknown_QueryInterface(unk, &IID_IDispatch, &obj);
 316 
 317                         if (FAILED(res)) {
 318                                 php_com_throw_exception(res, NULL);
 319                         } else if (obj) {
 320                                 /* we got our dispatchable object */
 321                                 php_com_wrap_dispatch(return_value, obj, (int)code_page);
 322                         }
 323                 }
 324         }
 325 
 326         if (obj) {
 327                 IDispatch_Release(obj);
 328         }
 329         if (unk) {
 330                 IUnknown_Release(obj);
 331         }
 332         efree(module);
 333 }
 334 /* }}} */
 335 
 336 /* Performs an Invoke on the given com object.
 337  * returns a failure code and creates an exception if there was an error */
 338 HRESULT php_com_invoke_helper(php_com_dotnet_object *obj, DISPID id_member,
 339                 WORD flags, DISPPARAMS *disp_params, VARIANT *v, int silent, int allow_noarg)
 340 {
 341         HRESULT hr;
 342         unsigned int arg_err;
 343         EXCEPINFO e = {0};
 344 
 345         hr = IDispatch_Invoke(V_DISPATCH(&obj->v), id_member,
 346                 &IID_NULL, LOCALE_SYSTEM_DEFAULT, flags, disp_params, v, &e, &arg_err);
 347 
 348         if (silent == 0 && FAILED(hr)) {
 349                 char *source = NULL, *desc = NULL, *msg = NULL;
 350                 size_t source_len, desc_len;
 351 
 352                 switch (hr) {
 353                         case DISP_E_EXCEPTION:
 354                                 if (e.bstrSource) {
 355                                         source = php_com_olestring_to_string(e.bstrSource, &source_len, obj->code_page);
 356                                         SysFreeString(e.bstrSource);
 357                                 }
 358                                 if (e.bstrDescription) {
 359                                         desc = php_com_olestring_to_string(e.bstrDescription, &desc_len, obj->code_page);
 360                                         SysFreeString(e.bstrDescription);
 361                                 }
 362                                 if (PG(html_errors)) {
 363                                         spprintf(&msg, 0, "<b>Source:</b> %s<br/><b>Description:</b> %s",
 364                                                 source ? source : "Unknown",
 365                                                 desc ? desc : "Unknown");
 366                                 } else {
 367                                         spprintf(&msg, 0, "Source: %s\nDescription: %s",
 368                                                 source ? source : "Unknown",
 369                                                 desc ? desc : "Unknown");
 370                                 }
 371                                 if (desc) {
 372                                         efree(desc);
 373                                 }
 374                                 if (source) {
 375                                         efree(source);
 376                                 }
 377                                 if (e.bstrHelpFile) {
 378                                         SysFreeString(e.bstrHelpFile);
 379                                 }
 380                                 break;
 381 
 382                         case DISP_E_PARAMNOTFOUND:
 383                         case DISP_E_TYPEMISMATCH:
 384                                 desc = php_win32_error_to_msg(hr);
 385                                 spprintf(&msg, 0, "Parameter %d: %s", arg_err, desc);
 386                                 LocalFree(desc);
 387                                 break;
 388 
 389                         case DISP_E_BADPARAMCOUNT:
 390                                 if ((disp_params->cArgs + disp_params->cNamedArgs == 0) && (allow_noarg == 1)) {
 391                                         /* if getting a property and they are missing all parameters,
 392                                          * we want to create a proxy object for them; so lets not create an
 393                                          * exception here */
 394                                         msg = NULL;
 395                                         break;
 396                                 }
 397                                 /* else fall through */
 398 
 399                         default:
 400                                 desc = php_win32_error_to_msg(hr);
 401                                 spprintf(&msg, 0, "Error [0x%08x] %s", hr, desc);
 402                                 LocalFree(desc);
 403                                 break;
 404                 }
 405 
 406                 if (msg) {
 407                         php_com_throw_exception(hr, msg);
 408                         efree(msg);
 409                 }
 410         }
 411 
 412         return hr;
 413 }
 414 
 415 /* map an ID to a name */
 416 HRESULT php_com_get_id_of_name(php_com_dotnet_object *obj, char *name,
 417                 size_t namelen, DISPID *dispid)
 418 {
 419         OLECHAR *olename;
 420         HRESULT hr;
 421         zval *tmp;
 422 
 423         if (namelen == -1) {
 424                 namelen = strlen(name);
 425         }
 426 
 427         if (obj->id_of_name_cache && NULL != (tmp = zend_hash_str_find(obj->id_of_name_cache, name, namelen))) {
 428                 *dispid = (DISPID)Z_LVAL_P(tmp);
 429                 return S_OK;
 430         }
 431 
 432         olename = php_com_string_to_olestring(name, namelen, obj->code_page);
 433 
 434         if (obj->typeinfo) {
 435                 hr = ITypeInfo_GetIDsOfNames(obj->typeinfo, &olename, 1, dispid);
 436                 if (FAILED(hr)) {
 437                         hr = IDispatch_GetIDsOfNames(V_DISPATCH(&obj->v), &IID_NULL, &olename, 1, LOCALE_SYSTEM_DEFAULT, dispid);
 438                         if (SUCCEEDED(hr)) {
 439                                 /* fall back on IDispatch direct */
 440                                 ITypeInfo_Release(obj->typeinfo);
 441                                 obj->typeinfo = NULL;
 442                         }
 443                 }
 444         } else {
 445                 hr = IDispatch_GetIDsOfNames(V_DISPATCH(&obj->v), &IID_NULL, &olename, 1, LOCALE_SYSTEM_DEFAULT, dispid);
 446         }
 447         efree(olename);
 448 
 449         if (SUCCEEDED(hr)) {
 450                 zval tmp;
 451 
 452                 /* cache the mapping */
 453                 if (!obj->id_of_name_cache) {
 454                         ALLOC_HASHTABLE(obj->id_of_name_cache);
 455                         zend_hash_init(obj->id_of_name_cache, 2, NULL, NULL, 0);
 456                 }
 457                 ZVAL_LONG(&tmp, *dispid);
 458                 zend_hash_str_update(obj->id_of_name_cache, name, namelen, &tmp);
 459         }
 460 
 461         return hr;
 462 }
 463 
 464 /* the core of COM */
 465 int php_com_do_invoke_byref(php_com_dotnet_object *obj, zend_internal_function *f,
 466                 WORD flags,     VARIANT *v, int nargs, zval *args)
 467 {
 468         DISPID dispid, altdispid;
 469         DISPPARAMS disp_params;
 470         HRESULT hr;
 471         VARIANT *vargs = NULL, *byref_vals = NULL;
 472         int i, byref_count = 0, j;
 473 
 474         /* assumption: that the active function (f) is the function we generated for the engine */
 475         if (!f) {
 476                 return FAILURE;
 477         }
 478 
 479         hr = php_com_get_id_of_name(obj, f->function_name->val, f->function_name->len, &dispid);
 480 
 481         if (FAILED(hr)) {
 482                 char *winerr = NULL;
 483                 char *msg = NULL;
 484                 winerr = php_win32_error_to_msg(hr);
 485                 spprintf(&msg, 0, "Unable to lookup `%s': %s", f->function_name->val, winerr);
 486                 LocalFree(winerr);
 487                 php_com_throw_exception(hr, msg);
 488                 efree(msg);
 489                 return FAILURE;
 490         }
 491 
 492 
 493         if (nargs) {
 494                 vargs = (VARIANT*)safe_emalloc(sizeof(VARIANT), nargs, 0);
 495         }
 496 
 497         if (f->arg_info) {
 498                 for (i = 0; i < nargs; i++) {
 499                         if (f->arg_info[nargs - i - 1].pass_by_reference) {
 500                                 byref_count++;
 501                         }
 502                 }
 503         }
 504 
 505         if (byref_count) {
 506                 byref_vals = (VARIANT*)safe_emalloc(sizeof(VARIANT), byref_count, 0);
 507                 for (j = 0, i = 0; i < nargs; i++) {
 508                         if (f->arg_info[nargs - i - 1].pass_by_reference) {
 509                                 /* put the value into byref_vals instead */
 510                                 php_com_variant_from_zval(&byref_vals[j], &args[nargs - i - 1], obj->code_page);
 511 
 512                                 /* if it is already byref, "move" it into the vargs array, otherwise
 513                                  * make vargs a reference to this value */
 514                                 if (V_VT(&byref_vals[j]) & VT_BYREF) {
 515                                         memcpy(&vargs[i], &byref_vals[j], sizeof(vargs[i]));
 516                                         VariantInit(&byref_vals[j]); /* leave the variant slot empty to simplify cleanup */
 517                                 } else {
 518                                         VariantInit(&vargs[i]);
 519                                         V_VT(&vargs[i]) = V_VT(&byref_vals[j]) | VT_BYREF;
 520                                         /* union magic ensures that this works out */
 521                                         vargs[i].byref = &V_UINT(&byref_vals[j]);
 522                                 }
 523                                 j++;
 524                         } else {
 525                                 php_com_variant_from_zval(&vargs[i], &args[nargs - i - 1], obj->code_page);
 526                         }
 527                 }
 528 
 529         } else {
 530                 /* Invoke'd args are in reverse order */
 531                 for (i = 0; i < nargs; i++) {
 532                         php_com_variant_from_zval(&vargs[i], &args[nargs - i - 1], obj->code_page);
 533                 }
 534         }
 535 
 536         disp_params.cArgs = nargs;
 537         disp_params.cNamedArgs = 0;
 538         disp_params.rgvarg = vargs;
 539         disp_params.rgdispidNamedArgs = NULL;
 540 
 541         if (flags & DISPATCH_PROPERTYPUT) {
 542                 altdispid = DISPID_PROPERTYPUT;
 543                 disp_params.rgdispidNamedArgs = &altdispid;
 544                 disp_params.cNamedArgs = 1;
 545         }
 546 
 547         /* this will create an exception if needed */
 548         hr = php_com_invoke_helper(obj, dispid, flags, &disp_params, v, 0, 0);
 549 
 550         /* release variants */
 551         if (vargs) {
 552                 if (f && f->arg_info) {
 553                         for (i = 0, j = 0; i < nargs; i++) {
 554                                 /* if this was byref, update the zval */
 555                                 if (f->arg_info[nargs - i - 1].pass_by_reference) {
 556                                         SEPARATE_ZVAL_IF_NOT_REF(&args[nargs - i - 1]);
 557 
 558                                         /* if the variant is pointing at the byref_vals, we need to map
 559                                          * the pointee value as a zval; otherwise, the value is pointing
 560                                          * into an existing PHP variant record */
 561                                         if (V_VT(&vargs[i]) & VT_BYREF) {
 562                                                 if (vargs[i].byref == &V_UINT(&byref_vals[j])) {
 563                                                         /* copy that value */
 564                                                         php_com_zval_from_variant(&args[nargs - i - 1], &byref_vals[j],
 565                                                                 obj->code_page);
 566                                                 }
 567                                         } else {
 568                                                 /* not sure if this can ever happen; the variant we marked as BYREF
 569                                                  * is no longer BYREF - copy its value */
 570                                                 php_com_zval_from_variant(&args[nargs - i - 1], &vargs[i],
 571                                                         obj->code_page);
 572                                         }
 573                                         VariantClear(&byref_vals[j]);
 574                                         j++;
 575                                 }
 576                                 VariantClear(&vargs[i]);
 577                         }
 578                 } else {
 579                         for (i = 0, j = 0; i < nargs; i++) {
 580                                 VariantClear(&vargs[i]);
 581                         }
 582                 }
 583                 efree(vargs);
 584         }
 585 
 586         return SUCCEEDED(hr) ? SUCCESS : FAILURE;
 587 }
 588 
 589 
 590 
 591 int php_com_do_invoke_by_id(php_com_dotnet_object *obj, DISPID dispid,
 592                 WORD flags,     VARIANT *v, int nargs, zval *args, int silent, int allow_noarg)
 593 {
 594         DISPID altdispid;
 595         DISPPARAMS disp_params;
 596         HRESULT hr;
 597         VARIANT *vargs = NULL;
 598         int i;
 599 
 600         if (nargs) {
 601                 vargs = (VARIANT*)safe_emalloc(sizeof(VARIANT), nargs, 0);
 602         }
 603 
 604         /* Invoke'd args are in reverse order */
 605         for (i = 0; i < nargs; i++) {
 606                 php_com_variant_from_zval(&vargs[i], &args[nargs - i - 1], obj->code_page);
 607         }
 608 
 609         disp_params.cArgs = nargs;
 610         disp_params.cNamedArgs = 0;
 611         disp_params.rgvarg = vargs;
 612         disp_params.rgdispidNamedArgs = NULL;
 613 
 614         if (flags & DISPATCH_PROPERTYPUT) {
 615                 altdispid = DISPID_PROPERTYPUT;
 616                 disp_params.rgdispidNamedArgs = &altdispid;
 617                 disp_params.cNamedArgs = 1;
 618         }
 619 
 620         /* this will create an exception if needed */
 621         hr = php_com_invoke_helper(obj, dispid, flags, &disp_params, v, silent, allow_noarg);
 622 
 623         /* release variants */
 624         if (vargs) {
 625                 for (i = 0; i < nargs; i++) {
 626                         VariantClear(&vargs[i]);
 627                 }
 628                 efree(vargs);
 629         }
 630 
 631         /* a bit of a hack this, but it's needed for COM array access. */
 632         if (hr == DISP_E_BADPARAMCOUNT)
 633                 return hr;
 634 
 635         return SUCCEEDED(hr) ? SUCCESS : FAILURE;
 636 }
 637 
 638 int php_com_do_invoke(php_com_dotnet_object *obj, char *name, size_t namelen,
 639                 WORD flags,     VARIANT *v, int nargs, zval *args, int allow_noarg)
 640 {
 641         DISPID dispid;
 642         HRESULT hr;
 643         char *winerr = NULL;
 644         char *msg = NULL;
 645 
 646         hr = php_com_get_id_of_name(obj, name, namelen, &dispid);
 647 
 648         if (FAILED(hr)) {
 649                 winerr = php_win32_error_to_msg(hr);
 650                 spprintf(&msg, 0, "Unable to lookup `%s': %s", name, winerr);
 651                 LocalFree(winerr);
 652                 php_com_throw_exception(hr, msg);
 653                 efree(msg);
 654                 return FAILURE;
 655         }
 656 
 657         return php_com_do_invoke_by_id(obj, dispid, flags, v, nargs, args, 0, allow_noarg);
 658 }
 659 
 660 /* {{{ proto string com_create_guid()
 661    Generate a globally unique identifier (GUID) */
 662 PHP_FUNCTION(com_create_guid)
 663 {
 664         GUID retval;
 665         OLECHAR *guid_string;
 666 
 667         if (zend_parse_parameters_none() == FAILURE) {
 668                 return;
 669         }
 670 
 671         php_com_initialize();
 672         if (CoCreateGuid(&retval) == S_OK && StringFromCLSID(&retval, &guid_string) == S_OK) {
 673                 size_t len;
 674                 char *str;
 675 
 676                 str = php_com_olestring_to_string(guid_string, &len, CP_ACP);
 677                 RETVAL_STRINGL(str, len);
 678                 // TODO: avoid reallocation ???
 679                 efree(str);
 680 
 681                 CoTaskMemFree(guid_string);
 682         } else {
 683                 RETURN_FALSE;
 684         }
 685 }
 686 /* }}} */
 687 
 688 /* {{{ proto bool com_event_sink(object comobject, object sinkobject [, mixed sinkinterface])
 689    Connect events from a COM object to a PHP object */
 690 PHP_FUNCTION(com_event_sink)
 691 {
 692         zval *object, *sinkobject, *sink=NULL;
 693         char *dispname = NULL, *typelibname = NULL;
 694         php_com_dotnet_object *obj;
 695         ITypeInfo *typeinfo = NULL;
 696 
 697         RETVAL_FALSE;
 698 
 699         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "Oo|z/",
 700                         &object, php_com_variant_class_entry, &sinkobject, &sink)) {
 701                 RETURN_FALSE;
 702         }
 703 
 704         php_com_initialize();
 705         obj = CDNO_FETCH(object);
 706 
 707         if (sink && Z_TYPE_P(sink) == IS_ARRAY) {
 708                 /* 0 => typelibname, 1 => dispname */
 709                 zval *tmp;
 710 
 711                 if ((tmp = zend_hash_index_find(Z_ARRVAL_P(sink), 0)) != NULL && Z_TYPE_P(tmp) == IS_STRING)
 712                         typelibname = Z_STRVAL_P(tmp);
 713                 if ((tmp = zend_hash_index_find(Z_ARRVAL_P(sink), 1)) != NULL && Z_TYPE_P(tmp) == IS_STRING)
 714                         dispname = Z_STRVAL_P(tmp);
 715         } else if (sink != NULL) {
 716                 convert_to_string(sink);
 717                 dispname = Z_STRVAL_P(sink);
 718         }
 719 
 720         typeinfo = php_com_locate_typeinfo(typelibname, obj, dispname, 1);
 721 
 722         if (typeinfo) {
 723                 HashTable *id_to_name;
 724 
 725                 ALLOC_HASHTABLE(id_to_name);
 726 
 727                 if (php_com_process_typeinfo(typeinfo, id_to_name, 0, &obj->sink_id, obj->code_page)) {
 728 
 729                         /* Create the COM wrapper for this sink */
 730                         obj->sink_dispatch = php_com_wrapper_export_as_sink(sinkobject, &obj->sink_id, id_to_name);
 731 
 732                         /* Now hook it up to the source */
 733                         php_com_object_enable_event_sink(obj, TRUE);
 734                         RETVAL_TRUE;
 735 
 736                 } else {
 737                         FREE_HASHTABLE(id_to_name);
 738                 }
 739         }
 740 
 741         if (typeinfo) {
 742                 ITypeInfo_Release(typeinfo);
 743         }
 744 
 745 }
 746 /* }}} */
 747 
 748 /* {{{ proto bool com_print_typeinfo(object comobject | string typelib, string dispinterface, bool wantsink)
 749    Print out a PHP class definition for a dispatchable interface */
 750 PHP_FUNCTION(com_print_typeinfo)
 751 {
 752         zval *arg1;
 753         char *ifacename = NULL;
 754         char *typelibname = NULL;
 755         size_t ifacelen;
 756         zend_bool wantsink = 0;
 757         php_com_dotnet_object *obj = NULL;
 758         ITypeInfo *typeinfo;
 759 
 760         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "z/|s!b", &arg1, &ifacename,
 761                                 &ifacelen, &wantsink)) {
 762                 RETURN_FALSE;
 763         }
 764 
 765         php_com_initialize();
 766         if (Z_TYPE_P(arg1) == IS_OBJECT) {
 767                 CDNO_FETCH_VERIFY(obj, arg1);
 768         } else {
 769                 convert_to_string(arg1);
 770                 typelibname = Z_STRVAL_P(arg1);
 771         }
 772 
 773         typeinfo = php_com_locate_typeinfo(typelibname, obj, ifacename, wantsink ? 1 : 0);
 774         if (typeinfo) {
 775                 php_com_process_typeinfo(typeinfo, NULL, 1, NULL, obj ? obj->code_page : COMG(code_page));
 776                 ITypeInfo_Release(typeinfo);
 777                 RETURN_TRUE;
 778         } else {
 779                 zend_error(E_WARNING, "Unable to find typeinfo using the parameters supplied");
 780         }
 781         RETURN_FALSE;
 782 }
 783 /* }}} */
 784 
 785 /* {{{ proto bool com_message_pump([int timeoutms])
 786    Process COM messages, sleeping for up to timeoutms milliseconds */
 787 PHP_FUNCTION(com_message_pump)
 788 {
 789         zend_long timeoutms = 0;
 790         MSG msg;
 791         DWORD result;
 792 
 793         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &timeoutms) == FAILURE)
 794                 RETURN_FALSE;
 795 
 796         php_com_initialize();
 797         result = MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD)timeoutms, QS_ALLINPUT);
 798 
 799         if (result == WAIT_OBJECT_0) {
 800                 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
 801                         TranslateMessage(&msg);
 802                         DispatchMessage(&msg);
 803                 }
 804                 /* we processed messages */
 805                 RETVAL_TRUE;
 806         } else {
 807                 /* we did not process messages (timed out) */
 808                 RETVAL_FALSE;
 809         }
 810 }
 811 /* }}} */
 812 
 813 /* {{{ proto bool com_load_typelib(string typelib_name [, bool case_insensitive])
 814    Loads a Typelibrary and registers its constants */
 815 PHP_FUNCTION(com_load_typelib)
 816 {
 817         char *name;
 818         size_t namelen;
 819         ITypeLib *pTL = NULL;
 820         zend_bool cs = TRUE;
 821         int codepage = COMG(code_page);
 822         int cached = 0;
 823 
 824         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &name, &namelen, &cs)) {
 825                 return;
 826         }
 827 
 828         RETVAL_FALSE;
 829 
 830         php_com_initialize();
 831         pTL = php_com_load_typelib_via_cache(name, codepage, &cached);
 832         if (pTL) {
 833                 if (cached) {
 834                         RETVAL_TRUE;
 835                 } else if (php_com_import_typelib(pTL, cs ? CONST_CS : 0, codepage) == SUCCESS) {
 836                         RETVAL_TRUE;
 837                 }
 838 
 839                 ITypeLib_Release(pTL);
 840                 pTL = NULL;
 841         }
 842 }
 843 /* }}} */
 844 
 845 
 846 
 847 /*
 848  * Local variables:
 849  * tab-width: 4
 850  * c-basic-offset: 4
 851  * End:
 852  * vim600: noet sw=4 ts=4 fdm=marker
 853  * vim<600: noet sw=4 ts=4
 854  */

/* [<][>][^][v][top][bottom][index][help] */