Synchronize with trunk's revision r57629.
[reactos.git] / dll / win32 / urlmon / umon.c
1 /*
2 * UrlMon
3 *
4 * Copyright 1999 Ulrich Czekalla for Corel Corporation
5 * Copyright 2002 Huw D M Davies for CodeWeavers
6 * Copyright 2005 Jacek Caban for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "urlmon_main.h"
24
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "hlink.h"
28 #include "shellapi.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
33
34 typedef struct {
35 IMoniker IMoniker_iface;
36 IUriContainer IUriContainer_iface;
37
38 LONG ref;
39
40 LPOLESTR URLName; /* URL string identified by this URLmoniker */
41 } URLMoniker;
42
43 static inline URLMoniker *impl_from_IMoniker(IMoniker *iface)
44 {
45 return CONTAINING_RECORD(iface, URLMoniker, IMoniker_iface);
46 }
47
48 static HRESULT WINAPI URLMoniker_QueryInterface(IMoniker *iface, REFIID riid, void **ppv)
49 {
50 URLMoniker *This = impl_from_IMoniker(iface);
51
52 if(!ppv)
53 return E_INVALIDARG;
54
55 if(IsEqualIID(&IID_IUnknown, riid)) {
56 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
57 *ppv = iface;
58 }else if(IsEqualIID(&IID_IPersist, riid)) {
59 TRACE("(%p)->(IID_IPersist %p)\n", This, ppv);
60 *ppv = iface;
61 }else if(IsEqualIID(&IID_IPersistStream,riid)) {
62 TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv);
63 *ppv = iface;
64 }else if(IsEqualIID(&IID_IMoniker, riid)) {
65 TRACE("(%p)->(IID_IMoniker %p)\n", This, ppv);
66 *ppv = iface;
67 }else if(IsEqualIID(&IID_IAsyncMoniker, riid)) {
68 TRACE("(%p)->(IID_IAsyncMoniker %p)\n", This, ppv);
69 *ppv = iface;
70 }else if(IsEqualIID(&IID_IUriContainer, riid)) {
71 TRACE("(%p)->(IID_IUriContainer %p)\n", This, ppv);
72 *ppv = &This->IUriContainer_iface;
73 }else {
74 WARN("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppv);
75 *ppv = NULL;
76 return E_NOINTERFACE;
77 }
78
79 IMoniker_AddRef((IUnknown*)*ppv);
80 return S_OK;
81 }
82
83 static ULONG WINAPI URLMoniker_AddRef(IMoniker *iface)
84 {
85 URLMoniker *This = impl_from_IMoniker(iface);
86 ULONG refCount = InterlockedIncrement(&This->ref);
87
88 TRACE("(%p) ref=%u\n",This, refCount);
89
90 return refCount;
91 }
92
93 static ULONG WINAPI URLMoniker_Release(IMoniker *iface)
94 {
95 URLMoniker *This = impl_from_IMoniker(iface);
96 ULONG refCount = InterlockedDecrement(&This->ref);
97
98 TRACE("(%p) ref=%u\n",This, refCount);
99
100 if (!refCount) {
101 heap_free(This->URLName);
102 heap_free(This);
103
104 URLMON_UnlockModule();
105 }
106
107 return refCount;
108 }
109
110 static HRESULT WINAPI URLMoniker_GetClassID(IMoniker *iface, CLSID *pClassID)
111 {
112 URLMoniker *This = impl_from_IMoniker(iface);
113
114 TRACE("(%p,%p)\n", This, pClassID);
115
116 if(!pClassID)
117 return E_POINTER;
118
119 /* Windows always returns CLSID_StdURLMoniker */
120 *pClassID = CLSID_StdURLMoniker;
121 return S_OK;
122 }
123
124 static HRESULT WINAPI URLMoniker_IsDirty(IMoniker *iface)
125 {
126 URLMoniker *This = impl_from_IMoniker(iface);
127
128 TRACE("(%p)\n",This);
129
130 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
131 method in the OLE-provided moniker interfaces always return S_FALSE because
132 their internal state never changes. */
133 return S_FALSE;
134 }
135
136 static HRESULT WINAPI URLMoniker_Load(IMoniker* iface,IStream* pStm)
137 {
138 URLMoniker *This = impl_from_IMoniker(iface);
139 HRESULT res;
140 ULONG size;
141 ULONG got;
142
143 TRACE("(%p,%p)\n",This,pStm);
144
145 if(!pStm)
146 return E_INVALIDARG;
147
148 /*
149 * NOTE
150 * Writes a ULONG containing length of unicode string, followed
151 * by that many unicode characters
152 */
153 res = IStream_Read(pStm, &size, sizeof(ULONG), &got);
154 if(SUCCEEDED(res)) {
155 if(got == sizeof(ULONG)) {
156 heap_free(This->URLName);
157 This->URLName = heap_alloc(size);
158 if(!This->URLName)
159 res = E_OUTOFMEMORY;
160 else {
161 res = IStream_Read(pStm, This->URLName, size, NULL);
162 This->URLName[size/sizeof(WCHAR) - 1] = 0;
163 }
164 }
165 else
166 res = E_FAIL;
167 }
168
169 return res;
170 }
171
172 static HRESULT WINAPI URLMoniker_Save(IMoniker *iface, IStream* pStm, BOOL fClearDirty)
173 {
174 URLMoniker *This = impl_from_IMoniker(iface);
175 HRESULT res;
176 ULONG size;
177
178 TRACE("(%p,%p,%d)\n", This, pStm, fClearDirty);
179
180 if(!pStm)
181 return E_INVALIDARG;
182
183 size = (strlenW(This->URLName) + 1)*sizeof(WCHAR);
184 res=IStream_Write(pStm,&size,sizeof(ULONG),NULL);
185 if(SUCCEEDED(res))
186 res=IStream_Write(pStm,This->URLName,size,NULL);
187
188 return res;
189
190 }
191
192 static HRESULT WINAPI URLMoniker_GetSizeMax(IMoniker* iface, ULARGE_INTEGER *pcbSize)
193 {
194 URLMoniker *This = impl_from_IMoniker(iface);
195
196 TRACE("(%p,%p)\n",This,pcbSize);
197
198 if(!pcbSize)
199 return E_INVALIDARG;
200
201 pcbSize->QuadPart = sizeof(ULONG) + ((strlenW(This->URLName)+1) * sizeof(WCHAR));
202 return S_OK;
203 }
204
205 static HRESULT WINAPI URLMoniker_BindToObject(IMoniker *iface, IBindCtx* pbc, IMoniker *pmkToLeft,
206 REFIID riid, void **ppv)
207 {
208 URLMoniker *This = impl_from_IMoniker(iface);
209 IRunningObjectTable *obj_tbl;
210 IUri *uri;
211 HRESULT hres;
212
213 TRACE("(%p)->(%p,%p,%s,%p): stub\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppv);
214
215 hres = IBindCtx_GetRunningObjectTable(pbc, &obj_tbl);
216 if(SUCCEEDED(hres)) {
217 FIXME("use running object table\n");
218 IRunningObjectTable_Release(obj_tbl);
219 }
220
221 hres = CreateUri(This->URLName, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
222 if(FAILED(hres))
223 return hres;
224
225 hres = bind_to_object(iface, uri, pbc, riid, ppv);
226
227 IUri_Release(uri);
228 return hres;
229 }
230
231 static HRESULT WINAPI URLMoniker_BindToStorage(IMoniker* iface, IBindCtx* pbc,
232 IMoniker* pmkToLeft, REFIID riid, void **ppvObject)
233 {
234 URLMoniker *This = impl_from_IMoniker(iface);
235 IUri *uri;
236 HRESULT hres;
237
238 TRACE("(%p)->(%p %p %s %p)\n", This, pbc, pmkToLeft, debugstr_guid(riid), ppvObject);
239
240 if(ppvObject) *ppvObject = NULL;
241
242 if(!pbc || !ppvObject) return E_INVALIDARG;
243
244 if(pmkToLeft)
245 FIXME("Unsupported pmkToLeft\n");
246
247 hres = CreateUri(This->URLName, Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
248 if(FAILED(hres))
249 return hres;
250
251 hres = bind_to_storage(uri, pbc, riid, ppvObject);
252
253 IUri_Release(uri);
254 return hres;
255 }
256
257 static HRESULT WINAPI URLMoniker_Reduce(IMoniker *iface, IBindCtx *pbc,
258 DWORD dwReduceHowFar, IMoniker **ppmkToLeft, IMoniker **ppmkReduced)
259 {
260 URLMoniker *This = impl_from_IMoniker(iface);
261
262 TRACE("(%p,%p,%d,%p,%p)\n", This, pbc, dwReduceHowFar, ppmkToLeft, ppmkReduced);
263
264 if(!ppmkReduced)
265 return E_INVALIDARG;
266
267 IMoniker_AddRef(iface);
268 *ppmkReduced = iface;
269 return MK_S_REDUCED_TO_SELF;
270 }
271
272 static HRESULT WINAPI URLMoniker_ComposeWith(IMoniker *iface, IMoniker *pmkRight,
273 BOOL fOnlyIfNotGeneric, IMoniker **ppmkComposite)
274 {
275 URLMoniker *This = impl_from_IMoniker(iface);
276 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
277 return E_NOTIMPL;
278 }
279
280 static HRESULT WINAPI URLMoniker_Enum(IMoniker *iface, BOOL fForward, IEnumMoniker **ppenumMoniker)
281 {
282 URLMoniker *This = impl_from_IMoniker(iface);
283
284 TRACE("(%p,%d,%p)\n", This, fForward, ppenumMoniker);
285
286 if(!ppenumMoniker)
287 return E_INVALIDARG;
288
289 /* Does not support sub-monikers */
290 *ppenumMoniker = NULL;
291 return S_OK;
292 }
293
294 static HRESULT WINAPI URLMoniker_IsEqual(IMoniker *iface, IMoniker *pmkOtherMoniker)
295 {
296 URLMoniker *This = impl_from_IMoniker(iface);
297 CLSID clsid;
298 LPOLESTR urlPath;
299 IBindCtx* bind;
300 HRESULT res;
301
302 TRACE("(%p,%p)\n",This, pmkOtherMoniker);
303
304 if(pmkOtherMoniker==NULL)
305 return E_INVALIDARG;
306
307 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
308
309 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
310 return S_FALSE;
311
312 res = CreateBindCtx(0,&bind);
313 if(FAILED(res))
314 return res;
315
316 res = S_FALSE;
317 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
318 int result = lstrcmpiW(urlPath, This->URLName);
319 CoTaskMemFree(urlPath);
320 if(result == 0)
321 res = S_OK;
322 }
323 IUnknown_Release(bind);
324 return res;
325 }
326
327
328 static HRESULT WINAPI URLMoniker_Hash(IMoniker *iface, DWORD *pdwHash)
329 {
330 URLMoniker *This = impl_from_IMoniker(iface);
331 int h = 0,i,skip,len;
332 int off = 0;
333 LPOLESTR val;
334
335 TRACE("(%p,%p)\n",This,pdwHash);
336
337 if(!pdwHash)
338 return E_INVALIDARG;
339
340 val = This->URLName;
341 len = lstrlenW(val);
342
343 if(len < 16) {
344 for(i = len ; i > 0; i--) {
345 h = (h * 37) + val[off++];
346 }
347 }else {
348 /* only sample some characters */
349 skip = len / 8;
350 for(i = len; i > 0; i -= skip, off += skip) {
351 h = (h * 39) + val[off];
352 }
353 }
354 *pdwHash = h;
355 return S_OK;
356 }
357
358 static HRESULT WINAPI URLMoniker_IsRunning(IMoniker* iface, IBindCtx* pbc,
359 IMoniker *pmkToLeft, IMoniker *pmkNewlyRunning)
360 {
361 URLMoniker *This = impl_from_IMoniker(iface);
362 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
363 return E_NOTIMPL;
364 }
365
366 static HRESULT WINAPI URLMoniker_GetTimeOfLastChange(IMoniker *iface,
367 IBindCtx *pbc, IMoniker *pmkToLeft, FILETIME *pFileTime)
368 {
369 URLMoniker *This = impl_from_IMoniker(iface);
370 FIXME("(%p)->(%p,%p,%p): stub\n", This, pbc, pmkToLeft, pFileTime);
371 return E_NOTIMPL;
372 }
373
374 static HRESULT WINAPI URLMoniker_Inverse(IMoniker *iface, IMoniker **ppmk)
375 {
376 URLMoniker *This = impl_from_IMoniker(iface);
377 TRACE("(%p,%p)\n",This,ppmk);
378 return MK_E_NOINVERSE;
379 }
380
381 static HRESULT WINAPI URLMoniker_CommonPrefixWith(IMoniker *iface, IMoniker *pmkOther, IMoniker **ppmkPrefix)
382 {
383 URLMoniker *This = impl_from_IMoniker(iface);
384 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
385 return E_NOTIMPL;
386 }
387
388 static HRESULT WINAPI URLMoniker_RelativePathTo(IMoniker *iface, IMoniker *pmOther, IMoniker **ppmkRelPath)
389 {
390 URLMoniker *This = impl_from_IMoniker(iface);
391 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
392 return E_NOTIMPL;
393 }
394
395 static HRESULT WINAPI URLMoniker_GetDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
396 LPOLESTR *ppszDisplayName)
397 {
398 URLMoniker *This = impl_from_IMoniker(iface);
399 int len;
400
401 TRACE("(%p,%p,%p,%p)\n", This, pbc, pmkToLeft, ppszDisplayName);
402
403 if(!ppszDisplayName)
404 return E_INVALIDARG;
405
406 if(!This->URLName)
407 return E_OUTOFMEMORY;
408
409 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
410 then look at pmkToLeft to try and complete the URL
411 */
412 len = lstrlenW(This->URLName)+1;
413 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
414 if(!*ppszDisplayName)
415 return E_OUTOFMEMORY;
416 lstrcpyW(*ppszDisplayName, This->URLName);
417 return S_OK;
418 }
419
420 static HRESULT WINAPI URLMoniker_ParseDisplayName(IMoniker *iface, IBindCtx *pbc, IMoniker *pmkToLeft,
421 LPOLESTR pszDisplayName, ULONG *pchEaten, IMoniker **ppmkOut)
422 {
423 URLMoniker *This = impl_from_IMoniker(iface);
424 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
425 return E_NOTIMPL;
426 }
427
428 static HRESULT WINAPI URLMoniker_IsSystemMoniker(IMoniker *iface, DWORD *pwdMksys)
429 {
430 URLMoniker *This = impl_from_IMoniker(iface);
431
432 TRACE("(%p,%p)\n",This,pwdMksys);
433
434 if(!pwdMksys)
435 return E_INVALIDARG;
436
437 *pwdMksys = MKSYS_URLMONIKER;
438 return S_OK;
439 }
440
441 static const IMonikerVtbl URLMonikerVtbl =
442 {
443 URLMoniker_QueryInterface,
444 URLMoniker_AddRef,
445 URLMoniker_Release,
446 URLMoniker_GetClassID,
447 URLMoniker_IsDirty,
448 URLMoniker_Load,
449 URLMoniker_Save,
450 URLMoniker_GetSizeMax,
451 URLMoniker_BindToObject,
452 URLMoniker_BindToStorage,
453 URLMoniker_Reduce,
454 URLMoniker_ComposeWith,
455 URLMoniker_Enum,
456 URLMoniker_IsEqual,
457 URLMoniker_Hash,
458 URLMoniker_IsRunning,
459 URLMoniker_GetTimeOfLastChange,
460 URLMoniker_Inverse,
461 URLMoniker_CommonPrefixWith,
462 URLMoniker_RelativePathTo,
463 URLMoniker_GetDisplayName,
464 URLMoniker_ParseDisplayName,
465 URLMoniker_IsSystemMoniker
466 };
467
468 static inline URLMoniker *impl_from_IUriContainer(IUriContainer *iface)
469 {
470 return CONTAINING_RECORD(iface, URLMoniker, IUriContainer_iface);
471 }
472
473 static HRESULT WINAPI UriContainer_QueryInterface(IUriContainer *iface, REFIID riid, void **ppv)
474 {
475 URLMoniker *This = impl_from_IUriContainer(iface);
476 return IMoniker_QueryInterface(&This->IMoniker_iface, riid, ppv);
477 }
478
479 static ULONG WINAPI UriContainer_AddRef(IUriContainer *iface)
480 {
481 URLMoniker *This = impl_from_IUriContainer(iface);
482 return IMoniker_AddRef(&This->IMoniker_iface);
483 }
484
485 static ULONG WINAPI UriContainer_Release(IUriContainer *iface)
486 {
487 URLMoniker *This = impl_from_IUriContainer(iface);
488 return IMoniker_Release(&This->IMoniker_iface);
489 }
490
491 static HRESULT WINAPI UriContainer_GetIUri(IUriContainer *iface, IUri **ppIUri)
492 {
493 URLMoniker *This = impl_from_IUriContainer(iface);
494
495 FIXME("(%p)->(%p)\n", This, ppIUri);
496
497 *ppIUri = NULL;
498 return S_FALSE;
499 }
500
501 static const IUriContainerVtbl UriContainerVtbl = {
502 UriContainer_QueryInterface,
503 UriContainer_AddRef,
504 UriContainer_Release,
505 UriContainer_GetIUri
506 };
507
508 static URLMoniker *alloc_moniker(void)
509 {
510 URLMoniker *ret;
511
512 ret = heap_alloc(sizeof(URLMoniker));
513 if(!ret)
514 return NULL;
515
516 ret->IMoniker_iface.lpVtbl = &URLMonikerVtbl;
517 ret->IUriContainer_iface.lpVtbl = &UriContainerVtbl;
518 ret->ref = 1;
519 ret->URLName = NULL;
520
521 return ret;
522 }
523
524 static HRESULT URLMoniker_Init(URLMoniker *This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
525 {
526 HRESULT hres;
527 DWORD sizeStr = 0;
528
529 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
530
531 This->URLName = heap_alloc(INTERNET_MAX_URL_LENGTH*sizeof(WCHAR));
532
533 if(lpszLeftURLName)
534 hres = CoInternetCombineUrl(lpszLeftURLName, lpszURLName, URL_FILE_USE_PATHURL,
535 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
536 else
537 hres = CoInternetParseUrl(lpszURLName, PARSE_CANONICALIZE, URL_FILE_USE_PATHURL,
538 This->URLName, INTERNET_MAX_URL_LENGTH, &sizeStr, 0);
539
540 if(FAILED(hres)) {
541 heap_free(This->URLName);
542 return hres;
543 }
544
545 URLMON_LockModule();
546
547 if(sizeStr != INTERNET_MAX_URL_LENGTH)
548 This->URLName = heap_realloc(This->URLName, (sizeStr+1)*sizeof(WCHAR));
549
550 TRACE("URLName = %s\n", debugstr_w(This->URLName));
551
552 return S_OK;
553 }
554
555 HRESULT StdURLMoniker_Construct(IUnknown *outer, void **ppv)
556 {
557 TRACE("(%p %p)\n", outer, ppv);
558
559 *ppv = alloc_moniker();
560 return *ppv ? S_OK : E_OUTOFMEMORY;
561 }
562
563 /***********************************************************************
564 * CreateURLMonikerEx (URLMON.@)
565 *
566 * Create a url moniker.
567 *
568 * PARAMS
569 * pmkContext [I] Context
570 * szURL [I] Url to create the moniker for
571 * ppmk [O] Destination for created moniker.
572 * dwFlags [I] Flags.
573 *
574 * RETURNS
575 * Success: S_OK. ppmk contains the created IMoniker object.
576 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
577 * E_OUTOFMEMORY if memory allocation fails.
578 */
579 HRESULT WINAPI CreateURLMonikerEx(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk, DWORD dwFlags)
580 {
581 URLMoniker *obj;
582 HRESULT hres;
583 LPOLESTR lefturl = NULL;
584
585 TRACE("(%p, %s, %p, %08x)\n", pmkContext, debugstr_w(szURL), ppmk, dwFlags);
586
587 if (ppmk)
588 *ppmk = NULL;
589
590 if (!szURL || !ppmk)
591 return E_INVALIDARG;
592
593 if (dwFlags & URL_MK_UNIFORM) FIXME("ignoring flag URL_MK_UNIFORM\n");
594
595 if(!(obj = alloc_moniker()))
596 return E_OUTOFMEMORY;
597
598 if(pmkContext) {
599 IBindCtx* bind;
600 DWORD dwMksys = 0;
601 IMoniker_IsSystemMoniker(pmkContext, &dwMksys);
602 if(dwMksys == MKSYS_URLMONIKER && SUCCEEDED(CreateBindCtx(0, &bind))) {
603 IMoniker_GetDisplayName(pmkContext, bind, NULL, &lefturl);
604 TRACE("lefturl = %s\n", debugstr_w(lefturl));
605 IBindCtx_Release(bind);
606 }
607 }
608
609 hres = URLMoniker_Init(obj, lefturl, szURL);
610 CoTaskMemFree(lefturl);
611 if(SUCCEEDED(hres))
612 hres = URLMoniker_QueryInterface(&obj->IMoniker_iface, &IID_IMoniker, (void**)ppmk);
613 IMoniker_Release(&obj->IMoniker_iface);
614 return hres;
615 }
616
617 /**********************************************************************
618 * CreateURLMoniker (URLMON.@)
619 *
620 * Create a url moniker.
621 *
622 * PARAMS
623 * pmkContext [I] Context
624 * szURL [I] Url to create the moniker for
625 * ppmk [O] Destination for created moniker.
626 *
627 * RETURNS
628 * Success: S_OK. ppmk contains the created IMoniker object.
629 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
630 * E_OUTOFMEMORY if memory allocation fails.
631 */
632 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
633 {
634 return CreateURLMonikerEx(pmkContext, szURL, ppmk, URL_MK_LEGACY);
635 }
636
637 /***********************************************************************
638 * IsAsyncMoniker (URLMON.@)
639 */
640 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
641 {
642 IUnknown *am;
643
644 TRACE("(%p)\n", pmk);
645 if(!pmk)
646 return E_INVALIDARG;
647 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
648 IUnknown_Release(am);
649 return S_OK;
650 }
651 return S_FALSE;
652 }
653
654 /***********************************************************************
655 * BindAsyncMoniker (URLMON.@)
656 *
657 * Bind a bind status callback to an asynchronous URL Moniker.
658 *
659 * PARAMS
660 * pmk [I] Moniker object to bind status callback to
661 * grfOpt [I] Options, seems not used
662 * pbsc [I] Status callback to bind
663 * iidResult [I] Interface to return
664 * ppvResult [O] Resulting asynchronous moniker object
665 *
666 * RETURNS
667 * Success: S_OK.
668 * Failure: E_INVALIDARG, if any argument is invalid, or
669 * E_OUTOFMEMORY if memory allocation fails.
670 */
671 HRESULT WINAPI BindAsyncMoniker(IMoniker *pmk, DWORD grfOpt, IBindStatusCallback *pbsc, REFIID iidResult, LPVOID *ppvResult)
672 {
673 LPBC pbc = NULL;
674 HRESULT hr = E_INVALIDARG;
675
676 TRACE("(%p %08x %p %s %p)\n", pmk, grfOpt, pbsc, debugstr_guid(iidResult), ppvResult);
677
678 if (pmk && ppvResult)
679 {
680 *ppvResult = NULL;
681
682 hr = CreateAsyncBindCtx(0, pbsc, NULL, &pbc);
683 if (hr == NOERROR)
684 {
685 hr = IMoniker_BindToObject(pmk, pbc, NULL, iidResult, ppvResult);
686 IBindCtx_Release(pbc);
687 }
688 }
689 return hr;
690 }
691
692 /***********************************************************************
693 * MkParseDisplayNameEx (URLMON.@)
694 */
695 HRESULT WINAPI MkParseDisplayNameEx(IBindCtx *pbc, LPCWSTR szDisplayName, ULONG *pchEaten, LPMONIKER *ppmk)
696 {
697 TRACE("(%p %s %p %p)\n", pbc, debugstr_w(szDisplayName), pchEaten, ppmk);
698
699 if (!pbc || !szDisplayName || !*szDisplayName || !pchEaten || !ppmk)
700 return E_INVALIDARG;
701
702 if(is_registered_protocol(szDisplayName)) {
703 HRESULT hres;
704
705 hres = CreateURLMoniker(NULL, szDisplayName, ppmk);
706 if(SUCCEEDED(hres)) {
707 *pchEaten = strlenW(szDisplayName);
708 return hres;
709 }
710 }
711
712 return MkParseDisplayName(pbc, szDisplayName, pchEaten, ppmk);
713 }
714
715
716 /***********************************************************************
717 * URLDownloadToCacheFileA (URLMON.@)
718 */
719 HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
720 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
721 {
722 LPWSTR url = NULL, file_name = NULL;
723 int len;
724 HRESULT hres;
725
726 TRACE("(%p %s %p %d %d %p)\n", lpUnkCaller, debugstr_a(szURL), szFileName,
727 dwBufLength, dwReserved, pBSC);
728
729 if(szURL) {
730 len = MultiByteToWideChar(CP_ACP, 0, szURL, -1, NULL, 0);
731 url = heap_alloc(len*sizeof(WCHAR));
732 MultiByteToWideChar(CP_ACP, 0, szURL, -1, url, len);
733 }
734
735 if(szFileName)
736 file_name = heap_alloc(dwBufLength*sizeof(WCHAR));
737
738 hres = URLDownloadToCacheFileW(lpUnkCaller, url, file_name, dwBufLength*sizeof(WCHAR),
739 dwReserved, pBSC);
740
741 if(SUCCEEDED(hres) && file_name)
742 WideCharToMultiByte(CP_ACP, 0, file_name, -1, szFileName, dwBufLength, NULL, NULL);
743
744 heap_free(url);
745 heap_free(file_name);
746
747 return hres;
748 }
749
750 /***********************************************************************
751 * URLDownloadToCacheFileW (URLMON.@)
752 */
753 HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName,
754 DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC)
755 {
756 WCHAR cache_path[MAX_PATH + 1];
757 FILETIME expire, modified;
758 HRESULT hr;
759 LPWSTR ext;
760
761 static WCHAR header[] = {
762 'H','T','T','P','/','1','.','0',' ','2','0','0',' ',
763 'O','K','\\','r','\\','n','\\','r','\\','n',0
764 };
765
766 TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL),
767 szFileName, dwBufLength, dwReserved, pBSC);
768
769 if (!szURL || !szFileName)
770 return E_INVALIDARG;
771
772 ext = PathFindExtensionW(szURL);
773
774 if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0))
775 return E_FAIL;
776
777 hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC);
778 if (FAILED(hr))
779 return hr;
780
781 expire.dwHighDateTime = 0;
782 expire.dwLowDateTime = 0;
783 modified.dwHighDateTime = 0;
784 modified.dwLowDateTime = 0;
785
786 if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY,
787 header, sizeof(header), NULL, NULL))
788 return E_FAIL;
789
790 if (strlenW(cache_path) > dwBufLength)
791 return E_OUTOFMEMORY;
792
793 lstrcpyW(szFileName, cache_path);
794
795 return S_OK;
796 }
797
798 /***********************************************************************
799 * HlinkSimpleNavigateToMoniker (URLMON.@)
800 */
801 HRESULT WINAPI HlinkSimpleNavigateToMoniker(IMoniker *pmkTarget,
802 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
803 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
804 {
805 LPWSTR target;
806 HRESULT hres;
807
808 TRACE("\n");
809
810 hres = IMoniker_GetDisplayName(pmkTarget, pbc, 0, &target);
811 if(hres == S_OK)
812 hres = HlinkSimpleNavigateToString( target, szLocation, szTargetFrameName,
813 pUnk, pbc, pbsc, grfHLNF, dwReserved );
814 CoTaskMemFree(target);
815
816 return hres;
817 }
818
819 /***********************************************************************
820 * HlinkSimpleNavigateToString (URLMON.@)
821 */
822 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
823 LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
824 IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
825 {
826 FIXME("%s %s %s %p %p %p %u %u partial stub\n", debugstr_w( szTarget ), debugstr_w( szLocation ),
827 debugstr_w( szTargetFrameName ), pUnk, pbc, pbsc, grfHLNF, dwReserved);
828
829 /* undocumented: 0 means HLNF_OPENINNEWWINDOW*/
830 if (!grfHLNF) grfHLNF = HLNF_OPENINNEWWINDOW;
831
832 if (grfHLNF == HLNF_OPENINNEWWINDOW)
833 {
834 SHELLEXECUTEINFOW sei;
835 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
836
837 memset(&sei, 0, sizeof(sei));
838 sei.cbSize = sizeof(sei);
839 sei.lpVerb = openW;
840 sei.nShow = SW_SHOWNORMAL;
841 sei.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NO_CONSOLE;
842 sei.lpFile = szTarget;
843
844 if (ShellExecuteExW(&sei)) return S_OK;
845 }
846
847 return E_NOTIMPL;
848 }
849
850 /***********************************************************************
851 * HlinkNavigateString (URLMON.@)
852 */
853 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
854 {
855 TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
856 return HlinkSimpleNavigateToString(
857 szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
858 }
859
860 /***********************************************************************
861 * GetSoftwareUpdateInfo (URLMON.@)
862 */
863 HRESULT WINAPI GetSoftwareUpdateInfo( LPCWSTR szDistUnit, LPSOFTDISTINFO psdi )
864 {
865 FIXME("%s %p\n", debugstr_w(szDistUnit), psdi );
866 return E_FAIL;
867 }
868
869 /***********************************************************************
870 * AsyncInstallDistributionUnit (URLMON.@)
871 */
872 HRESULT WINAPI AsyncInstallDistributionUnit( LPCWSTR szDistUnit, LPCWSTR szTYPE,
873 LPCWSTR szExt, DWORD dwFileVersionMS, DWORD dwFileVersionLS,
874 LPCWSTR szURL, IBindCtx *pbc, LPVOID pvReserved, DWORD flags )
875 {
876 FIXME(": stub\n");
877 return E_NOTIMPL;
878 }