moniker.c 47.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 *	Monikers
 *
 *	Copyright 1998	Marcus Meissner
5
 *      Copyright 1999  Noomen Hamza
6
 *      Copyright 2005  Robert Shearman (for CodeWeavers)
7
 *      Copyright 2007  Robert Shearman
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
22 23
 */

24 25 26
#include "config.h"
#include "wine/port.h"

27
#include <stdarg.h>
28
#include <string.h>
29

30 31
#define COBJMACROS

Alexandre Julliard's avatar
Alexandre Julliard committed
32
#include "winerror.h"
33
#include "windef.h"
34
#include "winbase.h"
35
#include "winuser.h"
36
#include "wtypes.h"
Patrik Stridvall's avatar
Patrik Stridvall committed
37
#include "ole2.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38

39 40
#include "wine/list.h"
#include "wine/debug.h"
41
#include "wine/unicode.h"
42
#include "wine/exception.h"
43

44
#include "compobj_private.h"
45
#include "moniker.h"
46
#include "irot.h"
47

48
WINE_DEFAULT_DEBUG_CHANNEL(ole);
49

50
/* see MSDN docs for IROTData::GetComparisonData, which states what this
51 52
 * constant is
 */
53 54
#define MAX_COMPARISON_DATA 2048

55
static LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
56
{
57
    return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
58 59
}

Noomen Hamza's avatar
Noomen Hamza committed
60
/* define the structure of the running object table elements */
61 62 63
struct rot_entry
{
    struct list        entry;
64 65
    InterfaceData* object; /* marshaled running object*/
    MonikerComparisonData* moniker_data; /* moniker comparison data that identifies this object */
66 67
    DWORD              cookie; /* cookie identifying this object */
    FILETIME           last_modified;
68
    IrotContextHandle  ctxt_handle;
69
};
Noomen Hamza's avatar
Noomen Hamza committed
70

71
/* define the RunningObjectTableImpl structure */
72 73 74
typedef struct RunningObjectTableImpl
{
    const IRunningObjectTableVtbl *lpVtbl;
75
    LONG ref;
76

77 78
    struct list rot; /* list of ROT entries */
    CRITICAL_SECTION lock;
Noomen Hamza's avatar
Noomen Hamza committed
79 80
} RunningObjectTableImpl;

81
static RunningObjectTableImpl* runningObjectTableInstance = NULL;
82
static IrotHandle irot_handle;
83

84 85 86 87
/* define the EnumMonikerImpl structure */
typedef struct EnumMonikerImpl
{
    const IEnumMonikerVtbl *lpVtbl;
88
    LONG ref;
89

90
    InterfaceList *moniker_list;
91
    ULONG pos;
92 93 94 95
} EnumMonikerImpl;


/* IEnumMoniker Local functions*/
96
static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
97
    ULONG pos, IEnumMoniker **ppenumMoniker);
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
static IrotHandle get_irot_handle(void)
{
    if (!irot_handle)
    {
        RPC_STATUS status;
        RPC_WSTR binding;
        IrotHandle new_handle;
        unsigned short ncacn_np[] = IROT_PROTSEQ;
        unsigned short endpoint[] = IROT_ENDPOINT;
        status = RpcStringBindingComposeW(NULL, ncacn_np, NULL, endpoint, NULL, &binding);
        if (status == RPC_S_OK)
        {
            status = RpcBindingFromStringBindingW(binding, &new_handle);
            RpcStringFreeW(&binding);
        }
        if (status != RPC_S_OK)
            return NULL;
        if (InterlockedCompareExchangePointer(&irot_handle, new_handle, NULL))
            /* another thread beat us to it */
            RpcBindingFree(&new_handle);
    }
    return irot_handle;
}

static BOOL start_rpcss(void)
{
    PROCESS_INFORMATION pi;
    STARTUPINFOW si;
127 128
    WCHAR cmd[MAX_PATH];
    static const WCHAR rpcss[] = {'\\','r','p','c','s','s','.','e','x','e',0};
129
    BOOL rslt;
130
    void *redir;
131 132 133 134 135

    TRACE("\n");

    ZeroMemory(&si, sizeof(STARTUPINFOA));
    si.cb = sizeof(STARTUPINFOA);
136 137
    GetSystemDirectoryW( cmd, MAX_PATH - sizeof(rpcss)/sizeof(WCHAR) );
    strcatW( cmd, rpcss );
138

139
    Wow64DisableWow64FsRedirection( &redir );
140
    rslt = CreateProcessW( cmd, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
141
    Wow64RevertWow64FsRedirection( redir );
142 143 144 145 146 147 148 149 150 151 152 153

    if (rslt)
    {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        Sleep(100);
    }

    return rslt;
}

static HRESULT create_stream_on_mip_ro(const InterfaceData *mip, IStream **stream)
154 155 156 157 158 159 160 161 162 163
{
    HGLOBAL hglobal = GlobalAlloc(0, mip->ulCntData);
    void *pv = GlobalLock(hglobal);
    memcpy(pv, mip->abData, mip->ulCntData);
    GlobalUnlock(hglobal);
    return CreateStreamOnHGlobal(hglobal, TRUE, stream);
}

static inline void rot_entry_delete(struct rot_entry *rot_entry)
{
164
    if (rot_entry->cookie)
165
    {
166 167 168 169
        InterfaceData *object = NULL;
        InterfaceData *moniker = NULL;
        __TRY
        {
170 171
            IrotRevoke(get_irot_handle(), rot_entry->cookie,
                       &rot_entry->ctxt_handle, &object, &moniker);
172 173
        }
        __EXCEPT(rpc_filter)
174 175
        {
        }
176 177 178 179 180 181 182 183 184 185 186 187 188 189
        __ENDTRY
        MIDL_user_free(object);
        if (moniker)
        {
            IStream *stream;
            HRESULT hr;
            hr = create_stream_on_mip_ro(moniker, &stream);
            if (hr == S_OK)
            {
                CoReleaseMarshalData(stream);
                IUnknown_Release(stream);
            }
        }
        MIDL_user_free(moniker);
190
    }
191
    if (rot_entry->object)
192 193 194
    {
        IStream *stream;
        HRESULT hr;
195
        hr = create_stream_on_mip_ro(rot_entry->object, &stream);
196 197 198 199 200 201 202 203 204 205 206 207
        if (hr == S_OK)
        {
            CoReleaseMarshalData(stream);
            IUnknown_Release(stream);
        }
    }
    HeapFree(GetProcessHeap(), 0, rot_entry->object);
    HeapFree(GetProcessHeap(), 0, rot_entry->moniker_data);
    HeapFree(GetProcessHeap(), 0, rot_entry);
}

/* moniker_data must be freed with HeapFree when no longer in use */
208
static HRESULT get_moniker_comparison_data(IMoniker *pMoniker, MonikerComparisonData **moniker_data)
209 210 211 212
{
    HRESULT hr;
    IROTData *pROTData = NULL;
    hr = IMoniker_QueryInterface(pMoniker, &IID_IROTData, (void *)&pROTData);
213
    if (SUCCEEDED(hr))
214
    {
215
        ULONG size = MAX_COMPARISON_DATA;
216 217 218 219 220 221
        *moniker_data = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MonikerComparisonData, abData[size]));
        if (!*moniker_data)
        {
            IROTData_Release(pROTData);
            return E_OUTOFMEMORY;
        }
222
        hr = IROTData_GetComparisonData(pROTData, (*moniker_data)->abData, size, &size);
223
        IROTData_Release(pROTData);
224 225 226 227 228 229 230
        if (hr != S_OK)
        {
            ERR("Failed to copy comparison data into buffer, hr = 0x%08x\n", hr);
            HeapFree(GetProcessHeap(), 0, *moniker_data);
            return hr;
        }
        (*moniker_data)->ulCntData = size;
231
    }
232
    else
233
    {
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        IBindCtx *pbc;
        LPOLESTR pszDisplayName;
        CLSID clsid;
        int len;

        TRACE("generating comparison data from display name\n");

        hr = CreateBindCtx(0, &pbc);
        if (FAILED(hr))
            return hr;
        hr = IMoniker_GetDisplayName(pMoniker, pbc, NULL, &pszDisplayName);
        IBindCtx_Release(pbc);
        if (FAILED(hr))
            return hr;
        hr = IMoniker_GetClassID(pMoniker, &clsid);
        if (FAILED(hr))
        {
            CoTaskMemFree(pszDisplayName);
            return hr;
        }

        len = strlenW(pszDisplayName);
        *moniker_data = HeapAlloc(GetProcessHeap(), 0,
257
            FIELD_OFFSET(MonikerComparisonData, abData[sizeof(CLSID) + (len+1)*sizeof(WCHAR)]));
258 259 260 261 262 263 264 265 266
        if (!*moniker_data)
        {
            CoTaskMemFree(pszDisplayName);
            return E_OUTOFMEMORY;
        }
        (*moniker_data)->ulCntData = sizeof(CLSID) + (len+1)*sizeof(WCHAR);

        memcpy(&(*moniker_data)->abData[0], &clsid, sizeof(clsid));
        memcpy(&(*moniker_data)->abData[sizeof(clsid)], pszDisplayName, (len+1)*sizeof(WCHAR));
267
        CoTaskMemFree(pszDisplayName);
268 269 270 271
    }
    return S_OK;
}

272
static HRESULT reduce_moniker(IMoniker *pmk, IBindCtx *pbc, IMoniker **pmkReduced)
273
{
274 275 276 277 278 279 280 281 282
    IBindCtx *pbcNew = NULL;
    HRESULT hr;
    if (!pbc)
    {
        hr = CreateBindCtx(0, &pbcNew);
        if (FAILED(hr))
            return hr;
        pbc = pbcNew;
    }
283 284 285
    hr = IMoniker_Reduce(pmk, pbc, MKRREDUCE_ALL, NULL, pmkReduced);
    if (FAILED(hr))
        ERR("reducing moniker failed with error 0x%08x\n", hr);
286
    if (pbcNew) IBindCtx_Release(pbcNew);
287 288 289
    return hr;
}

Noomen Hamza's avatar
Noomen Hamza committed
290 291
/***********************************************************************
 *        RunningObjectTable_QueryInterface
Alexandre Julliard's avatar
Alexandre Julliard committed
292
 */
293 294 295
static HRESULT WINAPI
RunningObjectTableImpl_QueryInterface(IRunningObjectTable* iface,
                                      REFIID riid,void** ppvObject)
Noomen Hamza's avatar
Noomen Hamza committed
296
{
297
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
Noomen Hamza's avatar
Noomen Hamza committed
298

299
    TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
Noomen Hamza's avatar
Noomen Hamza committed
300

301
    /* validate arguments */
Noomen Hamza's avatar
Noomen Hamza committed
302 303 304 305 306 307

    if (ppvObject==0)
        return E_INVALIDARG;

    *ppvObject = 0;

308 309
    if (IsEqualIID(&IID_IUnknown, riid) ||
        IsEqualIID(&IID_IRunningObjectTable, riid))
310
        *ppvObject = This;
Noomen Hamza's avatar
Noomen Hamza committed
311 312 313 314

    if ((*ppvObject)==0)
        return E_NOINTERFACE;

315
    IRunningObjectTable_AddRef(iface);
Noomen Hamza's avatar
Noomen Hamza committed
316 317 318 319 320 321 322

    return S_OK;
}

/***********************************************************************
 *        RunningObjectTable_AddRef
 */
323 324
static ULONG WINAPI
RunningObjectTableImpl_AddRef(IRunningObjectTable* iface)
Noomen Hamza's avatar
Noomen Hamza committed
325
{
326
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
Noomen Hamza's avatar
Noomen Hamza committed
327

328
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
329

330
    return InterlockedIncrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
331 332 333
}

/***********************************************************************
334
 *        RunningObjectTable_Destroy
Noomen Hamza's avatar
Noomen Hamza committed
335
 */
336
static HRESULT
337
RunningObjectTableImpl_Destroy(void)
Noomen Hamza's avatar
Noomen Hamza committed
338
{
339
    struct list *cursor, *cursor2;
340
    IrotHandle old_handle;
341

342
    TRACE("()\n");
343

Noomen Hamza's avatar
Noomen Hamza committed
344 345 346 347
    if (runningObjectTableInstance==NULL)
        return E_INVALIDARG;

    /* free the ROT table memory */
348 349 350 351 352 353
    LIST_FOR_EACH_SAFE(cursor, cursor2, &runningObjectTableInstance->rot)
    {
        struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
        list_remove(&rot_entry->entry);
        rot_entry_delete(rot_entry);
    }
Noomen Hamza's avatar
Noomen Hamza committed
354

355 356 357
    DEBUG_CLEAR_CRITSEC_NAME(&runningObjectTableInstance->lock);
    DeleteCriticalSection(&runningObjectTableInstance->lock);

Noomen Hamza's avatar
Noomen Hamza committed
358 359
    /* free the ROT structure memory */
    HeapFree(GetProcessHeap(),0,runningObjectTableInstance);
360
    runningObjectTableInstance = NULL;
Noomen Hamza's avatar
Noomen Hamza committed
361

362 363 364 365 366
    old_handle = irot_handle;
    irot_handle = NULL;
    if (old_handle)
        RpcBindingFree(&old_handle);

Noomen Hamza's avatar
Noomen Hamza committed
367 368 369 370 371 372
    return S_OK;
}

/***********************************************************************
 *        RunningObjectTable_Release
 */
373 374
static ULONG WINAPI
RunningObjectTableImpl_Release(IRunningObjectTable* iface)
Noomen Hamza's avatar
Noomen Hamza committed
375
{
376
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
377
    ULONG ref;
Noomen Hamza's avatar
Noomen Hamza committed
378

379
    TRACE("(%p)\n",This);
Noomen Hamza's avatar
Noomen Hamza committed
380

381
    ref = InterlockedDecrement(&This->ref);
Noomen Hamza's avatar
Noomen Hamza committed
382

383 384 385 386 387
    /* uninitialize ROT structure if there's no more references to it */
    if (ref == 0)
    {
        struct list *cursor, *cursor2;
        LIST_FOR_EACH_SAFE(cursor, cursor2, &This->rot)
Noomen Hamza's avatar
Noomen Hamza committed
388
        {
389 390 391
            struct rot_entry *rot_entry = LIST_ENTRY(cursor, struct rot_entry, entry);
            list_remove(&rot_entry->entry);
            rot_entry_delete(rot_entry);
Noomen Hamza's avatar
Noomen Hamza committed
392
        }
393 394 395
        /*  RunningObjectTable data structure will be not destroyed here ! the destruction will be done only
         *  when RunningObjectTableImpl_UnInitialize function is called
         */
Noomen Hamza's avatar
Noomen Hamza committed
396 397
    }

398
    return ref;
Noomen Hamza's avatar
Noomen Hamza committed
399 400 401 402
}

/***********************************************************************
 *        RunningObjectTable_Register
403 404 405 406 407
 *
 * PARAMS
 * grfFlags       [in] Registration options 
 * punkObject     [in] the object being registered
 * pmkObjectName  [in] the moniker of the object being registered
408
 * pdwRegister    [out] the value identifying the registration
Noomen Hamza's avatar
Noomen Hamza committed
409
 */
410 411 412
static HRESULT WINAPI
RunningObjectTableImpl_Register(IRunningObjectTable* iface, DWORD grfFlags,
               IUnknown *punkObject, IMoniker *pmkObjectName, DWORD *pdwRegister)
Noomen Hamza's avatar
Noomen Hamza committed
413
{
414
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
415 416 417 418
    struct rot_entry *rot_entry;
    HRESULT hr = S_OK;
    IStream *pStream = NULL;
    DWORD mshlflags;
419
    IBindCtx *pbc;
420
    InterfaceData *moniker = NULL;
Noomen Hamza's avatar
Noomen Hamza committed
421

422
    TRACE("(%p,%d,%p,%p,%p)\n",This,grfFlags,punkObject,pmkObjectName,pdwRegister);
Noomen Hamza's avatar
Noomen Hamza committed
423

424
    if (grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT))
425
    {
426
        ERR("Invalid grfFlags: 0x%08x\n", grfFlags & ~(ROTFLAGS_REGISTRATIONKEEPSALIVE|ROTFLAGS_ALLOWANYCLIENT));
Noomen Hamza's avatar
Noomen Hamza committed
427
        return E_INVALIDARG;
428
    }
Noomen Hamza's avatar
Noomen Hamza committed
429 430 431 432

    if (punkObject==NULL || pmkObjectName==NULL || pdwRegister==NULL)
        return E_INVALIDARG;

433 434 435
    rot_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*rot_entry));
    if (!rot_entry)
        return E_OUTOFMEMORY;
Noomen Hamza's avatar
Noomen Hamza committed
436

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    /* marshal object */
    hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    if (hr != S_OK)
    {
        rot_entry_delete(rot_entry);
        return hr;
    }
    mshlflags = (grfFlags & ROTFLAGS_REGISTRATIONKEEPSALIVE) ? MSHLFLAGS_TABLESTRONG : MSHLFLAGS_TABLEWEAK;
    hr = CoMarshalInterface(pStream, &IID_IUnknown, punkObject, MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, mshlflags);
    /* FIXME: a cleaner way would be to create an IStream class that writes
     * directly to an MInterfacePointer */
    if (hr == S_OK)
    {
        HGLOBAL hglobal;
        hr = GetHGlobalFromStream(pStream, &hglobal);
        if (hr == S_OK)
        {
            SIZE_T size = GlobalSize(hglobal);
            const void *pv = GlobalLock(hglobal);
            rot_entry->object = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(MInterfacePointer, abData[size]));
            rot_entry->object->ulCntData = size;
458
            memcpy(rot_entry->object->abData, pv, size);
459 460 461 462 463 464 465 466
            GlobalUnlock(hglobal);
        }
    }
    IStream_Release(pStream);
    if (hr != S_OK)
    {
        rot_entry_delete(rot_entry);
        return hr;
467
    }
468

469
    hr = CreateBindCtx(0, &pbc);
470 471 472 473 474 475
    if (FAILED(hr))
    {
        rot_entry_delete(rot_entry);
        return hr;
    }

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    hr = reduce_moniker(pmkObjectName, pbc, &pmkObjectName);
    if (FAILED(hr))
    {
        rot_entry_delete(rot_entry);
        IBindCtx_Release(pbc);
        return hr;
    }

    hr = IMoniker_GetTimeOfLastChange(pmkObjectName, pbc, NULL,
                                      &rot_entry->last_modified);
    IBindCtx_Release(pbc);
    if (FAILED(hr))
    {
        CoFileTimeNow(&rot_entry->last_modified);
        hr = S_OK;
    }

493 494
    hr = get_moniker_comparison_data(pmkObjectName,
                                     &rot_entry->moniker_data);
495 496 497
    if (hr != S_OK)
    {
        rot_entry_delete(rot_entry);
498
        IMoniker_Release(pmkObjectName);
499 500
        return hr;
    }
Noomen Hamza's avatar
Noomen Hamza committed
501

502 503 504 505
    hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    if (hr != S_OK)
    {
        rot_entry_delete(rot_entry);
506
        IMoniker_Release(pmkObjectName);
507
        return hr;
Noomen Hamza's avatar
Noomen Hamza committed
508
    }
509
    /* marshal moniker */
510 511
    hr = CoMarshalInterface(pStream, &IID_IMoniker, (IUnknown *)pmkObjectName,
                            MSHCTX_LOCAL | MSHCTX_NOSHAREDMEM, NULL, MSHLFLAGS_TABLESTRONG);
512 513 514 515 516 517 518 519 520 521
    /* FIXME: a cleaner way would be to create an IStream class that writes
     * directly to an MInterfacePointer */
    if (hr == S_OK)
    {
        HGLOBAL hglobal;
        hr = GetHGlobalFromStream(pStream, &hglobal);
        if (hr == S_OK)
        {
            SIZE_T size = GlobalSize(hglobal);
            const void *pv = GlobalLock(hglobal);
522 523
            moniker = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceData, abData[size]));
            moniker->ulCntData = size;
524
            memcpy(moniker->abData, pv, size);
525 526 527 528
            GlobalUnlock(hglobal);
        }
    }
    IStream_Release(pStream);
529
    IMoniker_Release(pmkObjectName);
530 531
    if (hr != S_OK)
    {
532
        HeapFree(GetProcessHeap(), 0, moniker);
533 534
        rot_entry_delete(rot_entry);
        return hr;
535
    }
536

537 538 539 540 541

    while (TRUE)
    {
        __TRY
        {
542 543 544 545
            hr = IrotRegister(get_irot_handle(), rot_entry->moniker_data,
                              rot_entry->object, moniker,
                              &rot_entry->last_modified, grfFlags,
                              &rot_entry->cookie, &rot_entry->ctxt_handle);
546 547 548 549 550 551 552 553 554 555 556 557 558
        }
        __EXCEPT(rpc_filter)
        {
            hr = HRESULT_FROM_WIN32(GetExceptionCode());
        }
        __ENDTRY
        if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
        {
            if (start_rpcss())
                continue;
        }
        break;
    }
559
    HeapFree(GetProcessHeap(), 0, moniker);
560
    if (FAILED(hr))
561 562 563 564 565 566 567 568 569 570 571 572 573
    {
        rot_entry_delete(rot_entry);
        return hr;
    }

    /* gives a registration identifier to the registered object*/
    *pdwRegister = rot_entry->cookie;

    EnterCriticalSection(&This->lock);
    list_add_tail(&This->rot, &rot_entry->entry);
    LeaveCriticalSection(&This->lock);

    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
574 575 576 577
}

/***********************************************************************
 *        RunningObjectTable_Revoke
578 579 580
 *
 * PARAMS
 *  dwRegister [in] Value identifying registration to be revoked
Noomen Hamza's avatar
Noomen Hamza committed
581
 */
582 583
static HRESULT WINAPI
RunningObjectTableImpl_Revoke( IRunningObjectTable* iface, DWORD dwRegister) 
Noomen Hamza's avatar
Noomen Hamza committed
584
{
585
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
586
    struct rot_entry *rot_entry;
Noomen Hamza's avatar
Noomen Hamza committed
587

588
    TRACE("(%p,%d)\n",This,dwRegister);
Noomen Hamza's avatar
Noomen Hamza committed
589

590 591 592 593 594 595 596
    EnterCriticalSection(&This->lock);
    LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
    {
        if (rot_entry->cookie == dwRegister)
        {
            list_remove(&rot_entry->entry);
            LeaveCriticalSection(&This->lock);
Noomen Hamza's avatar
Noomen Hamza committed
597

598 599 600
            rot_entry_delete(rot_entry);
            return S_OK;
        }
601
    }
602
    LeaveCriticalSection(&This->lock);
603

604
    return E_INVALIDARG;
Noomen Hamza's avatar
Noomen Hamza committed
605
}
606

Noomen Hamza's avatar
Noomen Hamza committed
607 608
/***********************************************************************
 *        RunningObjectTable_IsRunning
609 610 611
 *
 * PARAMS
 *  pmkObjectName [in]  moniker of the object whose status is desired 
Noomen Hamza's avatar
Noomen Hamza committed
612
 */
613 614
static HRESULT WINAPI
RunningObjectTableImpl_IsRunning( IRunningObjectTable* iface, IMoniker *pmkObjectName)
615
{
616
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
617
    MonikerComparisonData *moniker_data;
618
    HRESULT hr;
619
    const struct rot_entry *rot_entry;
Noomen Hamza's avatar
Noomen Hamza committed
620

621
    TRACE("(%p,%p)\n",This,pmkObjectName);
Noomen Hamza's avatar
Noomen Hamza committed
622

623
    hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
624 625
    if (FAILED(hr))
        return hr;
626
    hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
627
    IMoniker_Release(pmkObjectName);
628 629 630 631 632
    if (hr != S_OK)
        return hr;

    hr = S_FALSE;
    EnterCriticalSection(&This->lock);
633
    LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
634 635
    {
        if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
636
            !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
637 638 639 640 641 642 643
        {
            hr = S_OK;
            break;
        }
    }
    LeaveCriticalSection(&This->lock);

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    if (hr == S_FALSE)
    {
        while (TRUE)
        {
            __TRY
            {
                hr = IrotIsRunning(get_irot_handle(), moniker_data);
            }
            __EXCEPT(rpc_filter)
            {
                hr = HRESULT_FROM_WIN32(GetExceptionCode());
            }
            __ENDTRY
            if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
            {
                if (start_rpcss())
                    continue;
            }
            break;
        }
    }
665 666 667 668

    HeapFree(GetProcessHeap(), 0, moniker_data);

    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
669 670 671 672
}

/***********************************************************************
 *        RunningObjectTable_GetObject
673 674 675 676
 *
 * PARAMS
 * pmkObjectName [in] Pointer to the moniker on the object 
 * ppunkObject   [out] variable that receives the IUnknown interface pointer
Noomen Hamza's avatar
Noomen Hamza committed
677
 */
678 679 680
static HRESULT WINAPI
RunningObjectTableImpl_GetObject( IRunningObjectTable* iface,
                     IMoniker *pmkObjectName, IUnknown **ppunkObject)
Noomen Hamza's avatar
Noomen Hamza committed
681
{
682
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
683
    MonikerComparisonData *moniker_data;
684
    InterfaceData *object = NULL;
685
    IrotCookie cookie;
686 687
    HRESULT hr;
    struct rot_entry *rot_entry;
Noomen Hamza's avatar
Noomen Hamza committed
688

689
    TRACE("(%p,%p,%p)\n",This,pmkObjectName,ppunkObject);
Noomen Hamza's avatar
Noomen Hamza committed
690

691
    if (ppunkObject == NULL)
Noomen Hamza's avatar
Noomen Hamza committed
692
        return E_POINTER;
693

694
    *ppunkObject = NULL;
Noomen Hamza's avatar
Noomen Hamza committed
695

696
    hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
697 698
    if (FAILED(hr))
        return hr;
699
    hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
700
    IMoniker_Release(pmkObjectName);
701 702 703 704 705 706 707
    if (hr != S_OK)
        return hr;

    EnterCriticalSection(&This->lock);
    LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
    {
        if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
708
            !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
709 710 711 712 713 714 715 716 717 718 719 720 721 722
        {
            IStream *pStream;
            hr = create_stream_on_mip_ro(rot_entry->object, &pStream);
            if (hr == S_OK)
            {
                hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
                IStream_Release(pStream);
            }

            LeaveCriticalSection(&This->lock);
            HeapFree(GetProcessHeap(), 0, moniker_data);

            return hr;
        }
723
    }
724
    LeaveCriticalSection(&This->lock);
Noomen Hamza's avatar
Noomen Hamza committed
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
    TRACE("moniker unavailable locally, calling SCM\n");

    while (TRUE)
    {
        __TRY
        {
            hr = IrotGetObject(get_irot_handle(), moniker_data, &object, &cookie);
        }
        __EXCEPT(rpc_filter)
        {
            hr = HRESULT_FROM_WIN32(GetExceptionCode());
        }
        __ENDTRY
        if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
        {
            if (start_rpcss())
                continue;
        }
        break;
    }

    if (SUCCEEDED(hr))
    {
        IStream *pStream;
        hr = create_stream_on_mip_ro(object, &pStream);
        if (hr == S_OK)
        {
            hr = CoUnmarshalInterface(pStream, &IID_IUnknown, (void **)ppunkObject);
            IStream_Release(pStream);
        }
    }
    else
        WARN("Moniker unavailable, IrotGetObject returned 0x%08x\n", hr);
Noomen Hamza's avatar
Noomen Hamza committed
759

760 761 762
    HeapFree(GetProcessHeap(), 0, moniker_data);

    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
763
}
764

765
/***********************************************************************
Noomen Hamza's avatar
Noomen Hamza committed
766
 *        RunningObjectTable_NoteChangeTime
767 768 769 770
 *
 * PARAMS
 *  dwRegister [in] Value identifying registration being updated
 *  pfiletime  [in] Pointer to structure containing object's last change time
771
 */
772 773 774
static HRESULT WINAPI
RunningObjectTableImpl_NoteChangeTime(IRunningObjectTable* iface,
                                      DWORD dwRegister, FILETIME *pfiletime)
Noomen Hamza's avatar
Noomen Hamza committed
775
{
776
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
777
    struct rot_entry *rot_entry;
778
    HRESULT hr = E_INVALIDARG;
Noomen Hamza's avatar
Noomen Hamza committed
779

780
    TRACE("(%p,%d,%p)\n",This,dwRegister,pfiletime);
Noomen Hamza's avatar
Noomen Hamza committed
781

782 783 784 785 786 787 788
    EnterCriticalSection(&This->lock);
    LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, struct rot_entry, entry)
    {
        if (rot_entry->cookie == dwRegister)
        {
            rot_entry->last_modified = *pfiletime;
            LeaveCriticalSection(&This->lock);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

            while (TRUE)
            {
                __TRY
                {
                    hr = IrotNoteChangeTime(get_irot_handle(), dwRegister, pfiletime);
                }
                __EXCEPT(rpc_filter)
                {
                    hr = HRESULT_FROM_WIN32(GetExceptionCode());
                }
                __ENDTRY
                if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
                {
                    if (start_rpcss())
                        continue;
                }
                break;
            }
808 809

            goto done;
810 811 812
        }
    }
    LeaveCriticalSection(&This->lock);
Noomen Hamza's avatar
Noomen Hamza committed
813

814
done:
815 816
    TRACE("-- 0x08%x\n", hr);
    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
817
}
818

Noomen Hamza's avatar
Noomen Hamza committed
819 820
/***********************************************************************
 *        RunningObjectTable_GetTimeOfLastChange
821 822 823 824
 *
 * PARAMS
 *  pmkObjectName  [in]  moniker of the object whose status is desired 
 *  pfiletime      [out] structure that receives object's last change time
Noomen Hamza's avatar
Noomen Hamza committed
825
 */
826 827 828
static HRESULT WINAPI
RunningObjectTableImpl_GetTimeOfLastChange(IRunningObjectTable* iface,
                            IMoniker *pmkObjectName, FILETIME *pfiletime)
Noomen Hamza's avatar
Noomen Hamza committed
829
{
830
    HRESULT hr = MK_E_UNAVAILABLE;
831
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
832 833
    MonikerComparisonData *moniker_data;
    const struct rot_entry *rot_entry;
Noomen Hamza's avatar
Noomen Hamza committed
834

835
    TRACE("(%p,%p,%p)\n",This,pmkObjectName,pfiletime);
Noomen Hamza's avatar
Noomen Hamza committed
836 837 838 839

    if (pmkObjectName==NULL || pfiletime==NULL)
        return E_INVALIDARG;

840
    hr = reduce_moniker(pmkObjectName, NULL, &pmkObjectName);
841 842
    if (FAILED(hr))
        return hr;
843
    hr = get_moniker_comparison_data(pmkObjectName, &moniker_data);
844
    IMoniker_Release(pmkObjectName);
845 846
    if (hr != S_OK)
        return hr;
Noomen Hamza's avatar
Noomen Hamza committed
847

848
    hr = MK_E_UNAVAILABLE;
Noomen Hamza's avatar
Noomen Hamza committed
849

850
    EnterCriticalSection(&This->lock);
851
    LIST_FOR_EACH_ENTRY(rot_entry, &This->rot, const struct rot_entry, entry)
852 853
    {
        if ((rot_entry->moniker_data->ulCntData == moniker_data->ulCntData) &&
854
            !memcmp(moniker_data->abData, rot_entry->moniker_data->abData, moniker_data->ulCntData))
855 856 857 858 859 860 861 862
        {
            *pfiletime = rot_entry->last_modified;
            hr = S_OK;
            break;
        }
    }
    LeaveCriticalSection(&This->lock);

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    if (hr != S_OK)
    {
        while (TRUE)
        {
            __TRY
            {
                hr = IrotGetTimeOfLastChange(get_irot_handle(), moniker_data, pfiletime);
            }
            __EXCEPT(rpc_filter)
            {
                hr = HRESULT_FROM_WIN32(GetExceptionCode());
            }
            __ENDTRY
            if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
            {
                if (start_rpcss())
                    continue;
            }
            break;
        }
    }
884 885

    HeapFree(GetProcessHeap(), 0, moniker_data);
886 887

    TRACE("-- 0x%08x\n", hr);
888
    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
889 890 891 892
}

/***********************************************************************
 *        RunningObjectTable_EnumRunning
893 894 895
 *
 * PARAMS
 *  ppenumMoniker  [out]  receives the IEnumMoniker interface pointer 
Noomen Hamza's avatar
Noomen Hamza committed
896
 */
897 898 899
static HRESULT WINAPI
RunningObjectTableImpl_EnumRunning(IRunningObjectTable* iface,
                                   IEnumMoniker **ppenumMoniker)
Noomen Hamza's avatar
Noomen Hamza committed
900
{
901
    RunningObjectTableImpl *This = (RunningObjectTableImpl *)iface;
902 903
    InterfaceList *interface_list = NULL;
    HRESULT hr;
Noomen Hamza's avatar
Noomen Hamza committed
904

905
    TRACE("(%p, %p)\n", This, ppenumMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
906

907
    *ppenumMoniker = NULL;
Noomen Hamza's avatar
Noomen Hamza committed
908

909
    while (TRUE)
910
    {
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
        __TRY
        {
            hr = IrotEnumRunning(get_irot_handle(), &interface_list);
        }
        __EXCEPT(rpc_filter)
        {
            hr = HRESULT_FROM_WIN32(GetExceptionCode());
        }
        __ENDTRY
        if (hr == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE))
        {
            if (start_rpcss())
                continue;
        }
        break;
926
    }
927

928
    if (SUCCEEDED(hr))
929
        hr = EnumMonikerImpl_CreateEnumROTMoniker(interface_list,
930
                                                  0, ppenumMoniker);
Noomen Hamza's avatar
Noomen Hamza committed
931

932
    return hr;
Noomen Hamza's avatar
Noomen Hamza committed
933 934
}

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
/* Virtual function table for the IRunningObjectTable class. */
static const IRunningObjectTableVtbl VT_RunningObjectTableImpl =
{
    RunningObjectTableImpl_QueryInterface,
    RunningObjectTableImpl_AddRef,
    RunningObjectTableImpl_Release,
    RunningObjectTableImpl_Register,
    RunningObjectTableImpl_Revoke,
    RunningObjectTableImpl_IsRunning,
    RunningObjectTableImpl_GetObject,
    RunningObjectTableImpl_NoteChangeTime,
    RunningObjectTableImpl_GetTimeOfLastChange,
    RunningObjectTableImpl_EnumRunning
};

/***********************************************************************
 *        RunningObjectTable_Initialize
 */
HRESULT WINAPI RunningObjectTableImpl_Initialize(void)
{
    TRACE("\n");

    /* create the unique instance of the RunningObjectTableImpl structure */
    runningObjectTableInstance = HeapAlloc(GetProcessHeap(), 0, sizeof(RunningObjectTableImpl));

    if (!runningObjectTableInstance)
        return E_OUTOFMEMORY;

    /* initialize the virtual table function */
    runningObjectTableInstance->lpVtbl = &VT_RunningObjectTableImpl;

966 967 968
    /* the initial reference is set to "1" so that it isn't destroyed after its
     * first use until the process is destroyed, as the running object table is
     * a process-wide cache of a global table */
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    runningObjectTableInstance->ref = 1;

    list_init(&runningObjectTableInstance->rot);
    InitializeCriticalSection(&runningObjectTableInstance->lock);
    DEBUG_SET_CRITSEC_NAME(&runningObjectTableInstance->lock, "RunningObjectTableImpl.lock");

    return S_OK;
}

/***********************************************************************
 *        RunningObjectTable_UnInitialize
 */
HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void)
{
    TRACE("\n");

    if (runningObjectTableInstance==NULL)
        return E_POINTER;

    RunningObjectTableImpl_Release((IRunningObjectTable*)runningObjectTableInstance);

    RunningObjectTableImpl_Destroy();

    return S_OK;
}

Noomen Hamza's avatar
Noomen Hamza committed
995
/***********************************************************************
996
 *           GetRunningObjectTable (OLE32.@)
997 998 999 1000 1001 1002 1003 1004 1005 1006
 *
 * Retrieves the global running object table.
 *
 * PARAMS
 *  reserved [I] Reserved. Set to 0.
 *  pprot    [O] Address that receives the pointer to the running object table.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: Any HRESULT code.
Noomen Hamza's avatar
Noomen Hamza committed
1007
 */
1008 1009
HRESULT WINAPI
GetRunningObjectTable(DWORD reserved, LPRUNNINGOBJECTTABLE *pprot)
Noomen Hamza's avatar
Noomen Hamza committed
1010 1011 1012 1013
{
    IID riid=IID_IRunningObjectTable;
    HRESULT res;

1014
    TRACE("()\n");
Noomen Hamza's avatar
Noomen Hamza committed
1015 1016 1017 1018 1019 1020 1021

    if (reserved!=0)
        return E_UNEXPECTED;

    if(runningObjectTableInstance==NULL)
        return CO_E_NOTINITIALIZED;

1022
    res = IRunningObjectTable_QueryInterface((IRunningObjectTable*)runningObjectTableInstance,&riid,(void**)pprot);
Noomen Hamza's avatar
Noomen Hamza committed
1023 1024

    return res;
1025
}
Marcus Meissner's avatar
Marcus Meissner committed
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
static HRESULT get_moniker_for_progid_display_name(LPBC pbc,
                                                   LPCOLESTR szDisplayName,
                                                   LPDWORD pchEaten,
                                                   LPMONIKER *ppmk)
{
    CLSID clsid;
    HRESULT hr;
    LPWSTR progid;
    LPCWSTR start = szDisplayName;
    LPCWSTR end;
    int len;
    IMoniker *class_moniker;

    if (*start == '@')
        start++;

    /* find end delimiter */
    for (end = start; *end; end++)
        if (*end == ':')
            break;

    len = end - start;

    /* must start with '@' or have a ':' somewhere and mustn't be one character
     * long (since that looks like an absolute path) */
    if (((start == szDisplayName) && (*end == '\0')) || (len <= 1))
        return MK_E_SYNTAX;

    progid = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
    if (progid)
    {
        memcpy(progid, start, len * sizeof(WCHAR));
        progid[len] = '\0';
    }
    hr = CLSIDFromProgID(progid, &clsid);
    HeapFree(GetProcessHeap(), 0, progid);
    if (FAILED(hr))
        return MK_E_SYNTAX;

    hr = CreateClassMoniker(&clsid, &class_moniker);
    if (SUCCEEDED(hr))
    {
        IParseDisplayName *pdn;
        hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
                                   &IID_IParseDisplayName, (void **)&pdn);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
        /* fallback to using IClassFactory to get IParseDisplayName -
         * adsldp.dll depends on this */
        if (FAILED(hr))
        {
            IClassFactory *pcf;
            hr = IMoniker_BindToObject(class_moniker, pbc, NULL,
                                       &IID_IClassFactory, (void **)&pcf);
            if (SUCCEEDED(hr))
            {
                hr = IClassFactory_CreateInstance(pcf, NULL,
                                                  &IID_IParseDisplayName,
                                                  (void **)&pdn);
                IClassFactory_Release(pcf);
            }
        }
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
        IMoniker_Release(class_moniker);
        if (SUCCEEDED(hr))
        {
            hr = IParseDisplayName_ParseDisplayName(pdn, pbc,
                                                    (LPOLESTR)szDisplayName,
                                                    pchEaten, ppmk);
            IParseDisplayName_Release(pdn);
        }
    }
    return hr;
}

Marcus Meissner's avatar
Marcus Meissner committed
1099
/******************************************************************************
1100 1101
 *              MkParseDisplayName        [OLE32.@]
 */
1102
HRESULT WINAPI MkParseDisplayName(LPBC pbc, LPCOLESTR szDisplayName,
1103 1104
				LPDWORD pchEaten, LPMONIKER *ppmk)
{
1105 1106 1107 1108 1109 1110
    HRESULT hr = MK_E_SYNTAX;
    static const WCHAR wszClsidColon[] = {'c','l','s','i','d',':'};
    IMoniker *moniker;
    DWORD chEaten;

    TRACE("(%p, %s, %p, %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
1111

1112 1113 1114 1115 1116 1117 1118
    if (!pbc || !IsValidInterface((LPUNKNOWN) pbc))
        return E_INVALIDARG;

    if (!szDisplayName || !*szDisplayName)
        return E_INVALIDARG;

    if (!pchEaten || !ppmk)
1119 1120
        return E_INVALIDARG;

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    *pchEaten = 0;
    *ppmk = NULL;

    if (!strncmpiW(szDisplayName, wszClsidColon, sizeof(wszClsidColon)/sizeof(wszClsidColon[0])))
    {
        hr = ClassMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
        if (FAILED(hr) && (hr != MK_E_SYNTAX))
            return hr;
    }
    else
    {
        hr = get_moniker_for_progid_display_name(pbc, szDisplayName, &chEaten, &moniker);
        if (FAILED(hr) && (hr != MK_E_SYNTAX))
            return hr;
    }

    if (FAILED(hr))
    {
        hr = FileMoniker_CreateFromDisplayName(pbc, szDisplayName, &chEaten, &moniker);
        if (FAILED(hr) && (hr != MK_E_SYNTAX))
            return hr;
    }

    if (SUCCEEDED(hr))
    {
        while (TRUE)
        {
            IMoniker *next_moniker;
            *pchEaten += chEaten;
            szDisplayName += chEaten;
            if (!*szDisplayName)
            {
                *ppmk = moniker;
                return S_OK;
            }
            chEaten = 0;
            hr = IMoniker_ParseDisplayName(moniker, pbc, NULL,
                                           (LPOLESTR)szDisplayName, &chEaten,
                                           &next_moniker);
            IMoniker_Release(moniker);
            if (FAILED(hr))
            {
                *pchEaten = 0;
                break;
            }
            moniker = next_moniker;
        }
    }

    return hr;
1171 1172 1173 1174
}

/***********************************************************************
 *        GetClassFile (OLE32.@)
1175
 *
1176
 * Retrieves the class ID associated with the given filename.
1177 1178
 *
 * PARAMS
1179 1180
 *  filePathName [I] Filename to retrieve the class ID for.
 *  pclsid       [O] Address that receives the class ID for the file.
1181 1182 1183 1184
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: Any HRESULT code.
Marcus Meissner's avatar
Marcus Meissner committed
1185
 */
1186
HRESULT WINAPI GetClassFile(LPCOLESTR filePathName,CLSID *pclsid)
Marcus Meissner's avatar
Marcus Meissner committed
1187
{
1188 1189 1190 1191 1192 1193 1194 1195
    IStorage *pstg=0;
    HRESULT res;
    int nbElm, length, i;
    LONG sizeProgId;
    LPOLESTR *pathDec=0,absFile=0,progId=0;
    LPWSTR extension;
    static const WCHAR bkslashW[] = {'\\',0};
    static const WCHAR dotW[] = {'.',0};
1196

1197
    TRACE("%s, %p\n", debugstr_w(filePathName), pclsid);
Jacek Caban's avatar
Jacek Caban committed
1198

1199 1200
    /* if the file contain a storage object the return the CLSID written by IStorage_SetClass method*/
    if((StgIsStorageFile(filePathName))==S_OK){
Jacek Caban's avatar
Jacek Caban committed
1201

1202
        res=StgOpenStorage(filePathName,NULL,STGM_READ | STGM_SHARE_DENY_WRITE,NULL,0,&pstg);
1203

1204 1205
        if (SUCCEEDED(res))
            res=ReadClassStg(pstg,pclsid);
1206

1207
        IStorage_Release(pstg);
1208

1209 1210
        return res;
    }
Austin English's avatar
Austin English committed
1211
    /* If the file is not a storage object then attempt to match various bits in the file against a
1212
       pattern in the registry. This case is not frequently used, so I present only the pseudocode for
Austin English's avatar
Austin English committed
1213
       this case.
1214

1215
     for(i=0;i<nFileTypes;i++)
1216

1217
        for(i=0;j<nPatternsForType;j++){
1218

1219 1220
            PATTERN pat;
            HANDLE  hFile;
1221

1222 1223 1224 1225 1226
            pat=ReadPatternFromRegistry(i,j);
            hFile=CreateFileW(filePathName,,,,,,hFile);
            SetFilePosition(hFile,pat.offset);
            ReadFile(hFile,buf,pat.size,&r,NULL);
            if (memcmp(buf&pat.mask,pat.pattern.pat.size)==0){
1227

1228 1229 1230 1231 1232
                *pclsid=ReadCLSIDFromRegistry(i);
                return S_OK;
            }
        }
     */
1233

1234
    /* if the above strategies fail then search for the extension key in the registry */
1235

1236 1237 1238
    /* get the last element (absolute file) in the path name */
    nbElm=FileMonikerImpl_DecomposePath(filePathName,&pathDec);
    absFile=pathDec[nbElm-1];
1239

Austin English's avatar
Austin English committed
1240
    /* failed if the path represents a directory and not an absolute file name*/
1241 1242
    if (!lstrcmpW(absFile, bkslashW))
        return MK_E_INVALIDEXTENSION;
1243

1244 1245 1246 1247 1248
    /* get the extension of the file */
    extension = NULL;
    length=lstrlenW(absFile);
    for(i = length-1; (i >= 0) && *(extension = &absFile[i]) != '.'; i--)
        /* nothing */;
1249

1250 1251
    if (!extension || !lstrcmpW(extension, dotW))
        return MK_E_INVALIDEXTENSION;
1252

1253
    res=RegQueryValueW(HKEY_CLASSES_ROOT, extension, NULL, &sizeProgId);
1254

1255 1256 1257
    /* get the progId associated to the extension */
    progId = CoTaskMemAlloc(sizeProgId);
    res = RegQueryValueW(HKEY_CLASSES_ROOT, extension, progId, &sizeProgId);
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
    if (res==ERROR_SUCCESS)
        /* return the clsid associated to the progId */
        res= CLSIDFromProgID(progId,pclsid);

    for(i=0; pathDec[i]!=NULL;i++)
        CoTaskMemFree(pathDec[i]);
    CoTaskMemFree(pathDec);

    CoTaskMemFree(progId);

    if (res==ERROR_SUCCESS)
        return res;

    return MK_E_INVALIDEXTENSION;
1273
}
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

/***********************************************************************
 *        EnumMoniker_QueryInterface
 */
static HRESULT WINAPI EnumMonikerImpl_QueryInterface(IEnumMoniker* iface,REFIID riid,void** ppvObject)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;

    TRACE("(%p,%p,%p)\n",This,riid,ppvObject);

    /* validate arguments */
    if (ppvObject == NULL)
        return E_INVALIDARG;

    *ppvObject = NULL;

    if (IsEqualIID(&IID_IUnknown, riid))
1291
        *ppvObject = This;
1292 1293
    else
        if (IsEqualIID(&IID_IEnumMoniker, riid))
1294
            *ppvObject = This;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327

    if ((*ppvObject)==NULL)
        return E_NOINTERFACE;

    IEnumMoniker_AddRef(iface);

    return S_OK;
}

/***********************************************************************
 *        EnumMoniker_AddRef
 */
static ULONG   WINAPI EnumMonikerImpl_AddRef(IEnumMoniker* iface)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;

    TRACE("(%p)\n",This);

    return InterlockedIncrement(&This->ref);
}

/***********************************************************************
 *        EnumMoniker_release
 */
static ULONG   WINAPI EnumMonikerImpl_Release(IEnumMoniker* iface)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
    ULONG ref;

    TRACE("(%p)\n",This);

    ref = InterlockedDecrement(&This->ref);

1328
    /* uninitialize rot structure if there's no more reference to it*/
1329 1330 1331
    if (ref == 0)
    {
        ULONG i;
1332

1333
        TRACE("(%p) Deleting\n",This);
1334

1335 1336 1337
        for (i = 0; i < This->moniker_list->size; i++)
            HeapFree(GetProcessHeap(), 0, This->moniker_list->interfaces[i]);
        HeapFree(GetProcessHeap(), 0, This->moniker_list);
1338
        HeapFree(GetProcessHeap(), 0, This);
1339 1340 1341 1342 1343
    }

    return ref;
}
/***********************************************************************
1344
 *        EnumMoniker_Next
1345 1346 1347 1348 1349
 */
static HRESULT   WINAPI EnumMonikerImpl_Next(IEnumMoniker* iface, ULONG celt, IMoniker** rgelt, ULONG * pceltFetched)
{
    ULONG i;
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1350 1351
    HRESULT hr = S_OK;

1352
    TRACE("(%p) TabCurrentPos %d Tablastindx %d\n", This, This->pos, This->moniker_list->size);
1353 1354

    /* retrieve the requested number of moniker from the current position */
1355
    for(i = 0; (This->pos < This->moniker_list->size) && (i < celt); i++)
1356 1357
    {
        IStream *stream;
1358
        hr = create_stream_on_mip_ro(This->moniker_list->interfaces[This->pos++], &stream);
1359 1360 1361 1362 1363
        if (hr != S_OK) break;
        hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void **)&rgelt[i]);
        IStream_Release(stream);
        if (hr != S_OK) break;
    }
1364

1365
    if (pceltFetched != NULL)
1366 1367
        *pceltFetched= i;

1368 1369 1370 1371
    if (hr != S_OK)
        return hr;

    if (i == celt)
1372 1373 1374 1375 1376 1377 1378
        return S_OK;
    else
        return S_FALSE;

}

/***********************************************************************
1379
 *        EnumMoniker_Skip
1380 1381 1382 1383 1384 1385 1386
 */
static HRESULT   WINAPI EnumMonikerImpl_Skip(IEnumMoniker* iface, ULONG celt)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;

    TRACE("(%p)\n",This);

1387
    if  (This->pos + celt >= This->moniker_list->size)
1388
        return S_FALSE;
1389

1390
    This->pos += celt;
1391 1392 1393 1394 1395

    return S_OK;
}

/***********************************************************************
1396
 *        EnumMoniker_Reset
1397 1398 1399 1400 1401
 */
static HRESULT   WINAPI EnumMonikerImpl_Reset(IEnumMoniker* iface)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;

1402
    This->pos = 0;	/* set back to start of list */
1403 1404 1405 1406 1407 1408 1409

    TRACE("(%p)\n",This);

    return S_OK;
}

/***********************************************************************
1410
 *        EnumMoniker_Clone
1411 1412 1413 1414
 */
static HRESULT   WINAPI EnumMonikerImpl_Clone(IEnumMoniker* iface, IEnumMoniker ** ppenum)
{
    EnumMonikerImpl *This = (EnumMonikerImpl *)iface;
1415
    InterfaceList *moniker_list;
1416
    ULONG i;
1417 1418

    TRACE("(%p)\n",This);
1419

1420 1421
    *ppenum = NULL;

1422 1423
    moniker_list = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(InterfaceList, interfaces[This->moniker_list->size]));
    if (!moniker_list)
1424 1425
        return E_OUTOFMEMORY;

1426 1427
    moniker_list->size = This->moniker_list->size;
    for (i = 0; i < This->moniker_list->size; i++)
1428
    {
1429 1430 1431
        SIZE_T size = FIELD_OFFSET(InterfaceData, abData[This->moniker_list->interfaces[i]->ulCntData]);
        moniker_list->interfaces[i] = HeapAlloc(GetProcessHeap(), 0, size);
        if (!moniker_list->interfaces[i])
1432 1433 1434
        {
            ULONG end = i;
            for (i = 0; i < end; i++)
1435 1436
                HeapFree(GetProcessHeap(), 0, moniker_list->interfaces[i]);
            HeapFree(GetProcessHeap(), 0, moniker_list);
1437 1438
            return E_OUTOFMEMORY;
        }
1439
        memcpy(moniker_list->interfaces[i], This->moniker_list->interfaces[i], size);
1440 1441
    }

1442
    /* copy the enum structure */ 
1443
    return EnumMonikerImpl_CreateEnumROTMoniker(moniker_list, This->pos, ppenum);
1444 1445 1446
}

/* Virtual function table for the IEnumMoniker class. */
1447
static const IEnumMonikerVtbl VT_EnumMonikerImpl =
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
{
    EnumMonikerImpl_QueryInterface,
    EnumMonikerImpl_AddRef,
    EnumMonikerImpl_Release,
    EnumMonikerImpl_Next,
    EnumMonikerImpl_Skip,
    EnumMonikerImpl_Reset,
    EnumMonikerImpl_Clone
};

/***********************************************************************
 *        EnumMonikerImpl_CreateEnumROTMoniker
 *        Used by EnumRunning to create the structure and EnumClone
 *	  to copy the structure
 */
1463
static HRESULT EnumMonikerImpl_CreateEnumROTMoniker(InterfaceList *moniker_list,
1464 1465
                                                 ULONG current_pos,
                                                 IEnumMoniker **ppenumMoniker)
1466 1467 1468
{
    EnumMonikerImpl* This = NULL;

1469
    if (!ppenumMoniker)
1470 1471 1472
        return E_INVALIDARG;

    This = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumMonikerImpl));
1473
    if (!This) return E_OUTOFMEMORY;
1474 1475 1476 1477 1478 1479 1480 1481

    TRACE("(%p)\n", This);

    /* initialize the virtual table function */
    This->lpVtbl = &VT_EnumMonikerImpl;

    /* the initial reference is set to "1" */
    This->ref = 1;			/* set the ref count to one         */
1482
    This->pos = current_pos;		/* Set the list start posn */
1483
    This->moniker_list = moniker_list;
1484 1485 1486 1487 1488

    *ppenumMoniker =  (IEnumMoniker*)This;

    return S_OK;
}
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498


/* Shared implementation of moniker marshaler based on saving and loading of
 * monikers */

typedef struct MonikerMarshal
{
    const IUnknownVtbl *lpVtbl;
    const IMarshalVtbl *lpVtblMarshal;
    
1499
    LONG ref;
1500 1501 1502
    IMoniker *moniker;
} MonikerMarshal;

1503 1504 1505 1506 1507
static inline MonikerMarshal *impl_from_IMarshal( IMarshal *iface )
{
    return (MonikerMarshal *)((char*)iface - FIELD_OFFSET(MonikerMarshal, lpVtblMarshal));
}

1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
static HRESULT WINAPI MonikerMarshalInner_QueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppv)
{
    MonikerMarshal *This = (MonikerMarshal *)iface;
    TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
    *ppv = NULL;
    if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IMarshal, riid))
    {
        *ppv = &This->lpVtblMarshal;
        IUnknown_AddRef((IUnknown *)&This->lpVtblMarshal);
        return S_OK;
    }
    FIXME("No interface for %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI MonikerMarshalInner_AddRef(IUnknown *iface)
{
    MonikerMarshal *This = (MonikerMarshal *)iface;
    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI MonikerMarshalInner_Release(IUnknown *iface)
{
    MonikerMarshal *This = (MonikerMarshal *)iface;
    ULONG ref = InterlockedDecrement(&This->ref);

    if (!ref) HeapFree(GetProcessHeap(), 0, This);
    return ref;
}

static const IUnknownVtbl VT_MonikerMarshalInner =
{
    MonikerMarshalInner_QueryInterface,
    MonikerMarshalInner_AddRef,
    MonikerMarshalInner_Release
};

static HRESULT WINAPI MonikerMarshal_QueryInterface(IMarshal *iface, REFIID riid, LPVOID *ppv)
{
1547
    MonikerMarshal *This = impl_from_IMarshal(iface);
1548 1549 1550 1551 1552
    return IMoniker_QueryInterface(This->moniker, riid, ppv);
}

static ULONG WINAPI MonikerMarshal_AddRef(IMarshal *iface)
{
1553
    MonikerMarshal *This = impl_from_IMarshal(iface);
1554 1555 1556 1557 1558
    return IMoniker_AddRef(This->moniker);
}

static ULONG WINAPI MonikerMarshal_Release(IMarshal *iface)
{
1559
    MonikerMarshal *This = impl_from_IMarshal(iface);
1560 1561 1562 1563 1564 1565 1566
    return IMoniker_Release(This->moniker);
}

static HRESULT WINAPI MonikerMarshal_GetUnmarshalClass(
  LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
  void* pvDestContext, DWORD mshlflags, CLSID* pCid)
{
1567
    MonikerMarshal *This = impl_from_IMarshal(iface);
1568

1569
    TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1570 1571 1572 1573 1574 1575 1576 1577 1578
        dwDestContext, pvDestContext, mshlflags, pCid);

    return IMoniker_GetClassID(This->moniker, pCid);
}

static HRESULT WINAPI MonikerMarshal_GetMarshalSizeMax(
  LPMARSHAL iface, REFIID riid, void* pv, DWORD dwDestContext,
  void* pvDestContext, DWORD mshlflags, DWORD* pSize)
{
1579
    MonikerMarshal *This = impl_from_IMarshal(iface);
1580 1581 1582
    HRESULT hr;
    ULARGE_INTEGER size;

1583
    TRACE("(%s, %p, %x, %p, %x, %p)\n", debugstr_guid(riid), pv,
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
        dwDestContext, pvDestContext, mshlflags, pSize);

    hr = IMoniker_GetSizeMax(This->moniker, &size);
    if (hr == S_OK)
        *pSize = (DWORD)size.QuadPart;
    return hr;
}

static HRESULT WINAPI MonikerMarshal_MarshalInterface(LPMARSHAL iface, IStream *pStm, 
    REFIID riid, void* pv, DWORD dwDestContext,
    void* pvDestContext, DWORD mshlflags)
{
1596
    MonikerMarshal *This = impl_from_IMarshal(iface);
1597

1598
    TRACE("(%p, %s, %p, %x, %p, %x)\n", pStm, debugstr_guid(riid), pv,
1599 1600 1601 1602 1603 1604 1605
        dwDestContext, pvDestContext, mshlflags);

    return IMoniker_Save(This->moniker, pStm, FALSE);
}

static HRESULT WINAPI MonikerMarshal_UnmarshalInterface(LPMARSHAL iface, IStream *pStm, REFIID riid, void **ppv)
{
1606
    MonikerMarshal *This = impl_from_IMarshal(iface);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
    HRESULT hr;

    TRACE("(%p, %s, %p)\n", pStm, debugstr_guid(riid), ppv);

    hr = IMoniker_Load(This->moniker, pStm);
    if (hr == S_OK)
        hr = IMoniker_QueryInterface(This->moniker, riid, ppv);
    return hr;
}

static HRESULT WINAPI MonikerMarshal_ReleaseMarshalData(LPMARSHAL iface, IStream *pStm)
{
    TRACE("()\n");
    /* can't release a state-based marshal as nothing on server side to
     * release */
    return S_OK;
}

static HRESULT WINAPI MonikerMarshal_DisconnectObject(LPMARSHAL iface, DWORD dwReserved)
{
    TRACE("()\n");
    /* can't disconnect a state-based marshal as nothing on server side to
     * disconnect from */
    return S_OK;
}

static const IMarshalVtbl VT_MonikerMarshal =
{
    MonikerMarshal_QueryInterface,
    MonikerMarshal_AddRef,
    MonikerMarshal_Release,
    MonikerMarshal_GetUnmarshalClass,
    MonikerMarshal_GetMarshalSizeMax,
    MonikerMarshal_MarshalInterface,
    MonikerMarshal_UnmarshalInterface,
    MonikerMarshal_ReleaseMarshalData,
    MonikerMarshal_DisconnectObject
};

HRESULT MonikerMarshal_Create(IMoniker *inner, IUnknown **outer)
{
    MonikerMarshal *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
    if (!This) return E_OUTOFMEMORY;

    This->lpVtbl = &VT_MonikerMarshalInner;
    This->lpVtblMarshal = &VT_MonikerMarshal;
    This->ref = 1;
    This->moniker = inner;

    *outer = (IUnknown *)&This->lpVtbl;
    return S_OK;
}
1659

1660
void * __RPC_USER MIDL_user_allocate(SIZE_T size)
1661 1662 1663 1664 1665 1666 1667 1668
{
    return HeapAlloc(GetProcessHeap(), 0, size);
}

void __RPC_USER MIDL_user_free(void *p)
{
    HeapFree(GetProcessHeap(), 0, p);
}