Make the dsound tests run with DirectX < 8.
[wine] / dlls / 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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define COM_NO_WINDOWS_H
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "ole2.h"
37 #include "urlmon.h"
38 #include "wininet.h"
39 #include "shlwapi.h"
40 #include "urlmon_main.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
43
44 /* native urlmon.dll uses this key, too */
45 static const WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
46
47 /*static BOOL registered_wndclass = FALSE;*/
48
49 /* filemoniker data structure */
50 typedef struct URLMonikerImpl{
51
52     IMonikerVtbl*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
53     IBindingVtbl*  lpvtbl2;  /* VTable to IBinding interface */
54
55     ULONG ref; /* reference counter for this object */
56
57     LPOLESTR URLName; /* URL string identified by this URLmoniker */
58
59     HWND hwndCallback;
60     IBindCtx *pBC;
61     HINTERNET hinternet, hconnect, hrequest;
62 } URLMonikerImpl;
63
64 /********************************************************************************/
65 /* URLMoniker prototype functions :                                            */
66
67 /* IUnknown prototype functions */
68 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
69 static ULONG   WINAPI URLMonikerImpl_AddRef(IMoniker* iface);
70 static ULONG   WINAPI URLMonikerImpl_Release(IMoniker* iface);
71
72 /* IPersist prototype functions */
73 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
74
75 /* IPersistStream prototype functions */
76 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface);
77 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface, IStream* pStm);
78 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
79 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
80
81 /* IMoniker prototype functions */
82 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
83 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
84 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
85 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
86 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
87 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
88 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
89 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
90 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
91 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
92 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
93 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
94 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
95 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
96 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
97
98 /* IBinding interface to moniker */
99 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject);
100 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface);
101 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface);
102 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface);
103 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface);
104 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface);
105 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority);
106 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority);
107 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved);
108
109 /* Local function used by urlmoniker implementation */
110 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* iface, LPCOLESTR lpszLeftURL, LPCOLESTR lpszURL);
111 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* iface);
112
113 /********************************************************************************/
114 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
115 /* IPersistStream and IMoniker functions.                                       */
116 static IMonikerVtbl VT_URLMonikerImpl =
117 {
118     URLMonikerImpl_QueryInterface,
119     URLMonikerImpl_AddRef,
120     URLMonikerImpl_Release,
121     URLMonikerImpl_GetClassID,
122     URLMonikerImpl_IsDirty,
123     URLMonikerImpl_Load,
124     URLMonikerImpl_Save,
125     URLMonikerImpl_GetSizeMax,
126     URLMonikerImpl_BindToObject,
127     URLMonikerImpl_BindToStorage,
128     URLMonikerImpl_Reduce,
129     URLMonikerImpl_ComposeWith,
130     URLMonikerImpl_Enum,
131     URLMonikerImpl_IsEqual,
132     URLMonikerImpl_Hash,
133     URLMonikerImpl_IsRunning,
134     URLMonikerImpl_GetTimeOfLastChange,
135     URLMonikerImpl_Inverse,
136     URLMonikerImpl_CommonPrefixWith,
137     URLMonikerImpl_RelativePathTo,
138     URLMonikerImpl_GetDisplayName,
139     URLMonikerImpl_ParseDisplayName,
140     URLMonikerImpl_IsSystemMoniker
141 };
142
143 static IBindingVtbl VTBinding_URLMonikerImpl =
144 {
145     URLMonikerImpl_IBinding_QueryInterface,
146     URLMonikerImpl_IBinding_AddRef,
147     URLMonikerImpl_IBinding_Release,
148     URLMonikerImpl_IBinding_Abort,
149     URLMonikerImpl_IBinding_Suspend,
150     URLMonikerImpl_IBinding_Resume,
151     URLMonikerImpl_IBinding_SetPriority,
152     URLMonikerImpl_IBinding_GetPriority,
153     URLMonikerImpl_IBinding_GetBindResult
154 };
155
156
157 /*******************************************************************************
158  *        URLMoniker_QueryInterface
159  *******************************************************************************/
160 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
161 {
162     URLMonikerImpl *This = (URLMonikerImpl *)iface;
163
164     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
165
166     /* Perform a sanity check on the parameters.*/
167     if ( (This==0) || (ppvObject==0) )
168         return E_INVALIDARG;
169
170     /* Initialize the return parameter */
171     *ppvObject = 0;
172
173     /* Compare the riid with the interface IDs implemented by this object.*/
174     if (IsEqualIID(&IID_IUnknown, riid)      ||
175         IsEqualIID(&IID_IPersist, riid)      ||
176         IsEqualIID(&IID_IPersistStream,riid) ||
177         IsEqualIID(&IID_IMoniker, riid)
178        )
179         *ppvObject = iface;
180
181     /* Check that we obtained an interface.*/
182     if ((*ppvObject)==0)
183         return E_NOINTERFACE;
184
185     /* Query Interface always increases the reference count by one when it is successful */
186     URLMonikerImpl_AddRef(iface);
187
188     return S_OK;
189 }
190
191 /******************************************************************************
192  *        URLMoniker_AddRef
193  ******************************************************************************/
194 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
195 {
196     URLMonikerImpl *This = (URLMonikerImpl *)iface;
197
198     TRACE("(%p)\n",This);
199
200     return ++(This->ref);
201 }
202
203 /******************************************************************************
204  *        URLMoniker_Release
205  ******************************************************************************/
206 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
207 {
208     URLMonikerImpl *This = (URLMonikerImpl *)iface;
209
210     TRACE("(%p)\n",This);
211
212     This->ref--;
213
214     /* destroy the object if there's no more reference on it */
215     if (This->ref==0){
216
217         URLMonikerImpl_Destroy(This);
218
219         return 0;
220     }
221     return This->ref;
222 }
223
224 /******************************************************************************
225  *        URLMoniker_GetClassID
226  ******************************************************************************/
227 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
228                                                 CLSID *pClassID)/* Pointer to CLSID of object */
229 {
230     URLMonikerImpl *This = (URLMonikerImpl *)iface;
231
232     TRACE("(%p,%p)\n",This,pClassID);
233
234     if (pClassID==NULL)
235         return E_POINTER;
236     /* Windows always returns CLSID_StdURLMoniker */
237     *pClassID = CLSID_StdURLMoniker;
238     return S_OK;
239 }
240
241 /******************************************************************************
242  *        URLMoniker_IsDirty
243  ******************************************************************************/
244 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
245 {
246     URLMonikerImpl *This = (URLMonikerImpl *)iface;
247     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
248        method in the OLE-provided moniker interfaces always return S_FALSE because
249        their internal state never changes. */
250
251     TRACE("(%p)\n",This);
252
253     return S_FALSE;
254 }
255
256 /******************************************************************************
257  *        URLMoniker_Load
258  *
259  * NOTE
260  *  Writes a ULONG containing length of unicode string, followed
261  *  by that many unicode characters
262  ******************************************************************************/
263 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
264 {
265     URLMonikerImpl *This = (URLMonikerImpl *)iface;
266     
267     HRESULT res;
268     ULONG len;
269     ULONG got;
270     TRACE("(%p,%p)\n",This,pStm);
271
272     if(!pStm)
273         return E_INVALIDARG;
274
275     res = IStream_Read(pStm, &len, sizeof(ULONG), &got);
276     if(SUCCEEDED(res)) {
277         if(got == sizeof(ULONG)) {
278             if(This->URLName)
279                 HeapFree(GetProcessHeap(), 0, This->URLName);
280             This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(len+1));
281             if(!This->URLName)
282                 res = E_OUTOFMEMORY;
283             else {
284                 res = IStream_Read(pStm, This->URLName, len, NULL);
285                 This->URLName[len] = 0;
286             }
287         }
288         else
289             res = E_FAIL;
290     }
291     return res;
292 }
293
294 /******************************************************************************
295  *        URLMoniker_Save
296  ******************************************************************************/
297 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
298                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
299                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
300 {
301     URLMonikerImpl *This = (URLMonikerImpl *)iface;
302
303     HRESULT res;
304     ULONG len;
305     TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
306
307     if(!pStm)
308         return E_INVALIDARG;
309
310     len = strlenW(This->URLName);
311     res=IStream_Write(pStm,&len,sizeof(ULONG),NULL);
312     if(SUCCEEDED(res))
313         res=IStream_Write(pStm,&This->URLName,len*sizeof(WCHAR),NULL);
314     return res;
315
316 }
317
318 /******************************************************************************
319  *        URLMoniker_GetSizeMax
320  ******************************************************************************/
321 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
322                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
323 {
324     URLMonikerImpl *This = (URLMonikerImpl *)iface;
325
326     TRACE("(%p,%p)\n",This,pcbSize);
327
328     if(!pcbSize)
329         return E_INVALIDARG;
330
331     pcbSize->u.LowPart = sizeof(ULONG) + (strlenW(This->URLName) * sizeof(WCHAR));
332     pcbSize->u.HighPart = 0;
333     return S_OK;
334 }
335
336 /******************************************************************************
337  *         URLMoniker_Construct (local function)
338  *******************************************************************************/
339 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
340 {
341     HRESULT hres;
342     DWORD sizeStr;
343
344     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
345     memset(This, 0, sizeof(*This));
346
347     /* Initialize the virtual function table. */
348     This->lpvtbl1      = &VT_URLMonikerImpl;
349     This->lpvtbl2      = &VTBinding_URLMonikerImpl;
350     This->ref          = 0;
351
352     if(lpszLeftURLName) {
353         hres = UrlCombineW(lpszLeftURLName, lpszURLName, NULL, &sizeStr, 0);
354         if(FAILED(hres)) {
355             return hres;
356         }
357         sizeStr++;
358     }
359     else
360         sizeStr = lstrlenW(lpszURLName)+1;
361
362     This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr));
363
364     if (This->URLName==NULL)
365         return E_OUTOFMEMORY;
366
367     if(lpszLeftURLName) {
368         hres = UrlCombineW(lpszLeftURLName, lpszURLName, This->URLName, &sizeStr, 0);
369         if(FAILED(hres)) {
370             HeapFree(GetProcessHeap(), 0, This->URLName);
371             return hres;
372         }
373     }
374     else
375         strcpyW(This->URLName,lpszURLName);
376
377     return S_OK;
378 }
379
380
381
382 /******************************************************************************
383  *        URLMoniker_Destroy (local function)
384  *******************************************************************************/
385 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* This)
386 {
387     TRACE("(%p)\n",This);
388
389     if (This->URLName!=NULL)
390             HeapFree(GetProcessHeap(),0,This->URLName);
391
392     HeapFree(GetProcessHeap(),0,This);
393
394     return S_OK;
395 }
396
397 /******************************************************************************
398  *                  URLMoniker_BindToObject
399  ******************************************************************************/
400 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
401                                                   IBindCtx* pbc,
402                                                   IMoniker* pmkToLeft,
403                                                   REFIID riid,
404                                                   VOID** ppvResult)
405 {
406     URLMonikerImpl *This = (URLMonikerImpl *)iface;
407
408     *ppvResult=0;
409
410     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
411           ppvResult);
412
413     return E_NOTIMPL;
414 }
415
416 typedef struct {
417     enum {OnProgress, OnDataAvailable} callback;
418 } URLMON_CallbackData;
419
420
421 #if 0
422 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
423 {
424     return DefWindowProcA(hwnd, msg, wparam, lparam);
425 }
426
427 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
428 {
429 }
430
431 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
432                                              void *status_info, DWORD status_info_len)
433 {
434     URLMonikerImpl *This = (URLMonikerImpl *)context;
435     TRACE("handle %p this %p status %08lx\n", hinet, This, status);
436
437     if(This->filesize == -1) {
438         switch(status) {
439         case INTERNET_STATUS_RESOLVING_NAME:
440             PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
441             break;
442         case INTERNET_STATUS_CONNECTING_TO_SERVER:
443             PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
444             break;
445         case INTERNET_STATUS_SENDING_REQUEST:
446             PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
447             break;
448         case INTERNET_REQUEST_COMPLETE:
449           {
450               DWORD len, lensz = sizeof(len);
451
452             HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
453             TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
454             This->filesize = len;
455             break;
456           }
457         }
458     }
459
460     return;
461 }
462 #endif
463 /******************************************************************************
464  *        URLMoniker_BindToStorage
465  ******************************************************************************/
466 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
467                                                    IBindCtx* pbc,
468                                                    IMoniker* pmkToLeft,
469                                                    REFIID riid,
470                                                    VOID** ppvObject)
471 {
472     URLMonikerImpl *This = (URLMonikerImpl *)iface;
473     HRESULT hres;
474     IBindStatusCallback *pbscb;
475     BINDINFO bi;
476     DWORD bindf;
477
478     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
479     if(pmkToLeft) {
480         FIXME("pmkToLeft != NULL\n");
481         return E_NOTIMPL;
482     }
483     if(!IsEqualIID(&IID_IStream, riid)) {
484         FIXME("unsupported iid\n");
485         return E_NOTIMPL;
486     }
487
488     /* FIXME This is a bad hack (tm).  We should clearly download to a temporary file.
489        We also need to implement IStream ourselves so that IStream_Read can return
490        E_PENDING */
491
492     hres = CreateStreamOnHGlobal(0, TRUE, (IStream**)ppvObject);
493
494
495     if(SUCCEEDED(hres)) {
496         TRACE("Created dummy stream...\n");
497
498         hres = IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown**)&pbscb);
499         if(SUCCEEDED(hres)) {
500             TRACE("Got IBindStatusCallback...\n");
501
502             memset(&bi, 0, sizeof(bi));
503             bi.cbSize = sizeof(bi);
504             bindf = 0;
505             hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
506             if(SUCCEEDED(hres)) {
507                 URL_COMPONENTSW url;
508                 WCHAR *host, *path;
509                 DWORD len, lensz = sizeof(len), total_read = 0;
510                 LARGE_INTEGER last_read_pos;
511                 FORMATETC fmt;
512                 STGMEDIUM stg;
513
514                 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
515                       bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
516                 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
517                 TRACE("OnStartBinding rets %08lx\n", hres);
518
519 #if 0
520                 if(!registered_wndclass) {
521                     WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
522                     RegisterClassA(&urlmon_wndclass);
523                     registered_wndclass = TRUE;
524                 }
525
526                 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
527                                                    URLMON_hInstance, NULL);
528
529 #endif
530                 memset(&url, 0, sizeof(url));
531                 url.dwStructSize = sizeof(url);
532                 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
533                 InternetCrackUrlW(This->URLName, 0, 0, &url);
534                 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
535                 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
536                 host[url.dwHostNameLength] = '\0';
537                 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
538                 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
539                 path[url.dwUrlPathLength] = '\0';
540
541                 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
542 /*              InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
543
544                 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
545                                             INTERNET_SERVICE_HTTP, 0, (DWORD)This);
546                 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
547
548                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
549                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
550                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
551                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
552                 hres = E_OUTOFMEMORY; /* FIXME */
553                 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
554
555                     len = 0;
556                     HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
557
558                     TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
559
560                     last_read_pos.u.LowPart = last_read_pos.u.HighPart = 0;
561                     fmt.cfFormat = 0;
562                     fmt.ptd = NULL;
563                     fmt.dwAspect = 0;
564                     fmt.lindex = -1;
565                     fmt.tymed = TYMED_ISTREAM;
566                     stg.tymed = TYMED_ISTREAM;
567                     stg.u.pstm = *(IStream**)ppvObject;
568                     stg.pUnkForRelease = NULL;
569
570                     while(1) {
571                         char buf[4096];
572                         DWORD bufread;
573                         DWORD written;
574                         if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
575                             TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
576                             if(bufread == 0) break;
577                             IStream_Write(*(IStream**)ppvObject, buf, bufread, &written);
578                             total_read += bufread;
579                             IStream_Seek(*(IStream**)ppvObject, last_read_pos, STREAM_SEEK_SET, NULL);
580                             hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
581                                                                   BINDSTATUS_BEGINDOWNLOADDATA :
582                                                                   BINDSTATUS_DOWNLOADINGDATA, NULL);
583                             hres = IBindStatusCallback_OnDataAvailable(pbscb,
584                                                                        (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
585                                                                        BSCF_INTERMEDIATEDATANOTIFICATION,
586                                                                        total_read, &fmt, &stg);
587                             last_read_pos.u.LowPart += bufread; /* FIXME */
588                         } else
589                             break;
590                     }
591                     hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
592                     hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
593                     TRACE("OnDataAvail rets %08lx\n", hres);
594                     hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
595                     TRACE("OnStop rets %08lx\n", hres);
596                     hres = S_OK;
597                 }
598                 InternetCloseHandle(This->hrequest);
599                 InternetCloseHandle(This->hconnect);
600                 InternetCloseHandle(This->hinternet);
601             }
602         }
603     }
604     return hres;
605 }
606
607 /******************************************************************************
608  *        URLMoniker_Reduce
609  ******************************************************************************/
610 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
611                                             IBindCtx* pbc,
612                                             DWORD dwReduceHowFar,
613                                             IMoniker** ppmkToLeft,
614                                             IMoniker** ppmkReduced)
615 {
616     URLMonikerImpl *This = (URLMonikerImpl *)iface;
617     
618     TRACE("(%p,%p,%ld,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
619
620     if(!ppmkReduced)
621         return E_INVALIDARG;
622
623     URLMonikerImpl_AddRef(iface);
624     *ppmkReduced = iface;
625     return MK_S_REDUCED_TO_SELF;
626 }
627
628 /******************************************************************************
629  *        URLMoniker_ComposeWith
630  ******************************************************************************/
631 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
632                                                  IMoniker* pmkRight,
633                                                  BOOL fOnlyIfNotGeneric,
634                                                  IMoniker** ppmkComposite)
635 {
636     URLMonikerImpl *This = (URLMonikerImpl *)iface;
637     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
638
639     return E_NOTIMPL;
640 }
641
642 /******************************************************************************
643  *        URLMoniker_Enum
644  ******************************************************************************/
645 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
646 {
647     URLMonikerImpl *This = (URLMonikerImpl *)iface;
648     TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
649
650     if(!ppenumMoniker)
651         return E_INVALIDARG;
652
653     /* Does not support sub-monikers */
654     *ppenumMoniker = NULL;
655     return S_OK;
656 }
657
658 /******************************************************************************
659  *        URLMoniker_IsEqual
660  ******************************************************************************/
661 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
662 {
663     URLMonikerImpl *This = (URLMonikerImpl *)iface;
664     CLSID clsid;
665     LPOLESTR urlPath;
666     IBindCtx* bind;
667     HRESULT res;
668
669     TRACE("(%p,%p)\n",This,pmkOtherMoniker);
670
671     if(pmkOtherMoniker==NULL)
672         return E_INVALIDARG;
673
674     IMoniker_GetClassID(pmkOtherMoniker,&clsid);
675
676     if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
677         return S_FALSE;
678
679     res = CreateBindCtx(0,&bind);
680     if(FAILED(res))
681         return res;
682
683     res = S_FALSE;
684     if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
685         int result = lstrcmpiW(urlPath, This->URLName);
686         CoTaskMemFree(urlPath);
687         if(result == 0)
688             res = S_OK;
689     }
690     IUnknown_Release(bind);
691     return res;
692 }
693
694
695 /******************************************************************************
696  *        URLMoniker_Hash
697  ******************************************************************************/
698 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
699 {
700     URLMonikerImpl *This = (URLMonikerImpl *)iface;
701     
702     int  h = 0,i,skip,len;
703     int  off = 0;
704     LPOLESTR val;
705
706     TRACE("(%p,%p)\n",This,pdwHash);
707
708     if(!pdwHash)
709         return E_INVALIDARG;
710
711     val = This->URLName;
712     len = lstrlenW(val);
713
714     if(len < 16) {
715         for(i = len ; i > 0; i--) {
716             h = (h * 37) + val[off++];
717         }
718     }
719     else {
720         /* only sample some characters */
721         skip = len / 8;
722         for(i = len; i > 0; i -= skip, off += skip) {
723             h = (h * 39) + val[off];
724         }
725     }
726     *pdwHash = h;
727     return S_OK;
728 }
729
730 /******************************************************************************
731  *        URLMoniker_IsRunning
732  ******************************************************************************/
733 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
734                                                IBindCtx* pbc,
735                                                IMoniker* pmkToLeft,
736                                                IMoniker* pmkNewlyRunning)
737 {
738     URLMonikerImpl *This = (URLMonikerImpl *)iface;
739     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
740
741     return E_NOTIMPL;
742 }
743
744 /******************************************************************************
745  *        URLMoniker_GetTimeOfLastChange
746  ******************************************************************************/
747 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
748                                                          IBindCtx* pbc,
749                                                          IMoniker* pmkToLeft,
750                                                          FILETIME* pFileTime)
751 {
752     URLMonikerImpl *This = (URLMonikerImpl *)iface;
753     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
754
755     return E_NOTIMPL;
756 }
757
758 /******************************************************************************
759  *        URLMoniker_Inverse
760  ******************************************************************************/
761 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
762 {
763     URLMonikerImpl *This = (URLMonikerImpl *)iface;
764     TRACE("(%p,%p)\n",This,ppmk);
765
766     return MK_E_NOINVERSE;
767 }
768
769 /******************************************************************************
770  *        URLMoniker_CommonPrefixWith
771  ******************************************************************************/
772 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
773 {
774     URLMonikerImpl *This = (URLMonikerImpl *)iface;
775     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
776
777     return E_NOTIMPL;
778 }
779
780 /******************************************************************************
781  *        URLMoniker_RelativePathTo
782  ******************************************************************************/
783 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
784 {
785     URLMonikerImpl *This = (URLMonikerImpl *)iface;
786     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
787
788     return E_NOTIMPL;
789 }
790
791 /******************************************************************************
792  *        URLMoniker_GetDisplayName
793  ******************************************************************************/
794 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
795                                                     IBindCtx* pbc,
796                                                     IMoniker* pmkToLeft,
797                                                     LPOLESTR *ppszDisplayName)
798 {
799     URLMonikerImpl *This = (URLMonikerImpl *)iface;
800     
801     int len;
802     
803     TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
804     
805     if(!ppszDisplayName)
806         return E_INVALIDARG;
807     
808     /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
809         then look at pmkToLeft to try and complete the URL
810     */
811     len = lstrlenW(This->URLName)+1;
812     *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
813     if(!*ppszDisplayName)
814         return E_OUTOFMEMORY;
815     lstrcpyW(*ppszDisplayName, This->URLName);
816     return S_OK;
817 }
818
819 /******************************************************************************
820  *        URLMoniker_ParseDisplayName
821  ******************************************************************************/
822 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
823                                                       IBindCtx* pbc,
824                                                       IMoniker* pmkToLeft,
825                                                       LPOLESTR pszDisplayName,
826                                                       ULONG* pchEaten,
827                                                       IMoniker** ppmkOut)
828 {
829     URLMonikerImpl *This = (URLMonikerImpl *)iface;
830     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
831
832     return E_NOTIMPL;
833 }
834
835 /******************************************************************************
836  *        URLMoniker_IsSystemMoniker
837  ******************************************************************************/
838 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
839 {
840     URLMonikerImpl *This = (URLMonikerImpl *)iface;
841     TRACE("(%p,%p)\n",This,pwdMksys);
842
843     if(!pwdMksys)
844         return E_INVALIDARG;
845
846     *pwdMksys = MKSYS_URLMONIKER;
847     return S_OK;
848 }
849
850 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
851 {
852     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
853
854     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
855
856     /* Perform a sanity check on the parameters.*/
857     if ( (This==0) || (ppvObject==0) )
858         return E_INVALIDARG;
859
860     /* Initialize the return parameter */
861     *ppvObject = 0;
862
863     /* Compare the riid with the interface IDs implemented by this object.*/
864     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
865         *ppvObject = iface;
866
867     /* Check that we obtained an interface.*/
868     if ((*ppvObject)==0)
869         return E_NOINTERFACE;
870
871     /* Query Interface always increases the reference count by one when it is successful */
872     URLMonikerImpl_IBinding_AddRef(iface);
873
874     return S_OK;
875
876 }
877
878 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
879 {
880     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
881     TRACE("(%p)\n",This);
882
883     return URLMonikerImpl_AddRef((IMoniker*)This);
884 }
885
886 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
887 {
888     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
889     TRACE("(%p)\n",This);
890
891     return URLMonikerImpl_Release((IMoniker*)This);
892 }
893
894 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
895 {
896     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
897     FIXME("(%p): stub\n", This);
898
899     return E_NOTIMPL;
900 }
901
902 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
903 {
904     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
905     FIXME("(%p)->(%s, %p, %p, %p): stub\n", This, debugstr_guid(pclsidProtocol), pdwResult, pszResult, pdwReserved);
906
907     return E_NOTIMPL;
908 }
909
910 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
911 {
912     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
913     FIXME("(%p)->(%p): stub\n", This, pnPriority);
914
915     return E_NOTIMPL;
916 }
917
918 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
919 {
920     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
921     FIXME("(%p): stub\n", This);
922
923     return E_NOTIMPL;
924 }
925
926 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
927 {
928     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
929     FIXME("(%p)->(%ld): stub\n", This, nPriority);
930
931     return E_NOTIMPL;
932 }
933
934 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
935 {
936     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
937     FIXME("(%p): stub\n", This);
938
939     return E_NOTIMPL;
940 }
941
942 /***********************************************************************
943  *           CreateAsyncBindCtx (URLMON.@)
944  */
945 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
946     IEnumFORMATETC *format, IBindCtx **pbind)
947 {
948     FIXME("stub.\n");
949     return E_INVALIDARG;
950 }
951 /***********************************************************************
952  *           CreateAsyncBindCtxEx (URLMON.@)
953  *
954  * Create an asynchronous bind context.
955  *
956  * FIXME
957  *   Not implemented.
958  */
959 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
960     IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
961     DWORD reserved)
962 {
963      FIXME("stub, returns failure\n");
964      return E_INVALIDARG;
965 }
966
967
968 /***********************************************************************
969  *           CreateURLMoniker (URLMON.@)
970  *
971  * Create a url moniker.
972  *
973  * PARAMS
974  *    pmkContext [I] Context
975  *    szURL      [I] Url to create the moniker for
976  *    ppmk       [O] Destination for created moniker.
977  *
978  * RETURNS
979  *    Success: S_OK. ppmk contains the created IMoniker object.
980  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
981  *             E_OUTOFMEMORY if memory allocation fails.
982  */
983 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
984 {
985     URLMonikerImpl *obj;
986     HRESULT hres;
987     IID iid = IID_IMoniker;
988     LPOLESTR lefturl = NULL;
989
990     TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
991
992     if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
993         return E_OUTOFMEMORY;
994
995     if(pmkContext) {
996         CLSID clsid;
997         IBindCtx* bind;
998         IMoniker_GetClassID(pmkContext, &clsid);
999         if(IsEqualCLSID(&clsid, &CLSID_StdURLMoniker) && SUCCEEDED(CreateBindCtx(0, &bind))) {
1000             URLMonikerImpl_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1001             IBindCtx_Release(bind);
1002         }
1003     }
1004         
1005     hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1006     CoTaskMemFree(lefturl);
1007     if(SUCCEEDED(hres))
1008         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
1009     else
1010         HeapFree(GetProcessHeap(), 0, obj);
1011     return hres;
1012 }
1013
1014
1015 /***********************************************************************
1016  *           CoInternetGetSession (URLMON.@)
1017  *
1018  * Create a new internet session and return an IInternetSession interface
1019  * representing it.
1020  *
1021  * PARAMS
1022  *    dwSessionMode      [I] Mode for the internet session
1023  *    ppIInternetSession [O] Destination for creates IInternetSession object
1024  *    dwReserved         [I] Reserved, must be 0.
1025  *
1026  * RETURNS
1027  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
1028  *    Failure: E_INVALIDARG, if any argument is invalid, or
1029  *             E_OUTOFMEMORY if memory allocation fails.
1030  */
1031 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession, DWORD dwReserved)
1032 {
1033     FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
1034
1035     if(dwSessionMode) {
1036       ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
1037     }
1038
1039     if(dwReserved) {
1040       ERR("dwReserved: %ld, must be zero\n", dwReserved);
1041     }
1042
1043     *ppIInternetSession=NULL;
1044     return E_OUTOFMEMORY;
1045 }
1046
1047 /***********************************************************************
1048  *           CoInternetQueryInfo (URLMON.@)
1049  *
1050  * Retrieves information relevant to a specified URL
1051  *
1052  * RETURNS
1053  *    S_OK                      success
1054  *    S_FALSE                   buffer too small
1055  *    INET_E_QUERYOPTIONUNKNOWN invalid option
1056  *
1057  */
1058 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1059   DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1060   DWORD dwReserved)
1061 {
1062   FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
1063     QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1064   return S_OK;
1065 }
1066
1067 /***********************************************************************
1068  *           CoInternetCreateSecurityManager (URLMON.@)
1069  *
1070  */
1071 typedef void *IInternetSecurityManager;
1072 HRESULT CoInternetCreateSecurityManager( IServiceProvider *pSP,
1073     IInternetSecurityManager **ppSM, DWORD dwReserved )
1074 {
1075   FIXME("%p %ld\n", pSP, dwReserved );
1076   return E_NOTIMPL;
1077 }
1078
1079 static BOOL URLMON_IsBinary(LPVOID pBuffer, DWORD cbSize)
1080 {
1081     int binarycount = 0;
1082     int i;
1083     unsigned char *buff = pBuffer;
1084     for(i=0; i<cbSize; i++) {
1085         if(buff[i] < 32)
1086             binarycount++;
1087     }
1088     return binarycount > (cbSize-binarycount);
1089 }
1090
1091 /***********************************************************************
1092  *           FindMimeFromData (URLMON.@)
1093  *
1094  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
1095  *
1096  * NOTE
1097  *  See http://msdn.microsoft.com/workshop/networking/moniker/overview/appendix_a.asp
1098  */
1099 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
1100    DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
1101    LPWSTR* ppwzMimeOut, DWORD dwReserved)
1102 {
1103     static const WCHAR szBinaryMime[] = {'a','p','p','l','i','c','a','t','i','o','n','/','o','c','t','e','t','-','s','t','r','e','a','m','\0'};
1104     static const WCHAR szTextMime[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
1105     static const WCHAR szContentType[] = {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
1106     WCHAR szTmpMime[256];
1107     LPCWSTR mimeType = NULL;
1108     HKEY hKey = NULL;
1109
1110     TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
1111           debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
1112
1113     if((!pwzUrl && (!pBuffer || cbSize <= 0)) || !ppwzMimeOut)
1114         return E_INVALIDARG;
1115
1116     if(pwzMimeProposed)
1117         mimeType = pwzMimeProposed;
1118     else {
1119         /* Try and find the mime type in the registry */
1120         if(pwzUrl) {
1121             LPWSTR ext = strrchrW(pwzUrl, '.');
1122             if(ext) {
1123                 DWORD dwSize;
1124                 if(!RegOpenKeyExW(HKEY_CLASSES_ROOT, ext, 0, 0, &hKey)) {
1125                     if(!RegQueryValueExW(hKey, szContentType, NULL, NULL, (LPBYTE)szTmpMime, &dwSize)) {
1126                         mimeType = szTmpMime;
1127                     }
1128                     RegCloseKey(hKey);
1129                 }
1130             }
1131         }
1132     }
1133     if(!mimeType && pBuffer && cbSize > 0)
1134         mimeType = URLMON_IsBinary(pBuffer, cbSize)?szBinaryMime:szTextMime;
1135
1136     TRACE("Using %s\n", debugstr_w(mimeType));
1137     *ppwzMimeOut = CoTaskMemAlloc((lstrlenW(mimeType)+1)*sizeof(WCHAR));
1138     if(!*ppwzMimeOut) return E_OUTOFMEMORY;
1139     lstrcpyW(*ppwzMimeOut, mimeType);
1140     return S_OK;
1141 }
1142
1143 /***********************************************************************
1144  *           IsAsyncMoniker (URLMON.@)
1145  */
1146 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1147 {
1148     IUnknown *am;
1149     
1150     TRACE("(%p)\n", pmk);
1151     if(!pmk)
1152         return E_INVALIDARG;
1153     if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1154         IUnknown_Release(am);
1155         return S_OK;
1156     }
1157     return S_FALSE;
1158 }
1159
1160 /***********************************************************************
1161  *           RegisterBindStatusCallback (URLMON.@)
1162  *
1163  * Register a bind status callback.
1164  *
1165  * PARAMS
1166  *  pbc           [I] Binding context
1167  *  pbsc          [I] Callback to register
1168  *  ppbscPrevious [O] Destination for previous callback
1169  *  dwReserved    [I] Reserved, must be 0.
1170  *
1171  * RETURNS
1172  *    Success: S_OK.
1173  *    Failure: E_INVALIDARG, if any argument is invalid, or
1174  *             E_OUTOFMEMORY if memory allocation fails.
1175  */
1176 HRESULT WINAPI RegisterBindStatusCallback(
1177     IBindCtx *pbc,
1178     IBindStatusCallback *pbsc,
1179     IBindStatusCallback **ppbscPrevious,
1180     DWORD dwReserved)
1181 {
1182     IBindStatusCallback *prev;
1183
1184         TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1185
1186     if (pbc == NULL || pbsc == NULL)
1187         return E_INVALIDARG;
1188
1189     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1190     {
1191         IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1192         if (ppbscPrevious)
1193             *ppbscPrevious = prev;
1194         else
1195             IBindStatusCallback_Release(prev);
1196     }
1197
1198         return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1199 }
1200
1201 /***********************************************************************
1202  *           RevokeBindStatusCallback (URLMON.@)
1203  *
1204  * Unregister a bind status callback.
1205  *
1206  *  pbc           [I] Binding context
1207  *  pbsc          [I] Callback to unregister
1208  *
1209  * RETURNS
1210  *    Success: S_OK.
1211  *    Failure: E_INVALIDARG, if any argument is invalid, or
1212  *             E_FAIL if pbsc wasn't registered with pbc.
1213  */
1214 HRESULT WINAPI RevokeBindStatusCallback(
1215     IBindCtx *pbc,
1216     IBindStatusCallback *pbsc)
1217 {
1218     IBindStatusCallback *callback;
1219     HRESULT hr = E_FAIL;
1220
1221         TRACE("(%p,%p)\n", pbc, pbsc);
1222
1223     if (pbc == NULL || pbsc == NULL)
1224         return E_INVALIDARG;
1225
1226     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1227     {
1228         if (callback == pbsc)
1229         {
1230             IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1231             hr = S_OK;
1232         }
1233         IBindStatusCallback_Release(pbsc);
1234     }
1235
1236     return hr;
1237 }
1238
1239 /***********************************************************************
1240  *           ReleaseBindInfo (URLMON.@)
1241  *
1242  * Release the resources used by the specified BINDINFO structure.
1243  *
1244  * PARAMS
1245  *  pbindinfo [I] BINDINFO to release.
1246  *
1247  * RETURNS
1248  *  Nothing.
1249  */
1250 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1251 {
1252     FIXME("(%p)stub!\n", pbindinfo);
1253 }
1254
1255 /***********************************************************************
1256  *           URLDownloadToFileA (URLMON.@)
1257  *
1258  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1259  * report progress.
1260  *
1261  * PARAMS
1262  *  pCaller    [I] controlling IUnknown interface.
1263  *  szURL      [I] URL of the file to download
1264  *  szFileName [I] file name to store the content of the URL
1265  *  dwReserved [I] reserved - set to 0
1266  *  lpfnCB     [I] callback for progress report
1267  *
1268  * RETURNS
1269  *  S_OK on success
1270  *  E_OUTOFMEMORY when going out of memory
1271  */
1272 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1273                                   LPCSTR szURL,
1274                                   LPCSTR szFileName,
1275                                   DWORD dwReserved,
1276                                   LPBINDSTATUSCALLBACK lpfnCB)
1277 {
1278     UNICODE_STRING szURL_w, szFileName_w;
1279
1280     if ((szURL == NULL) || (szFileName == NULL)) {
1281         FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1282         return E_INVALIDARG; /* The error code is not specified in this case... */
1283     }
1284     
1285     if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1286         if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1287             HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1288
1289             RtlFreeUnicodeString(&szURL_w);
1290             RtlFreeUnicodeString(&szFileName_w);
1291             
1292             return ret;
1293         } else {
1294             RtlFreeUnicodeString(&szURL_w);
1295         }
1296     }
1297     
1298     FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1299     return E_OUTOFMEMORY;
1300 }
1301
1302 /***********************************************************************
1303  *           URLDownloadToFileW (URLMON.@)
1304  *
1305  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1306  * report progress.
1307  *
1308  * PARAMS
1309  *  pCaller    [I] controlling IUnknown interface.
1310  *  szURL      [I] URL of the file to download
1311  *  szFileName [I] file name to store the content of the URL
1312  *  dwReserved [I] reserved - set to 0
1313  *  lpfnCB     [I] callback for progress report
1314  *
1315  * RETURNS
1316  *  S_OK on success
1317  *  E_OUTOFMEMORY when going out of memory
1318  */
1319 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1320                                   LPCWSTR szURL,
1321                                   LPCWSTR szFileName,
1322                                   DWORD dwReserved,
1323                                   LPBINDSTATUSCALLBACK lpfnCB)
1324 {
1325     HINTERNET hinternet, hcon, hreq;
1326     BOOL r;
1327     CHAR buffer[0x1000];
1328     DWORD sz, total, written;
1329     DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1330     URL_COMPONENTSW url;
1331     WCHAR host[0x80], path[0x100];
1332     HANDLE hfile;
1333     static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1334
1335     /* Note: all error codes would need to be checked agains real Windows behaviour... */
1336     TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1337
1338     if ((szURL == NULL) || (szFileName == NULL)) {
1339         FIXME(" cannot accept NULL strings !\n");
1340         return E_INVALIDARG;
1341     }
1342
1343     /* Would be better to use the application name here rather than 'urlmon' :-/ */
1344     hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1345     if (hinternet == NULL) {
1346         return E_OUTOFMEMORY;
1347     }                                                                                                                             
1348
1349     memset(&url, 0, sizeof(url));
1350     url.dwStructSize = sizeof(url);
1351     url.lpszHostName = host;
1352     url.dwHostNameLength = sizeof(host);
1353     url.lpszUrlPath = path;
1354     url.dwUrlPathLength = sizeof(path);
1355
1356     if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1357         InternetCloseHandle(hinternet);
1358         return E_OUTOFMEMORY;
1359     }
1360
1361     if (lpfnCB) {
1362         if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1363             InternetCloseHandle(hinternet);
1364             return S_OK;
1365         }
1366     }
1367     
1368     hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1369                             url.lpszUserName, url.lpszPassword,
1370                             INTERNET_SERVICE_HTTP, 0, 0);
1371     if (!hcon) {
1372         InternetCloseHandle(hinternet);
1373         return E_OUTOFMEMORY;
1374     }
1375     
1376     hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1377     if (!hreq) {
1378         InternetCloseHandle(hinternet);
1379         InternetCloseHandle(hcon);
1380         return E_OUTOFMEMORY;
1381     }                                                                                                                             
1382
1383     if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1384         InternetCloseHandle(hinternet);
1385         InternetCloseHandle(hcon);
1386         InternetCloseHandle(hreq);
1387         return E_OUTOFMEMORY;
1388     }
1389     
1390     if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1391                        &total_size, &arg_size, NULL)) {
1392         TRACE(" total size : %ld\n", total_size);
1393     }
1394     
1395     hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1396                         FILE_ATTRIBUTE_NORMAL, NULL );
1397     if (hfile == INVALID_HANDLE_VALUE) {
1398         return E_ACCESSDENIED;
1399     }
1400     
1401     if (lpfnCB) {
1402         if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1403                                            BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1404             InternetCloseHandle(hreq);
1405             InternetCloseHandle(hcon);
1406             InternetCloseHandle(hinternet);
1407             CloseHandle(hfile);
1408             return S_OK;
1409         }
1410     }
1411     
1412     total = 0;
1413     while (1) {
1414         r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1415         if (!r) {
1416             InternetCloseHandle(hreq);
1417             InternetCloseHandle(hcon);
1418             InternetCloseHandle(hinternet);
1419             
1420             CloseHandle(hfile);
1421             return E_OUTOFMEMORY;           
1422         }
1423         if (!sz)
1424             break;
1425         
1426         total += sz;
1427
1428         if (lpfnCB) {
1429             if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1430                                                BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1431                 InternetCloseHandle(hreq);
1432                 InternetCloseHandle(hcon);
1433                 InternetCloseHandle(hinternet);
1434                 CloseHandle(hfile);
1435                 return S_OK;
1436             }
1437         }
1438         
1439         if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1440             InternetCloseHandle(hreq);
1441             InternetCloseHandle(hcon);
1442             InternetCloseHandle(hinternet);
1443             
1444             CloseHandle(hfile);
1445             return E_OUTOFMEMORY;
1446         }
1447     }
1448
1449     if (lpfnCB) {
1450         if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1451                                            BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1452             InternetCloseHandle(hreq);
1453             InternetCloseHandle(hcon);
1454             InternetCloseHandle(hinternet);
1455             CloseHandle(hfile);
1456             return S_OK;
1457         }
1458     }
1459     
1460     InternetCloseHandle(hreq);
1461     InternetCloseHandle(hcon);
1462     InternetCloseHandle(hinternet);
1463     
1464     CloseHandle(hfile);
1465
1466     return S_OK;
1467 }
1468
1469 /***********************************************************************
1470  *           HlinkSimpleNavigateToString (URLMON.@)
1471  */
1472 HRESULT WINAPI HlinkSimpleNavigateToString( LPCWSTR szTarget,
1473     LPCWSTR szLocation, LPCWSTR szTargetFrameName, IUnknown *pUnk,
1474     IBindCtx *pbc, IBindStatusCallback *pbsc, DWORD grfHLNF, DWORD dwReserved)
1475 {
1476     FIXME("%s\n", debugstr_w( szTarget ) );
1477     return E_NOTIMPL;
1478 }
1479
1480 /***********************************************************************
1481  *           HlinkNavigateString (URLMON.@)
1482  */
1483 HRESULT WINAPI HlinkNavigateString( IUnknown *pUnk, LPCWSTR szTarget )
1484 {
1485     TRACE("%p %s\n", pUnk, debugstr_w( szTarget ) );
1486     return HlinkSimpleNavigateToString( 
1487                szTarget, NULL, NULL, pUnk, NULL, NULL, 0, 0 );
1488 }