Allow the implementation of the VxDCall entry points to be moved to
[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 "urlmon_main.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
42
43 /* native urlmon.dll uses this key, too */
44 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
45
46 /*static BOOL registered_wndclass = FALSE;*/
47
48 /* filemoniker data structure */
49 typedef struct URLMonikerImpl{
50
51     ICOM_VTABLE(IMoniker)*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
52     ICOM_VTABLE(IBinding)*  lpvtbl2;  /* VTable to IBinding interface */
53
54     ULONG ref; /* reference counter for this object */
55
56     LPOLESTR URLName; /* URL string identified by this URLmoniker */
57
58     HWND hwndCallback;
59     IBindCtx *pBC;
60     HINTERNET hinternet, hconnect, hrequest;
61 } URLMonikerImpl;
62
63 /********************************************************************************/
64 /* URLMoniker prototype functions :                                            */
65
66 /* IUnknown prototype functions */
67 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
68 static ULONG   WINAPI URLMonikerImpl_AddRef(IMoniker* iface);
69 static ULONG   WINAPI URLMonikerImpl_Release(IMoniker* iface);
70
71 /* IPersist prototype functions */
72 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
73
74 /* IPersistStream prototype functions */
75 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface);
76 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface, IStream* pStm);
77 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
78 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
79
80 /* IMoniker prototype functions */
81 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
82 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
83 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
84 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
85 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
86 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
87 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
88 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
89 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
90 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
91 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
92 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
93 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
94 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
95 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
96
97 /* IBinding interface to moniker */
98 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject);
99 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface);
100 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface);
101 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface);
102 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface);
103 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface);
104 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority);
105 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority);
106 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved);
107
108 /* Local function used by urlmoniker implementation */
109 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* iface, LPCOLESTR lpszURL);
110 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* iface);
111
112 /********************************************************************************/
113 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
114 /* IPersistStream and IMoniker functions.                                       */
115 static ICOM_VTABLE(IMoniker) VT_URLMonikerImpl =
116 {
117     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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 ICOM_VTABLE(IBinding) VTBinding_URLMonikerImpl =
144 {
145     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
146     URLMonikerImpl_IBinding_QueryInterface,
147     URLMonikerImpl_IBinding_AddRef,
148     URLMonikerImpl_IBinding_Release,
149     URLMonikerImpl_IBinding_Abort,
150     URLMonikerImpl_IBinding_Suspend,
151     URLMonikerImpl_IBinding_Resume,
152     URLMonikerImpl_IBinding_SetPriority,
153     URLMonikerImpl_IBinding_GetPriority,
154     URLMonikerImpl_IBinding_GetBindResult
155 };
156
157
158 /*******************************************************************************
159  *        URLMoniker_QueryInterface
160  *******************************************************************************/
161 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
162 {
163     ICOM_THIS(URLMonikerImpl,iface);
164
165     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
166
167     /* Perform a sanity check on the parameters.*/
168     if ( (This==0) || (ppvObject==0) )
169         return E_INVALIDARG;
170
171     /* Initialize the return parameter */
172     *ppvObject = 0;
173
174     /* Compare the riid with the interface IDs implemented by this object.*/
175     if (IsEqualIID(&IID_IUnknown, riid)      ||
176         IsEqualIID(&IID_IPersist, riid)      ||
177         IsEqualIID(&IID_IPersistStream,riid) ||
178         IsEqualIID(&IID_IMoniker, riid)
179        )
180         *ppvObject = iface;
181
182     /* Check that we obtained an interface.*/
183     if ((*ppvObject)==0)
184         return E_NOINTERFACE;
185
186     /* Query Interface always increases the reference count by one when it is successful */
187     URLMonikerImpl_AddRef(iface);
188
189     return S_OK;
190 }
191
192 /******************************************************************************
193  *        URLMoniker_AddRef
194  ******************************************************************************/
195 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
196 {
197     ICOM_THIS(URLMonikerImpl,iface);
198
199     TRACE("(%p)\n",This);
200
201     return ++(This->ref);
202 }
203
204 /******************************************************************************
205  *        URLMoniker_Release
206  ******************************************************************************/
207 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
208 {
209     ICOM_THIS(URLMonikerImpl,iface);
210
211     TRACE("(%p)\n",This);
212
213     This->ref--;
214
215     /* destroy the object if there's no more reference on it */
216     if (This->ref==0){
217
218         URLMonikerImpl_Destroy(This);
219
220         return 0;
221     }
222     return This->ref;
223 }
224
225 /******************************************************************************
226  *        URLMoniker_GetClassID
227  ******************************************************************************/
228 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
229                                                 CLSID *pClassID)/* Pointer to CLSID of object */
230 {
231     ICOM_THIS(URLMonikerImpl,iface);
232
233     FIXME("(%p)->(%p): stub\n",This,pClassID);
234
235     if (pClassID==NULL)
236         return E_POINTER;
237
238     return E_NOTIMPL;
239 }
240
241 /******************************************************************************
242  *        URLMoniker_IsDirty
243  ******************************************************************************/
244 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
245 {
246     ICOM_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 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
260 {
261     ICOM_THIS(URLMonikerImpl,iface);
262
263     FIXME("(%p)->(%p): stub\n",This,pStm);
264
265     return E_NOTIMPL;
266 }
267
268 /******************************************************************************
269  *        URLMoniker_Save
270  ******************************************************************************/
271 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
272                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
273                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
274 {
275     ICOM_THIS(URLMonikerImpl,iface);
276
277     FIXME("(%p)->(%p,%d): stub\n",This,pStm,fClearDirty);
278
279     return E_NOTIMPL;
280
281 }
282
283 /******************************************************************************
284  *        URLMoniker_GetSizeMax
285  ******************************************************************************/
286 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
287                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
288 {
289     ICOM_THIS(URLMonikerImpl,iface);
290
291     FIXME("(%p)->(%p): stub\n",This,pcbSize);
292
293     return E_NOTIMPL;
294 }
295
296 /******************************************************************************
297  *         URLMoniker_Construct (local function)
298  *******************************************************************************/
299 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszURLName)
300 {
301     int sizeStr = strlenW(lpszURLName);
302
303     TRACE("(%p,%s)\n",This,debugstr_w(lpszURLName));
304     memset(This, 0, sizeof(*This));
305
306     /* Initialize the virtual fgunction table. */
307     This->lpvtbl1      = &VT_URLMonikerImpl;
308     This->lpvtbl2      = &VTBinding_URLMonikerImpl;
309     This->ref          = 0;
310
311     This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
312
313     if (This->URLName==NULL)
314         return E_OUTOFMEMORY;
315
316     strcpyW(This->URLName,lpszURLName);
317
318     return S_OK;
319 }
320
321
322
323 /******************************************************************************
324  *        URLMoniker_Destroy (local function)
325  *******************************************************************************/
326 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* This)
327 {
328     TRACE("(%p)\n",This);
329
330     if (This->URLName!=NULL)
331             HeapFree(GetProcessHeap(),0,This->URLName);
332
333     HeapFree(GetProcessHeap(),0,This);
334
335     return S_OK;
336 }
337
338 /******************************************************************************
339  *                  URLMoniker_BindToObject
340  ******************************************************************************/
341 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
342                                                   IBindCtx* pbc,
343                                                   IMoniker* pmkToLeft,
344                                                   REFIID riid,
345                                                   VOID** ppvResult)
346 {
347     ICOM_THIS(URLMonikerImpl,iface);
348
349     *ppvResult=0;
350
351     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
352           ppvResult);
353
354     return E_NOTIMPL;
355 }
356
357 typedef struct {
358     enum {OnProgress, OnDataAvailable} callback;
359 } URLMON_CallbackData;
360
361
362 #if 0
363 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
364 {
365     return DefWindowProcA(hwnd, msg, wparam, lparam);
366 }
367
368 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
369 {
370 }
371
372 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
373                                              void *status_info, DWORD status_info_len)
374 {
375     URLMonikerImpl *This = (URLMonikerImpl *)context;
376     TRACE("handle %p this %p status %08lx\n", hinet, This, status);
377
378     if(This->filesize == -1) {
379         switch(status) {
380         case INTERNET_STATUS_RESOLVING_NAME:
381             PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
382             break;
383         case INTERNET_STATUS_CONNECTING_TO_SERVER:
384             PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
385             break;
386         case INTERNET_STATUS_SENDING_REQUEST:
387             PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
388             break;
389         case INTERNET_REQUEST_COMPLETE:
390           {
391               DWORD len, lensz = sizeof(len);
392
393             HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
394             TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
395             This->filesize = len;
396             break;
397           }
398         }
399     }
400
401     return;
402 }
403 #endif
404 /******************************************************************************
405  *        URLMoniker_BindToStorage
406  ******************************************************************************/
407 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
408                                                    IBindCtx* pbc,
409                                                    IMoniker* pmkToLeft,
410                                                    REFIID riid,
411                                                    VOID** ppvObject)
412 {
413     ICOM_THIS(URLMonikerImpl,iface);
414     HRESULT hres;
415     IBindStatusCallback *pbscb;
416     BINDINFO bi;
417     DWORD bindf;
418
419     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
420     if(pmkToLeft) {
421         FIXME("pmkToLeft != NULL\n");
422         return E_NOTIMPL;
423     }
424     if(!IsEqualIID(&IID_IStream, riid)) {
425         FIXME("unsupported iid\n");
426         return E_NOTIMPL;
427     }
428
429     /* FIXME This is a bad hack (tm).  We should clearly download to a temporary file.
430        We also need to implement IStream ourselves so that IStream_Read can return
431        E_PENDING */
432
433     hres = CreateStreamOnHGlobal(0, TRUE, (IStream**)ppvObject);
434
435
436     if(SUCCEEDED(hres)) {
437         TRACE("Created dummy stream...\n");
438
439         hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&pbscb);
440         if(SUCCEEDED(hres)) {
441             TRACE("Got IBindStatusCallback...\n");
442
443             memset(&bi, 0, sizeof(bi));
444             bi.cbSize = sizeof(bi);
445             bindf = 0;
446             hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
447             if(SUCCEEDED(hres)) {
448                 URL_COMPONENTSW url;
449                 WCHAR *host, *path;
450                 DWORD len, lensz = sizeof(len), total_read = 0;
451                 LARGE_INTEGER last_read_pos;
452                 FORMATETC fmt;
453                 STGMEDIUM stg;
454
455                 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
456                       bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
457                 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
458                 TRACE("OnStartBinding rets %08lx\n", hres);
459
460 #if 0
461                 if(!registered_wndclass) {
462                     WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
463                     RegisterClassA(&urlmon_wndclass);
464                     registered_wndclass = TRUE;
465                 }
466
467                 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
468                                                    URLMON_hInstance, NULL);
469
470 #endif
471                 memset(&url, 0, sizeof(url));
472                 url.dwStructSize = sizeof(url);
473                 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
474                 InternetCrackUrlW(This->URLName, 0, 0, &url);
475                 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
476                 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
477                 host[url.dwHostNameLength] = '\0';
478                 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
479                 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
480                 path[url.dwUrlPathLength] = '\0';
481
482                 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
483 /*              InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
484
485                 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
486                                             INTERNET_SERVICE_HTTP, 0, (DWORD)This);
487                 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
488
489                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
490                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
491                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
492                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
493                 hres = E_OUTOFMEMORY; /* FIXME */
494                 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
495
496                     len = 0;
497                     HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
498
499                     TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
500
501                     last_read_pos.u.LowPart = last_read_pos.u.HighPart = 0;
502                     fmt.cfFormat = 0;
503                     fmt.ptd = NULL;
504                     fmt.dwAspect = 0;
505                     fmt.lindex = -1;
506                     fmt.tymed = TYMED_ISTREAM;
507                     stg.tymed = TYMED_ISTREAM;
508                     stg.u.pstm = *(IStream**)ppvObject;
509                     stg.pUnkForRelease = NULL;
510
511                     while(1) {
512                         char buf[4096];
513                         DWORD bufread;
514                         DWORD written;
515                         if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
516                             TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
517                             if(bufread == 0) break;
518                             IStream_Write(*(IStream**)ppvObject, buf, bufread, &written);
519                             total_read += bufread;
520                             IStream_Seek(*(IStream**)ppvObject, last_read_pos, STREAM_SEEK_SET, NULL);
521                             hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
522                                                                   BINDSTATUS_BEGINDOWNLOADDATA :
523                                                                   BINDSTATUS_DOWNLOADINGDATA, NULL);
524                             hres = IBindStatusCallback_OnDataAvailable(pbscb,
525                                                                        (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
526                                                                        BSCF_INTERMEDIATEDATANOTIFICATION,
527                                                                        total_read, &fmt, &stg);
528                             last_read_pos.u.LowPart += bufread; /* FIXME */
529                         } else
530                             break;
531                     }
532                     hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
533                     hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
534                     TRACE("OnDataAvail rets %08lx\n", hres);
535                     hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
536                     TRACE("OnStop rets %08lx\n", hres);
537                     hres = S_OK;
538                 }
539                 InternetCloseHandle(This->hrequest);
540                 InternetCloseHandle(This->hconnect);
541                 InternetCloseHandle(This->hinternet);
542             }
543         }
544     }
545     return hres;
546 }
547
548 /******************************************************************************
549  *        URLMoniker_Reduce
550  ******************************************************************************/
551 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
552                                             IBindCtx* pbc,
553                                             DWORD dwReduceHowFar,
554                                             IMoniker** ppmkToLeft,
555                                             IMoniker** ppmkReduced)
556 {
557     ICOM_THIS(URLMonikerImpl,iface);
558     FIXME("(%p)->(%p,%ld,%p,%p): stub\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
559
560     return E_NOTIMPL;
561 }
562
563 /******************************************************************************
564  *        URLMoniker_ComposeWith
565  ******************************************************************************/
566 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
567                                                  IMoniker* pmkRight,
568                                                  BOOL fOnlyIfNotGeneric,
569                                                  IMoniker** ppmkComposite)
570 {
571     ICOM_THIS(URLMonikerImpl,iface);
572     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
573
574     return E_NOTIMPL;
575 }
576
577 /******************************************************************************
578  *        URLMoniker_Enum
579  ******************************************************************************/
580 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
581 {
582     ICOM_THIS(URLMonikerImpl,iface);
583     FIXME("(%p)->(%d,%p): stub\n",This,fForward,ppenumMoniker);
584
585     return E_NOTIMPL;
586 }
587
588 /******************************************************************************
589  *        URLMoniker_IsEqual
590  ******************************************************************************/
591 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
592 {
593     ICOM_THIS(URLMonikerImpl,iface);
594     FIXME("(%p)->(%p): stub\n",This,pmkOtherMoniker);
595
596     return E_NOTIMPL;
597 }
598
599
600 /******************************************************************************
601  *        URLMoniker_Hash
602  ******************************************************************************/
603 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
604 {
605     ICOM_THIS(URLMonikerImpl,iface);
606     FIXME("(%p)->(%p): stub\n",This,pdwHash);
607
608     return E_NOTIMPL;
609 }
610
611 /******************************************************************************
612  *        URLMoniker_IsRunning
613  ******************************************************************************/
614 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
615                                                IBindCtx* pbc,
616                                                IMoniker* pmkToLeft,
617                                                IMoniker* pmkNewlyRunning)
618 {
619     ICOM_THIS(URLMonikerImpl,iface);
620     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
621
622     return E_NOTIMPL;
623 }
624
625 /******************************************************************************
626  *        URLMoniker_GetTimeOfLastChange
627  ******************************************************************************/
628 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
629                                                          IBindCtx* pbc,
630                                                          IMoniker* pmkToLeft,
631                                                          FILETIME* pFileTime)
632 {
633     ICOM_THIS(URLMonikerImpl,iface);
634     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
635
636     return E_NOTIMPL;
637 }
638
639 /******************************************************************************
640  *        URLMoniker_Inverse
641  ******************************************************************************/
642 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
643 {
644     ICOM_THIS(URLMonikerImpl,iface);
645     FIXME("(%p)->(%p): stub\n",This,ppmk);
646
647     return E_NOTIMPL;
648 }
649
650 /******************************************************************************
651  *        URLMoniker_CommonPrefixWith
652  ******************************************************************************/
653 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
654 {
655     ICOM_THIS(URLMonikerImpl,iface);
656     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
657
658     return E_NOTIMPL;
659 }
660
661 /******************************************************************************
662  *        URLMoniker_RelativePathTo
663  ******************************************************************************/
664 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
665 {
666     ICOM_THIS(URLMonikerImpl,iface);
667     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
668
669     return E_NOTIMPL;
670 }
671
672 /******************************************************************************
673  *        URLMoniker_GetDisplayName
674  ******************************************************************************/
675 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
676                                                     IBindCtx* pbc,
677                                                     IMoniker* pmkToLeft,
678                                                     LPOLESTR *ppszDisplayName)
679 {
680     ICOM_THIS(URLMonikerImpl,iface);
681     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,ppszDisplayName);
682
683     return E_NOTIMPL;
684 }
685
686 /******************************************************************************
687  *        URLMoniker_ParseDisplayName
688  ******************************************************************************/
689 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
690                                                       IBindCtx* pbc,
691                                                       IMoniker* pmkToLeft,
692                                                       LPOLESTR pszDisplayName,
693                                                       ULONG* pchEaten,
694                                                       IMoniker** ppmkOut)
695 {
696     ICOM_THIS(URLMonikerImpl,iface);
697     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
698
699     return E_NOTIMPL;
700 }
701
702 /******************************************************************************
703  *        URLMoniker_IsSystemMoniker
704  ******************************************************************************/
705 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
706 {
707     ICOM_THIS(URLMonikerImpl,iface);
708     FIXME("(%p)->(%p)\n",This,pwdMksys);
709
710     return E_NOTIMPL;
711 }
712
713 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
714 {
715     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
716
717     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
718
719     /* Perform a sanity check on the parameters.*/
720     if ( (This==0) || (ppvObject==0) )
721         return E_INVALIDARG;
722
723     /* Initialize the return parameter */
724     *ppvObject = 0;
725
726     /* Compare the riid with the interface IDs implemented by this object.*/
727     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
728         *ppvObject = iface;
729
730     /* Check that we obtained an interface.*/
731     if ((*ppvObject)==0)
732         return E_NOINTERFACE;
733
734     /* Query Interface always increases the reference count by one when it is successful */
735     URLMonikerImpl_IBinding_AddRef(iface);
736
737     return S_OK;
738
739 }
740
741 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
742 {
743     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
744     TRACE("(%p)\n",This);
745
746     return URLMonikerImpl_AddRef((IMoniker*)This);
747 }
748
749 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
750 {
751     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
752     TRACE("(%p)\n",This);
753
754     return URLMonikerImpl_Release((IMoniker*)This);
755 }
756
757 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
758 {
759     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
760     FIXME("(%p): stub\n", This);
761
762     return E_NOTIMPL;
763 }
764
765 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
766 {
767     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
768     FIXME("(%p)->(%s, %p, %p, %p): stub\n", This, debugstr_guid(pclsidProtocol), pdwResult, pszResult, pdwReserved);
769
770     return E_NOTIMPL;
771 }
772
773 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
774 {
775     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
776     FIXME("(%p)->(%p): stub\n", This, pnPriority);
777
778     return E_NOTIMPL;
779 }
780
781 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
782 {
783     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
784     FIXME("(%p): stub\n", This);
785
786     return E_NOTIMPL;
787 }
788
789 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
790 {
791     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
792     FIXME("(%p)->(%ld): stub\n", This, nPriority);
793
794     return E_NOTIMPL;
795 }
796
797 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
798 {
799     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
800     FIXME("(%p): stub\n", This);
801
802     return E_NOTIMPL;
803 }
804
805 /***********************************************************************
806  *           CreateAsyncBindCtx (URLMON.@)
807  */
808 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
809     IEnumFORMATETC *format, IBindCtx **pbind)
810 {
811     FIXME("stub.\n");
812     return E_INVALIDARG;
813 }
814 /***********************************************************************
815  *           CreateAsyncBindCtxEx (URLMON.@)
816  *
817  * Create an asynchronous bind context.
818  *
819  * FIXME
820  *   Not implemented.
821  */
822 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
823     IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
824     DWORD reserved)
825 {
826      FIXME("stub, returns failure\n");
827      return E_INVALIDARG;
828 }
829
830
831 /***********************************************************************
832  *           CreateURLMoniker (URLMON.@)
833  *
834  * Create a url moniker.
835  *
836  * PARAMS
837  *    pmkContext [I] Context
838  *    szURL      [I] Url to create the moniker for
839  *    ppmk       [O] Destination for created moniker.
840  *
841  * RETURNS
842  *    Success: S_OK. ppmk contains the created IMoniker object.
843  *    Failure: MK_E_SYNTAX if szURL is not a valid url, or
844  *             E_OUTOFMEMORY if memory allocation fails.
845  */
846 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
847 {
848     URLMonikerImpl *obj;
849     HRESULT hres;
850     IID iid = IID_IMoniker;
851
852     TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
853
854     if (NULL != pmkContext)
855         FIXME("Non-null pmkContext not implemented\n");
856
857     if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
858         return E_OUTOFMEMORY;
859
860     hres = URLMonikerImpl_Construct(obj, szURL);
861     if(SUCCEEDED(hres))
862         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
863     else
864         HeapFree(GetProcessHeap(), 0, obj);
865     return hres;
866 }
867
868
869 /***********************************************************************
870  *           CoInternetGetSession (URLMON.@)
871  *
872  * Create a new internet session and return an IInternetSession interface
873  * representing it.
874  *
875  * PARAMS
876  *    dwSessionMode      [I] Mode for the internet session
877  *    ppIInternetSession [O] Destination for creates IInternetSession object
878  *    dwReserved         [I] Reserved, must be 0.
879  *
880  * RETURNS
881  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
882  *    Failure: E_INVALIDARG, if any argument is invalid, or
883  *             E_OUTOFMEMORY if memory allocation fails.
884  */
885 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, /*IInternetSession*/void **ppIInternetSession, DWORD dwReserved)
886 {
887     FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
888
889     if(dwSessionMode) {
890       ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
891     }
892
893     if(dwReserved) {
894       ERR("dwReserved: %ld, must be zero\n", dwReserved);
895     }
896
897     *ppIInternetSession=NULL;
898     return E_OUTOFMEMORY;
899 }
900
901 /***********************************************************************
902  *           CoInternetQueryInfo (URLMON.@)
903  *
904  * Retrieves information relevant to a specified URL
905  *
906  * RETURNS
907  *    S_OK                      success
908  *    S_FALSE                   buffer too small
909  *    INET_E_QUERYOPTIONUNKNOWN invalid option
910  *
911  */
912 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
913   DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
914   DWORD dwReserved)
915 {
916   FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
917     QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
918   return S_OK;
919 }
920
921 /***********************************************************************
922  *           FindMimeFromData (URLMON.@)
923  *
924  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
925  *
926  */
927 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
928    DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
929    LPWSTR* ppwzMimeOut, DWORD dwReserved)
930 {
931   FIXME("stub\n");
932   return E_OUTOFMEMORY;
933 }
934
935 /***********************************************************************
936  *           IsAsyncMoniker (URLMON.@)
937  */
938 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
939 {
940     FIXME("stub\n");
941     return S_FALSE;
942 }
943
944 /***********************************************************************
945  *           RegisterBindStatusCallback (URLMON.@)
946  *
947  * Register a bind status callback.
948  *
949  * PARAMS
950  *  pbc           [I] Binding context
951  *  pbsc          [I] Callback to register
952  *  ppbscPrevious [O] Destination for previous callback
953  *  dwReserved    [I] Reserved, must be 0.
954  *
955  * RETURNS
956  *    Success: S_OK.
957  *    Failure: E_INVALIDARG, if any argument is invalid, or
958  *             E_OUTOFMEMORY if memory allocation fails.
959  */
960 HRESULT WINAPI RegisterBindStatusCallback(
961     IBindCtx *pbc,
962     IBindStatusCallback *pbsc,
963     IBindStatusCallback **ppbscPrevious,
964     DWORD dwReserved)
965 {
966     IBindStatusCallback *prev;
967
968         TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
969
970     if (pbc == NULL || pbsc == NULL)
971         return E_INVALIDARG;
972
973     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&prev)))
974     {
975         IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
976         if (ppbscPrevious)
977             *ppbscPrevious = prev;
978         else
979             IBindStatusCallback_Release(prev);
980     }
981
982         return IBindCtx_RegisterObjectParam(pbc, BSCBHolder, (IUnknown *)pbsc);
983 }
984
985 /***********************************************************************
986  *           RevokeBindStatusCallback (URLMON.@)
987  *
988  * Unregister a bind status callback.
989  *
990  *  pbc           [I] Binding context
991  *  pbsc          [I] Callback to unregister
992  *
993  * RETURNS
994  *    Success: S_OK.
995  *    Failure: E_INVALIDARG, if any argument is invalid, or
996  *             E_FAIL if pbsc wasn't registered with pbc.
997  */
998 HRESULT WINAPI RevokeBindStatusCallback(
999     IBindCtx *pbc,
1000     IBindStatusCallback *pbsc)
1001 {
1002     IBindStatusCallback *callback;
1003     HRESULT hr = E_FAIL;
1004
1005         TRACE("(%p,%p)\n", pbc, pbsc);
1006
1007     if (pbc == NULL || pbsc == NULL)
1008         return E_INVALIDARG;
1009
1010     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&callback)))
1011     {
1012         if (callback == pbsc)
1013         {
1014             IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
1015             hr = S_OK;
1016         }
1017         IBindStatusCallback_Release(pbsc);
1018     }
1019
1020     return hr;
1021 }
1022
1023 /***********************************************************************
1024  *           ReleaseBindInfo (URLMON.@)
1025  *
1026  * Release the resources used by the specified BINDINFO structure.
1027  *
1028  * PARAMS
1029  *  pbindinfo [I] BINDINFO to release.
1030  *
1031  * RETURNS
1032  *  Nothing.
1033  */
1034 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1035 {
1036     FIXME("(%p)stub!\n", pbindinfo);
1037 }
1038
1039 /***********************************************************************
1040  *           URLDownloadToFileA (URLMON.@)
1041  *
1042  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1043  * report progress.
1044  *
1045  * PARAMS
1046  *  pCaller    [I] controlling IUnknown interface.
1047  *  szURL      [I] URL of the file to download
1048  *  szFileName [I] file name to store the content of the URL
1049  *  dwReserved [I] reserved - set to 0
1050  *  lpfnCB     [I] callback for progress report
1051  *
1052  * RETURNS
1053  *  S_OK on success
1054  *  E_OUTOFMEMORY when going out of memory
1055  */
1056 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1057                                   LPCSTR szURL,
1058                                   LPCSTR szFileName,
1059                                   DWORD dwReserved,
1060                                   LPBINDSTATUSCALLBACK lpfnCB)
1061 {
1062     UNICODE_STRING szURL_w, szFileName_w;
1063
1064     if ((szURL == NULL) || (szFileName == NULL)) {
1065         FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1066         return E_INVALIDARG; /* The error code is not specified in this case... */
1067     }
1068     
1069     if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1070         if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1071             HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1072
1073             RtlFreeUnicodeString(&szURL_w);
1074             RtlFreeUnicodeString(&szFileName_w);
1075             
1076             return ret;
1077         } else {
1078             RtlFreeUnicodeString(&szURL_w);
1079         }
1080     }
1081     
1082     FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1083     return E_OUTOFMEMORY;
1084 }
1085
1086 /***********************************************************************
1087  *           URLDownloadToFileW (URLMON.@)
1088  *
1089  * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1090  * report progress.
1091  *
1092  * PARAMS
1093  *  pCaller    [I] controlling IUnknown interface.
1094  *  szURL      [I] URL of the file to download
1095  *  szFileName [I] file name to store the content of the URL
1096  *  dwReserved [I] reserved - set to 0
1097  *  lpfnCB     [I] callback for progress report
1098  *
1099  * RETURNS
1100  *  S_OK on success
1101  *  E_OUTOFMEMORY when going out of memory
1102  */
1103 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1104                                   LPCWSTR szURL,
1105                                   LPCWSTR szFileName,
1106                                   DWORD dwReserved,
1107                                   LPBINDSTATUSCALLBACK lpfnCB)
1108 {
1109     HINTERNET hinternet, hcon, hreq;
1110     BOOL r;
1111     CHAR buffer[0x1000];
1112     DWORD sz, total, written;
1113     DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1114     URL_COMPONENTSW url;
1115     WCHAR host[0x80], path[0x100];
1116     HANDLE hfile;
1117     WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1118
1119     /* Note: all error codes would need to be checked agains real Windows behaviour... */
1120     TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1121
1122     if ((szURL == NULL) || (szFileName == NULL)) {
1123         FIXME(" cannot accept NULL strings !\n");
1124         return E_INVALIDARG;
1125     }
1126
1127     /* Would be better to use the application name here rather than 'urlmon' :-/ */
1128     hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1129     if (hinternet == NULL) {
1130         return E_OUTOFMEMORY;
1131     }                                                                                                                             
1132
1133     memset(&url, 0, sizeof(url));
1134     url.dwStructSize = sizeof(url);
1135     url.lpszHostName = host;
1136     url.dwHostNameLength = sizeof(host);
1137     url.lpszUrlPath = path;
1138     url.dwUrlPathLength = sizeof(path);
1139
1140     if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1141         InternetCloseHandle(hinternet);
1142         return E_OUTOFMEMORY;
1143     }
1144
1145     if (lpfnCB) {
1146         if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1147             InternetCloseHandle(hinternet);
1148             return S_OK;
1149         }
1150     }
1151     
1152     hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1153                             url.lpszUserName, url.lpszPassword,
1154                             INTERNET_SERVICE_HTTP, 0, 0);
1155     if (!hcon) {
1156         InternetCloseHandle(hinternet);
1157         return E_OUTOFMEMORY;
1158     }
1159     
1160     hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1161     if (!hreq) {
1162         InternetCloseHandle(hinternet);
1163         InternetCloseHandle(hcon);
1164         return E_OUTOFMEMORY;
1165     }                                                                                                                             
1166
1167     if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1168         InternetCloseHandle(hinternet);
1169         InternetCloseHandle(hcon);
1170         InternetCloseHandle(hreq);
1171         return E_OUTOFMEMORY;
1172     }
1173     
1174     if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1175                        &total_size, &arg_size, NULL)) {
1176         TRACE(" total size : %ld\n", total_size);
1177     }
1178     
1179     hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1180                         FILE_ATTRIBUTE_NORMAL, NULL );
1181     if (hfile == INVALID_HANDLE_VALUE) {
1182         return E_ACCESSDENIED;
1183     }
1184     
1185     if (lpfnCB) {
1186         if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1187                                            BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1188             InternetCloseHandle(hreq);
1189             InternetCloseHandle(hcon);
1190             InternetCloseHandle(hinternet);
1191             CloseHandle(hfile);
1192             return S_OK;
1193         }
1194     }
1195     
1196     total = 0;
1197     while (1) {
1198         r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1199         if (!r) {
1200             InternetCloseHandle(hreq);
1201             InternetCloseHandle(hcon);
1202             InternetCloseHandle(hinternet);
1203             
1204             CloseHandle(hfile);
1205             return E_OUTOFMEMORY;           
1206         }
1207         if (!sz)
1208             break;
1209         
1210         total += sz;
1211
1212         if (lpfnCB) {
1213             if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1214                                                BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1215                 InternetCloseHandle(hreq);
1216                 InternetCloseHandle(hcon);
1217                 InternetCloseHandle(hinternet);
1218                 CloseHandle(hfile);
1219                 return S_OK;
1220             }
1221         }
1222         
1223         if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1224             InternetCloseHandle(hreq);
1225             InternetCloseHandle(hcon);
1226             InternetCloseHandle(hinternet);
1227             
1228             CloseHandle(hfile);
1229             return E_OUTOFMEMORY;
1230         }
1231     }
1232
1233     if (lpfnCB) {
1234         if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1235                                            BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1236             InternetCloseHandle(hreq);
1237             InternetCloseHandle(hcon);
1238             InternetCloseHandle(hinternet);
1239             CloseHandle(hfile);
1240             return S_OK;
1241         }
1242     }
1243     
1244     InternetCloseHandle(hreq);
1245     InternetCloseHandle(hcon);
1246     InternetCloseHandle(hinternet);
1247     
1248     CloseHandle(hfile);
1249
1250     return S_OK;
1251 }