hlink_main.c 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Implementation of hyperlinking (hlink.dll)
 *
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21
#include "hlink_private.h"
22

23
#include "winreg.h"
24
#include "rpcproxy.h"
25
#include "hlguids.h"
26 27

#include "wine/debug.h"
28

29 30
WINE_DEFAULT_DEBUG_CHANNEL(hlink);

31 32
static HINSTANCE instance;

33
typedef HRESULT (*LPFNCREATEINSTANCE)(IUnknown*, REFIID, LPVOID*);
34 35 36

typedef struct
{
37 38
    IClassFactory      IClassFactory_iface;
    LPFNCREATEINSTANCE lpfnCI;
39
} CFImpl;
40

41 42 43 44 45
static inline CFImpl *impl_from_IClassFactory(IClassFactory *iface)
{
    return CONTAINING_RECORD(iface, CFImpl, IClassFactory_iface);
}

46 47
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
48
    TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
49 50 51 52

    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
53
        instance = hinstDLL;
54 55 56 57 58 59 60 61
        DisableThreadLibraryCalls(hinstDLL);
        break;
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

62 63 64
/***********************************************************************
 *             DllCanUnloadNow (HLINK.@)
 */
65 66 67 68 69
HRESULT WINAPI DllCanUnloadNow( void )
{
    return S_OK;
}

70 71 72
/***********************************************************************
 *             HlinkCreateFromMoniker (HLINK.@)
 */
73 74 75 76
HRESULT WINAPI HlinkCreateFromMoniker( IMoniker *pimkTrgt, LPCWSTR pwzLocation,
        LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
        IUnknown* piunkOuter, REFIID riid, void** ppvObj)
{
77 78
    IHlink *hl = NULL;
    HRESULT r = S_OK;
79

80
    TRACE("%p %s %s %p %i %p %s %p\n", pimkTrgt, debugstr_w(pwzLocation),
81 82 83
            debugstr_w(pwzFriendlyName), pihlsite, dwSiteData, piunkOuter,
            debugstr_guid(riid), ppvObj);

84
    r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
85 86 87
    if (FAILED(r))
        return r;

88 89
    IHlink_SetMonikerReference(hl, HLINKSETF_LOCATION | HLINKSETF_TARGET, pimkTrgt, pwzLocation);

90 91 92 93 94 95 96
    if (pwzFriendlyName)
        IHlink_SetFriendlyName(hl, pwzFriendlyName);
    if (pihlsite)
        IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);

    *ppvObj = hl;

97
    TRACE("Returning %i\n",r);
98 99

    return r;
100 101
}

102 103 104
/***********************************************************************
 *             HlinkCreateFromString (HLINK.@)
 */
105 106 107 108
HRESULT WINAPI HlinkCreateFromString( LPCWSTR pwzTarget, LPCWSTR pwzLocation,
        LPCWSTR pwzFriendlyName, IHlinkSite* pihlsite, DWORD dwSiteData,
        IUnknown* piunkOuter, REFIID riid, void** ppvObj)
{
109 110
    IHlink *hl = NULL;
    HRESULT r = S_OK;
111 112
    WCHAR *hash, *tgt;
    const WCHAR *loc;
113

114
    TRACE("%s %s %s %p %i %p %s %p\n", debugstr_w(pwzTarget),
115 116 117 118 119 120 121
            debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), pihlsite,
            dwSiteData, piunkOuter, debugstr_guid(riid), ppvObj);

    r = CoCreateInstance(&CLSID_StdHlink, piunkOuter, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&hl);
    if (FAILED(r))
        return r;

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    if (pwzTarget)
    {
        hash = strchrW(pwzTarget, '#');
        if (hash)
        {
            if (hash == pwzTarget)
                tgt = NULL;
            else
            {
                int tgt_len = hash - pwzTarget;
                tgt = heap_alloc((tgt_len + 1) * sizeof(WCHAR));
                if (!tgt)
                    return E_OUTOFMEMORY;
                memcpy(tgt, pwzTarget, tgt_len * sizeof(WCHAR));
                tgt[tgt_len] = 0;
            }
            if (!pwzLocation)
                loc = hash + 1;
            else
                loc = pwzLocation;
        }
        else
        {
            tgt = hlink_strdupW(pwzTarget);
            if (!tgt)
                return E_OUTOFMEMORY;
            loc = pwzLocation;
        }
    }
    else
    {
        tgt = NULL;
        loc = pwzLocation;
    }

    IHlink_SetStringReference(hl, HLINKSETF_TARGET | HLINKSETF_LOCATION, tgt, loc);

    heap_free(tgt);
160 161 162

    if (pwzFriendlyName)
        IHlink_SetFriendlyName(hl, pwzFriendlyName);
163

164 165 166
    if (pihlsite)
        IHlink_SetHlinkSite(hl, pihlsite, dwSiteData);

167
    TRACE("Returning %i\n",r);
168 169 170
    *ppvObj = hl;

    return r;
171 172 173
}


174
/***********************************************************************
175
 *             HlinkCreateBrowseContext (HLINK.@)
176
 */
177 178
HRESULT WINAPI HlinkCreateBrowseContext( IUnknown* piunkOuter, REFIID riid, void** ppvObj)
{
179
    HRESULT r = S_OK;
180

181 182 183 184
    TRACE("%p %s %p\n", piunkOuter, debugstr_guid(riid), ppvObj);

    r = CoCreateInstance(&CLSID_StdHlinkBrowseContext, piunkOuter, CLSCTX_INPROC_SERVER, riid, ppvObj);

185
    TRACE("returning %i\n",r);
186 187

    return r;
188 189
}

190 191 192
/***********************************************************************
 *             HlinkNavigate (HLINK.@)
 */
193 194 195 196
HRESULT WINAPI HlinkNavigate(IHlink *phl, IHlinkFrame *phlFrame,
        DWORD grfHLNF, LPBC pbc, IBindStatusCallback *pbsc,
        IHlinkBrowseContext *phlbc)
{
197
    HRESULT r = S_OK;
198

199
    TRACE("%p %p %i %p %p %p\n", phl, phlFrame, grfHLNF, pbc, pbsc, phlbc);
200 201 202 203 204 205 206

    if (phlFrame)
        r = IHlinkFrame_Navigate(phlFrame, grfHLNF, pbc, pbsc, phl);
    else if (phl)
        r = IHlink_Navigate(phl, grfHLNF, pbc, pbsc, phlbc);

    return r;
207 208
}

209 210 211
/***********************************************************************
 *             HlinkOnNavigate (HLINK.@)
 */
212 213 214 215
HRESULT WINAPI HlinkOnNavigate( IHlinkFrame *phlFrame,
        IHlinkBrowseContext* phlbc, DWORD grfHLNF, IMoniker *pmkTarget,
        LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName, ULONG* puHLID)
{
216
    HRESULT r = S_OK;
217

218
    TRACE("%p %p %i %p %s %s %p\n",phlFrame, phlbc, grfHLNF, pmkTarget,
219 220 221 222 223 224 225 226 227 228
            debugstr_w(pwzLocation), debugstr_w(pwzFriendlyName), puHLID);

    r = IHlinkBrowseContext_OnNavigateHlink(phlbc, grfHLNF, pmkTarget,
            pwzLocation, pwzFriendlyName, puHLID);

    if (phlFrame)
        r = IHlinkFrame_OnNavigate(phlFrame,grfHLNF,pmkTarget, pwzLocation,
                pwzFriendlyName, 0);

    return r;
229 230
}

231 232 233
/***********************************************************************
 *             HlinkCreateFromData (HLINK.@)
 */
234 235 236 237
HRESULT WINAPI HlinkCreateFromData(IDataObject *piDataObj,
        IHlinkSite *pihlsite, DWORD dwSiteData, IUnknown *piunkOuter,
        REFIID riid, void **ppvObj)
{
238
    FIXME("%p %p %d %p %p %p\n",
239
          piDataObj, pihlsite, dwSiteData, piunkOuter, riid, ppvObj);
240 241 242
    *ppvObj = NULL;
    return E_NOTIMPL;
}
243

244 245 246
/***********************************************************************
 *             HlinkQueryCreateFromData (HLINK.@)
 */
247 248 249
HRESULT WINAPI HlinkQueryCreateFromData(IDataObject* piDataObj)
{
    FIXME("%p\n", piDataObj);
250 251 252
    return E_NOTIMPL;
}

253 254 255
/***********************************************************************
 *             HlinkNavigateToStringReference (HLINK.@)
 */
256 257 258 259 260
HRESULT WINAPI HlinkNavigateToStringReference( LPCWSTR pwzTarget,
        LPCWSTR pwzLocation, IHlinkSite *pihlsite, DWORD dwSiteData,
        IHlinkFrame *pihlframe, DWORD grfHLNF, LPBC pibc,
        IBindStatusCallback *pibsc, IHlinkBrowseContext *pihlbc)
{
261 262 263
    HRESULT r;
    IHlink *hlink = NULL;

264
    TRACE("%s %s %p %08x %p %08x %p %p %p\n",
265 266
          debugstr_w(pwzTarget), debugstr_w(pwzLocation), pihlsite,
          dwSiteData, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
267 268 269

    r = HlinkCreateFromString( pwzTarget, pwzLocation, NULL, pihlsite,
                               dwSiteData, NULL, &IID_IHlink, (LPVOID*) &hlink );
270
    if (SUCCEEDED(r)) {
271
        r = HlinkNavigate(hlink, pihlframe, grfHLNF, pibc, pibsc, pihlbc);
272 273
        IHlink_Release(hlink);
    }
274 275

    return r;
276 277
}

278 279 280
/***********************************************************************
 *             HlinkIsShortcut (HLINK.@)
 */
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
HRESULT WINAPI HlinkIsShortcut(LPCWSTR pwzFileName)
{
    int len;

    static const WCHAR url_ext[] = {'.','u','r','l',0};

    TRACE("(%s)\n", debugstr_w(pwzFileName));

    if(!pwzFileName)
        return E_INVALIDARG;

    len = strlenW(pwzFileName)-4;
    if(len < 0)
        return S_FALSE;

    return strcmpiW(pwzFileName+len, url_ext) ? S_FALSE : S_OK;
}

299 300 301
/***********************************************************************
 *             HlinkGetSpecialReference (HLINK.@)
 */
302 303
HRESULT WINAPI HlinkGetSpecialReference(ULONG uReference, LPWSTR *ppwzReference)
{
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    DWORD res, type, size = 100;
    LPCWSTR value_name;
    WCHAR *buf;
    HKEY hkey;

    static const WCHAR start_pageW[] = {'S','t','a','r','t',' ','P','a','g','e',0};
    static const WCHAR search_pageW[] = {'S','e','a','r','c','h',' ','P','a','g','e',0};

    static const WCHAR ie_main_keyW[] =
        {'S','o','f','t','w','a','r','e',
         '\\','M','i','c','r','o','s','o','f','t','\\',
         'I','n','t','e','r','n','e','t',' ','E','x','p','l','o','r','e','r',
         '\\','M','a','i','n',0};

    TRACE("(%u %p)\n", uReference, ppwzReference);

    *ppwzReference = NULL;

    switch(uReference) {
    case HLSR_HOME:
        value_name = start_pageW;
        break;
    case HLSR_SEARCHPAGE:
        value_name = search_pageW;
        break;
    case HLSR_HISTORYFOLDER:
        return E_NOTIMPL;
    default:
        return E_INVALIDARG;
    }

    res = RegOpenKeyW(HKEY_CURRENT_USER, ie_main_keyW, &hkey);
    if(res != ERROR_SUCCESS) {
        WARN("Could not open key: %u\n", res);
        return HRESULT_FROM_WIN32(res);
    }

    buf = CoTaskMemAlloc(size);
    res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
    buf = CoTaskMemRealloc(buf, size);
    if(res == ERROR_MORE_DATA)
        res = RegQueryValueExW(hkey, value_name, NULL, &type, (PBYTE)buf, &size);
    RegCloseKey(hkey);
    if(res != ERROR_SUCCESS) {
        WARN("Could not query value %s: %u\n", debugstr_w(value_name), res);
        CoTaskMemFree(buf);
        return HRESULT_FROM_WIN32(res);
    }

    *ppwzReference = buf;
    return S_OK;
355 356
}

357 358 359
/***********************************************************************
 *             HlinkTranslateURL (HLINK.@)
 */
360 361
HRESULT WINAPI HlinkTranslateURL(LPCWSTR pwzURL, DWORD grfFlags, LPWSTR *ppwzTranslatedURL)
{
362
    FIXME("(%s %08x %p)\n", debugstr_w(pwzURL), grfFlags, ppwzTranslatedURL);
363 364 365
    return E_NOTIMPL;
}

366 367 368
/***********************************************************************
 *             HlinkUpdateStackItem (HLINK.@)
 */
369 370 371 372 373 374 375 376
HRESULT WINAPI HlinkUpdateStackItem(IHlinkFrame *pihlframe, IHlinkBrowseContext *pihlbc,
        ULONG uHLID, IMoniker *pimkTrgt, LPCWSTR pwzLocation, LPCWSTR pwzFriendlyName)
{
    FIXME("(%p %p %u %p %s %s)\n", pihlframe, pihlbc, uHLID, pimkTrgt, debugstr_w(pwzLocation),
          debugstr_w(pwzFriendlyName));
    return E_NOTIMPL;
}

377 378 379
/***********************************************************************
 *             HlinkParseDisplayName (HLINK.@)
 */
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
HRESULT WINAPI HlinkParseDisplayName(LPBC pibc, LPCWSTR pwzDisplayName, BOOL fNoForceAbs,
        ULONG *pcchEaten, IMoniker **ppimk)
{
    HRESULT hres;

    TRACE("(%p %s %x %p %p)\n", pibc, debugstr_w(pwzDisplayName), fNoForceAbs, pcchEaten, ppimk);

    if(fNoForceAbs)
        FIXME("Unsupported fNoForceAbs\n");

    hres = MkParseDisplayNameEx(pibc, pwzDisplayName, pcchEaten, ppimk);
    if(SUCCEEDED(hres))
        return hres;

    hres = MkParseDisplayName(pibc, pwzDisplayName, pcchEaten, ppimk);
    if(SUCCEEDED(hres))
        return hres;

    hres = CreateFileMoniker(pwzDisplayName, ppimk);
    if(SUCCEEDED(hres))
        *pcchEaten = strlenW(pwzDisplayName);

    return hres;
}

405 406 407
/***********************************************************************
 *             HlinkResolveMonikerForData (HLINK.@)
 */
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
HRESULT WINAPI HlinkResolveMonikerForData(LPMONIKER pimkReference, DWORD reserved, LPBC pibc,
        ULONG cFmtetc, FORMATETC *rgFmtetc, IBindStatusCallback *pibsc, LPMONIKER pimkBase)
{
    LPOLESTR name = NULL;
    IBindCtx *bctx;
    DWORD mksys = 0;
    void *obj = NULL;
    HRESULT hres;

    TRACE("(%p %x %p %d %p %p %p)\n", pimkReference, reserved, pibc, cFmtetc, rgFmtetc, pibsc, pimkBase);

    if(cFmtetc || rgFmtetc || pimkBase)
        FIXME("Unsupported args\n");

    hres = RegisterBindStatusCallback(pibc, pibsc, NULL /* FIXME */, 0);
    if(FAILED(hres))
        return hres;

    hres = IMoniker_IsSystemMoniker(pimkReference, &mksys);
    if(SUCCEEDED(hres) && mksys != MKSYS_URLMONIKER)
        WARN("sysmk = %x\n", mksys);

    /* FIXME: What is it for? */
    CreateBindCtx(0, &bctx);
    hres = IMoniker_GetDisplayName(pimkReference, bctx, NULL, &name);
    IBindCtx_Release(bctx);
    if(SUCCEEDED(hres)) {
        TRACE("got display name %s\n", debugstr_w(name));
        CoTaskMemFree(name);
    }

    return IMoniker_BindToStorage(pimkReference, pibc, NULL, &IID_IUnknown, &obj);
}
441

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
/***********************************************************************
 *             HlinkClone (HLINK.@)
 */
HRESULT WINAPI HlinkClone(IHlink *hlink, REFIID riid, IHlinkSite *hls,
        DWORD site_data, void **obj)
{
    IMoniker *mk, *clone_mk = NULL;
    WCHAR *loc, *name = NULL;
    HRESULT hres;

    if(!hlink || !riid || !obj)
        return E_INVALIDARG;

    *obj = NULL;

    hres = IHlink_GetMonikerReference(hlink, HLINKGETREF_DEFAULT, &mk, &loc);
    if(FAILED(hres))
        return hres;

    if(mk) {
        IStream *strm;
        LARGE_INTEGER lgint;

        hres = CreateStreamOnHGlobal(NULL, TRUE, &strm);
        if(FAILED(hres)) {
            IMoniker_Release(mk);
            goto cleanup;
        }

        hres = OleSaveToStream((IPersistStream*)mk, strm);
        if(FAILED(hres)) {
            IStream_Release(strm);
            IMoniker_Release(mk);
            goto cleanup;
        }
        IMoniker_Release(mk);

        lgint.QuadPart = 0;
        hres = IStream_Seek(strm, lgint, STREAM_SEEK_SET, NULL);
        if(FAILED(hres)) {
            IStream_Release(strm);
            goto cleanup;
        }

        hres = OleLoadFromStream(strm, &IID_IMoniker, (void**)&clone_mk);
        IStream_Release(strm);
        if(FAILED(hres))
            goto cleanup;
    }

    hres = IHlink_GetFriendlyName(hlink, HLFNAMEF_DEFAULT, &name);
    if(FAILED(hres))
        goto cleanup;

    hres = HlinkCreateFromMoniker(clone_mk, loc, name, hls, site_data, NULL,
            &IID_IHlink, obj);

cleanup:
    if(clone_mk)
        IMoniker_Release(clone_mk);
    CoTaskMemFree(loc);
    CoTaskMemFree(name);
    return hres;
}

507 508 509
static HRESULT WINAPI HLinkCF_fnQueryInterface ( LPCLASSFACTORY iface,
        REFIID riid, LPVOID *ppvObj)
{
510
    CFImpl *This = impl_from_IClassFactory(iface);
511 512

    TRACE("(%p)->(%s)\n",This,debugstr_guid(riid));
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    *ppvObj = NULL;

    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IClassFactory))
    {
        *ppvObj = This;
        return S_OK;
    }

    TRACE("-- E_NOINTERFACE\n");
    return E_NOINTERFACE;
}

static ULONG WINAPI HLinkCF_fnAddRef (LPCLASSFACTORY iface)
{
    return 2;
}

static ULONG WINAPI HLinkCF_fnRelease(LPCLASSFACTORY iface)
{
    return 1;
}

static HRESULT WINAPI HLinkCF_fnCreateInstance( LPCLASSFACTORY iface,
        LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
{
540
    CFImpl *This = impl_from_IClassFactory(iface);
541 542 543 544 545 546 547 548 549 550 551

    TRACE("%p->(%p,%s,%p)\n", This, pUnkOuter, debugstr_guid(riid), ppvObject);

    *ppvObject = NULL;

    return This->lpfnCI(pUnkOuter, riid, ppvObject);
}

static HRESULT WINAPI HLinkCF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
{
    FIXME("%p %d\n", iface, fLock);
552 553 554
    return E_NOTIMPL;
}

555 556 557 558 559 560 561 562 563
static const IClassFactoryVtbl hlcfvt =
{
    HLinkCF_fnQueryInterface,
    HLinkCF_fnAddRef,
    HLinkCF_fnRelease,
    HLinkCF_fnCreateInstance,
    HLinkCF_fnLockServer
};

564 565
static CFImpl HLink_cf = { { &hlcfvt }, HLink_Constructor };
static CFImpl HLinkBrowseContext_cf = { { &hlcfvt }, HLinkBrowseContext_Constructor };
566

567 568 569
/***********************************************************************
 *             DllGetClassObject (HLINK.@)
 */
570 571
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
{
572 573 574
    IClassFactory   *pcf = NULL;

    TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
575 576 577 578 579

    if (!ppv)
        return E_INVALIDARG;
    *ppv = NULL;

580
    if (IsEqualIID(rclsid, &CLSID_StdHlink))
581
        pcf = &HLink_cf.IClassFactory_iface;
582
    else if (IsEqualIID(rclsid, &CLSID_StdHlinkBrowseContext))
583
        pcf = &HLinkBrowseContext_cf.IClassFactory_iface;
584 585 586 587
    else
        return CLASS_E_CLASSNOTAVAILABLE;

    return IClassFactory_QueryInterface(pcf, iid, ppv);
588
}
589

590 591 592 593
/***********************************************************************
 *		DllRegisterServer (HLINK.@)
 */
HRESULT WINAPI DllRegisterServer(void)
594
{
595
    return __wine_register_resources( instance );
596 597
}

598
/***********************************************************************
599
 *		DllUnregisterServer (HLINK.@)
600
 */
601
HRESULT WINAPI DllUnregisterServer(void)
602
{
603
    return __wine_unregister_resources( instance );
604
}