Fix a crash when launching Unix executables.
[wine/multimedia.git] / dlls / urlmon / umon.c
blobdc088c609c459babb6300f35f25f87ee3ad16d8f
1 /*
2 * UrlMon
4 * Copyright 1999 Ulrich Czekalla for Corel Corporation
5 * Copyright 2002 Huw D M Davies for CodeWeavers
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.
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.
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
22 #define COM_NO_WINDOWS_H
23 #include <stdarg.h>
24 #include <stdio.h>
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"
42 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
44 /* native urlmon.dll uses this key, too */
45 static const WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
47 /*static BOOL registered_wndclass = FALSE;*/
49 /* filemoniker data structure */
50 typedef struct URLMonikerImpl{
52 ICOM_VTABLE(IMoniker)* lpvtbl1; /* VTable relative to the IMoniker interface.*/
53 ICOM_VTABLE(IBinding)* lpvtbl2; /* VTable to IBinding interface */
55 ULONG ref; /* reference counter for this object */
57 LPOLESTR URLName; /* URL string identified by this URLmoniker */
59 HWND hwndCallback;
60 IBindCtx *pBC;
61 HINTERNET hinternet, hconnect, hrequest;
62 } URLMonikerImpl;
64 /********************************************************************************/
65 /* URLMoniker prototype functions : */
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);
72 /* IPersist prototype functions */
73 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
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);
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);
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);
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);
113 /********************************************************************************/
114 /* Virtual function table for the URLMonikerImpl class which include IPersist,*/
115 /* IPersistStream and IMoniker functions. */
116 static ICOM_VTABLE(IMoniker) VT_URLMonikerImpl =
118 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
119 URLMonikerImpl_QueryInterface,
120 URLMonikerImpl_AddRef,
121 URLMonikerImpl_Release,
122 URLMonikerImpl_GetClassID,
123 URLMonikerImpl_IsDirty,
124 URLMonikerImpl_Load,
125 URLMonikerImpl_Save,
126 URLMonikerImpl_GetSizeMax,
127 URLMonikerImpl_BindToObject,
128 URLMonikerImpl_BindToStorage,
129 URLMonikerImpl_Reduce,
130 URLMonikerImpl_ComposeWith,
131 URLMonikerImpl_Enum,
132 URLMonikerImpl_IsEqual,
133 URLMonikerImpl_Hash,
134 URLMonikerImpl_IsRunning,
135 URLMonikerImpl_GetTimeOfLastChange,
136 URLMonikerImpl_Inverse,
137 URLMonikerImpl_CommonPrefixWith,
138 URLMonikerImpl_RelativePathTo,
139 URLMonikerImpl_GetDisplayName,
140 URLMonikerImpl_ParseDisplayName,
141 URLMonikerImpl_IsSystemMoniker
144 static ICOM_VTABLE(IBinding) VTBinding_URLMonikerImpl =
146 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
147 URLMonikerImpl_IBinding_QueryInterface,
148 URLMonikerImpl_IBinding_AddRef,
149 URLMonikerImpl_IBinding_Release,
150 URLMonikerImpl_IBinding_Abort,
151 URLMonikerImpl_IBinding_Suspend,
152 URLMonikerImpl_IBinding_Resume,
153 URLMonikerImpl_IBinding_SetPriority,
154 URLMonikerImpl_IBinding_GetPriority,
155 URLMonikerImpl_IBinding_GetBindResult
159 /*******************************************************************************
160 * URLMoniker_QueryInterface
161 *******************************************************************************/
162 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
164 ICOM_THIS(URLMonikerImpl,iface);
166 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
168 /* Perform a sanity check on the parameters.*/
169 if ( (This==0) || (ppvObject==0) )
170 return E_INVALIDARG;
172 /* Initialize the return parameter */
173 *ppvObject = 0;
175 /* Compare the riid with the interface IDs implemented by this object.*/
176 if (IsEqualIID(&IID_IUnknown, riid) ||
177 IsEqualIID(&IID_IPersist, riid) ||
178 IsEqualIID(&IID_IPersistStream,riid) ||
179 IsEqualIID(&IID_IMoniker, riid)
181 *ppvObject = iface;
183 /* Check that we obtained an interface.*/
184 if ((*ppvObject)==0)
185 return E_NOINTERFACE;
187 /* Query Interface always increases the reference count by one when it is successful */
188 URLMonikerImpl_AddRef(iface);
190 return S_OK;
193 /******************************************************************************
194 * URLMoniker_AddRef
195 ******************************************************************************/
196 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
198 ICOM_THIS(URLMonikerImpl,iface);
200 TRACE("(%p)\n",This);
202 return ++(This->ref);
205 /******************************************************************************
206 * URLMoniker_Release
207 ******************************************************************************/
208 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
210 ICOM_THIS(URLMonikerImpl,iface);
212 TRACE("(%p)\n",This);
214 This->ref--;
216 /* destroy the object if there's no more reference on it */
217 if (This->ref==0){
219 URLMonikerImpl_Destroy(This);
221 return 0;
223 return This->ref;
226 /******************************************************************************
227 * URLMoniker_GetClassID
228 ******************************************************************************/
229 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
230 CLSID *pClassID)/* Pointer to CLSID of object */
232 ICOM_THIS(URLMonikerImpl,iface);
234 TRACE("(%p,%p)\n",This,pClassID);
236 if (pClassID==NULL)
237 return E_POINTER;
238 /* Windows always returns CLSID_StdURLMoniker */
239 *pClassID = CLSID_StdURLMoniker;
240 return S_OK;
243 /******************************************************************************
244 * URLMoniker_IsDirty
245 ******************************************************************************/
246 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
248 ICOM_THIS(URLMonikerImpl,iface);
249 /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
250 method in the OLE-provided moniker interfaces always return S_FALSE because
251 their internal state never changes. */
253 TRACE("(%p)\n",This);
255 return S_FALSE;
258 /******************************************************************************
259 * URLMoniker_Load
261 * NOTE
262 * Writes a ULONG containing length of unicode string, followed
263 * by that many unicode characters
264 ******************************************************************************/
265 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
267 ICOM_THIS(URLMonikerImpl,iface);
269 HRESULT res;
270 ULONG len;
271 ULONG got;
272 TRACE("(%p,%p)\n",This,pStm);
274 if(!pStm)
275 return E_INVALIDARG;
277 res = IStream_Read(pStm, &len, sizeof(ULONG), &got);
278 if(SUCCEEDED(res)) {
279 if(got == sizeof(ULONG)) {
280 if(This->URLName)
281 HeapFree(GetProcessHeap(), 0, This->URLName);
282 This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(len+1));
283 if(!This->URLName)
284 res = E_OUTOFMEMORY;
285 else {
286 res = IStream_Read(pStm, This->URLName, len, NULL);
287 This->URLName[len] = 0;
290 else
291 res = E_FAIL;
293 return res;
296 /******************************************************************************
297 * URLMoniker_Save
298 ******************************************************************************/
299 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
300 IStream* pStm,/* pointer to the stream where the object is to be saved */
301 BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
303 ICOM_THIS(URLMonikerImpl,iface);
305 HRESULT res;
306 ULONG len;
307 TRACE("(%p,%p,%d)\n",This,pStm,fClearDirty);
309 if(!pStm)
310 return E_INVALIDARG;
312 len = strlenW(This->URLName);
313 res=IStream_Write(pStm,&len,sizeof(ULONG),NULL);
314 if(SUCCEEDED(res))
315 res=IStream_Write(pStm,&This->URLName,len*sizeof(WCHAR),NULL);
316 return res;
320 /******************************************************************************
321 * URLMoniker_GetSizeMax
322 ******************************************************************************/
323 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
324 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
326 ICOM_THIS(URLMonikerImpl,iface);
328 TRACE("(%p,%p)\n",This,pcbSize);
330 if(!pcbSize)
331 return E_INVALIDARG;
333 pcbSize->u.LowPart = sizeof(ULONG) + (strlenW(This->URLName) * sizeof(WCHAR));
334 pcbSize->u.HighPart = 0;
335 return S_OK;
338 /******************************************************************************
339 * URLMoniker_Construct (local function)
340 *******************************************************************************/
341 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszLeftURLName, LPCOLESTR lpszURLName)
343 HRESULT hres;
344 DWORD sizeStr;
346 TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszLeftURLName),debugstr_w(lpszURLName));
347 memset(This, 0, sizeof(*This));
349 /* Initialize the virtual function table. */
350 This->lpvtbl1 = &VT_URLMonikerImpl;
351 This->lpvtbl2 = &VTBinding_URLMonikerImpl;
352 This->ref = 0;
354 if(lpszLeftURLName) {
355 hres = UrlCombineW(lpszLeftURLName, lpszURLName, NULL, &sizeStr, 0);
356 if(FAILED(hres)) {
357 return hres;
359 sizeStr++;
361 else
362 sizeStr = lstrlenW(lpszURLName)+1;
364 This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr));
366 if (This->URLName==NULL)
367 return E_OUTOFMEMORY;
369 if(lpszLeftURLName) {
370 hres = UrlCombineW(lpszLeftURLName, lpszURLName, This->URLName, &sizeStr, 0);
371 if(FAILED(hres)) {
372 HeapFree(GetProcessHeap(), 0, This->URLName);
373 return hres;
376 else
377 strcpyW(This->URLName,lpszURLName);
379 return S_OK;
384 /******************************************************************************
385 * URLMoniker_Destroy (local function)
386 *******************************************************************************/
387 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* This)
389 TRACE("(%p)\n",This);
391 if (This->URLName!=NULL)
392 HeapFree(GetProcessHeap(),0,This->URLName);
394 HeapFree(GetProcessHeap(),0,This);
396 return S_OK;
399 /******************************************************************************
400 * URLMoniker_BindToObject
401 ******************************************************************************/
402 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
403 IBindCtx* pbc,
404 IMoniker* pmkToLeft,
405 REFIID riid,
406 VOID** ppvResult)
408 ICOM_THIS(URLMonikerImpl,iface);
410 *ppvResult=0;
412 FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
413 ppvResult);
415 return E_NOTIMPL;
418 typedef struct {
419 enum {OnProgress, OnDataAvailable} callback;
420 } URLMON_CallbackData;
423 #if 0
424 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
426 return DefWindowProcA(hwnd, msg, wparam, lparam);
429 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
433 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
434 void *status_info, DWORD status_info_len)
436 URLMonikerImpl *This = (URLMonikerImpl *)context;
437 TRACE("handle %p this %p status %08lx\n", hinet, This, status);
439 if(This->filesize == -1) {
440 switch(status) {
441 case INTERNET_STATUS_RESOLVING_NAME:
442 PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
443 break;
444 case INTERNET_STATUS_CONNECTING_TO_SERVER:
445 PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
446 break;
447 case INTERNET_STATUS_SENDING_REQUEST:
448 PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
449 break;
450 case INTERNET_REQUEST_COMPLETE:
452 DWORD len, lensz = sizeof(len);
454 HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
455 TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
456 This->filesize = len;
457 break;
462 return;
464 #endif
465 /******************************************************************************
466 * URLMoniker_BindToStorage
467 ******************************************************************************/
468 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
469 IBindCtx* pbc,
470 IMoniker* pmkToLeft,
471 REFIID riid,
472 VOID** ppvObject)
474 ICOM_THIS(URLMonikerImpl,iface);
475 HRESULT hres;
476 IBindStatusCallback *pbscb;
477 BINDINFO bi;
478 DWORD bindf;
480 FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
481 if(pmkToLeft) {
482 FIXME("pmkToLeft != NULL\n");
483 return E_NOTIMPL;
485 if(!IsEqualIID(&IID_IStream, riid)) {
486 FIXME("unsupported iid\n");
487 return E_NOTIMPL;
490 /* FIXME This is a bad hack (tm). We should clearly download to a temporary file.
491 We also need to implement IStream ourselves so that IStream_Read can return
492 E_PENDING */
494 hres = CreateStreamOnHGlobal(0, TRUE, (IStream**)ppvObject);
497 if(SUCCEEDED(hres)) {
498 TRACE("Created dummy stream...\n");
500 hres = IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown**)&pbscb);
501 if(SUCCEEDED(hres)) {
502 TRACE("Got IBindStatusCallback...\n");
504 memset(&bi, 0, sizeof(bi));
505 bi.cbSize = sizeof(bi);
506 bindf = 0;
507 hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
508 if(SUCCEEDED(hres)) {
509 URL_COMPONENTSW url;
510 WCHAR *host, *path;
511 DWORD len, lensz = sizeof(len), total_read = 0;
512 LARGE_INTEGER last_read_pos;
513 FORMATETC fmt;
514 STGMEDIUM stg;
516 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
517 bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
518 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
519 TRACE("OnStartBinding rets %08lx\n", hres);
521 #if 0
522 if(!registered_wndclass) {
523 WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
524 RegisterClassA(&urlmon_wndclass);
525 registered_wndclass = TRUE;
528 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
529 URLMON_hInstance, NULL);
531 #endif
532 memset(&url, 0, sizeof(url));
533 url.dwStructSize = sizeof(url);
534 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
535 InternetCrackUrlW(This->URLName, 0, 0, &url);
536 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
537 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
538 host[url.dwHostNameLength] = '\0';
539 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
540 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
541 path[url.dwUrlPathLength] = '\0';
543 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
544 /* InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
546 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
547 INTERNET_SERVICE_HTTP, 0, (DWORD)This);
548 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
550 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
551 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
552 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
553 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
554 hres = E_OUTOFMEMORY; /* FIXME */
555 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
557 len = 0;
558 HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
560 TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
562 last_read_pos.u.LowPart = last_read_pos.u.HighPart = 0;
563 fmt.cfFormat = 0;
564 fmt.ptd = NULL;
565 fmt.dwAspect = 0;
566 fmt.lindex = -1;
567 fmt.tymed = TYMED_ISTREAM;
568 stg.tymed = TYMED_ISTREAM;
569 stg.u.pstm = *(IStream**)ppvObject;
570 stg.pUnkForRelease = NULL;
572 while(1) {
573 char buf[4096];
574 DWORD bufread;
575 DWORD written;
576 if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
577 TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
578 if(bufread == 0) break;
579 IStream_Write(*(IStream**)ppvObject, buf, bufread, &written);
580 total_read += bufread;
581 IStream_Seek(*(IStream**)ppvObject, last_read_pos, STREAM_SEEK_SET, NULL);
582 hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
583 BINDSTATUS_BEGINDOWNLOADDATA :
584 BINDSTATUS_DOWNLOADINGDATA, NULL);
585 hres = IBindStatusCallback_OnDataAvailable(pbscb,
586 (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
587 BSCF_INTERMEDIATEDATANOTIFICATION,
588 total_read, &fmt, &stg);
589 last_read_pos.u.LowPart += bufread; /* FIXME */
590 } else
591 break;
593 hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
594 hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
595 TRACE("OnDataAvail rets %08lx\n", hres);
596 hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
597 TRACE("OnStop rets %08lx\n", hres);
598 hres = S_OK;
600 InternetCloseHandle(This->hrequest);
601 InternetCloseHandle(This->hconnect);
602 InternetCloseHandle(This->hinternet);
606 return hres;
609 /******************************************************************************
610 * URLMoniker_Reduce
611 ******************************************************************************/
612 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
613 IBindCtx* pbc,
614 DWORD dwReduceHowFar,
615 IMoniker** ppmkToLeft,
616 IMoniker** ppmkReduced)
618 ICOM_THIS(URLMonikerImpl,iface);
620 TRACE("(%p,%p,%ld,%p,%p)\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
622 if(!ppmkReduced)
623 return E_INVALIDARG;
625 URLMonikerImpl_AddRef(iface);
626 *ppmkReduced = iface;
627 return MK_S_REDUCED_TO_SELF;
630 /******************************************************************************
631 * URLMoniker_ComposeWith
632 ******************************************************************************/
633 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
634 IMoniker* pmkRight,
635 BOOL fOnlyIfNotGeneric,
636 IMoniker** ppmkComposite)
638 ICOM_THIS(URLMonikerImpl,iface);
639 FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
641 return E_NOTIMPL;
644 /******************************************************************************
645 * URLMoniker_Enum
646 ******************************************************************************/
647 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
649 ICOM_THIS(URLMonikerImpl,iface);
650 TRACE("(%p,%d,%p)\n",This,fForward,ppenumMoniker);
652 if(!ppenumMoniker)
653 return E_INVALIDARG;
655 /* Does not support sub-monikers */
656 *ppenumMoniker = NULL;
657 return S_OK;
660 /******************************************************************************
661 * URLMoniker_IsEqual
662 ******************************************************************************/
663 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
665 ICOM_THIS(URLMonikerImpl,iface);
666 CLSID clsid;
667 LPOLESTR urlPath;
668 IBindCtx* bind;
669 HRESULT res;
671 TRACE("(%p,%p)\n",This,pmkOtherMoniker);
673 if(pmkOtherMoniker==NULL)
674 return E_INVALIDARG;
676 IMoniker_GetClassID(pmkOtherMoniker,&clsid);
678 if(!IsEqualCLSID(&clsid,&CLSID_StdURLMoniker))
679 return S_FALSE;
681 res = CreateBindCtx(0,&bind);
682 if(FAILED(res))
683 return res;
685 res = S_FALSE;
686 if(SUCCEEDED(IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&urlPath))) {
687 int result = lstrcmpiW(urlPath, This->URLName);
688 CoTaskMemFree(urlPath);
689 if(result == 0)
690 res = S_OK;
692 IUnknown_Release(bind);
693 return res;
697 /******************************************************************************
698 * URLMoniker_Hash
699 ******************************************************************************/
700 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
702 ICOM_THIS(URLMonikerImpl,iface);
704 int h = 0,i,skip,len;
705 int off = 0;
706 LPOLESTR val;
708 TRACE("(%p,%p)\n",This,pdwHash);
710 if(!pdwHash)
711 return E_INVALIDARG;
713 val = This->URLName;
714 len = lstrlenW(val);
716 if(len < 16) {
717 for(i = len ; i > 0; i--) {
718 h = (h * 37) + val[off++];
721 else {
722 /* only sample some characters */
723 skip = len / 8;
724 for(i = len; i > 0; i -= skip, off += skip) {
725 h = (h * 39) + val[off];
728 *pdwHash = h;
729 return S_OK;
732 /******************************************************************************
733 * URLMoniker_IsRunning
734 ******************************************************************************/
735 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
736 IBindCtx* pbc,
737 IMoniker* pmkToLeft,
738 IMoniker* pmkNewlyRunning)
740 ICOM_THIS(URLMonikerImpl,iface);
741 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
743 return E_NOTIMPL;
746 /******************************************************************************
747 * URLMoniker_GetTimeOfLastChange
748 ******************************************************************************/
749 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
750 IBindCtx* pbc,
751 IMoniker* pmkToLeft,
752 FILETIME* pFileTime)
754 ICOM_THIS(URLMonikerImpl,iface);
755 FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
757 return E_NOTIMPL;
760 /******************************************************************************
761 * URLMoniker_Inverse
762 ******************************************************************************/
763 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
765 ICOM_THIS(URLMonikerImpl,iface);
766 TRACE("(%p,%p)\n",This,ppmk);
768 return MK_E_NOINVERSE;
771 /******************************************************************************
772 * URLMoniker_CommonPrefixWith
773 ******************************************************************************/
774 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
776 ICOM_THIS(URLMonikerImpl,iface);
777 FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
779 return E_NOTIMPL;
782 /******************************************************************************
783 * URLMoniker_RelativePathTo
784 ******************************************************************************/
785 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
787 ICOM_THIS(URLMonikerImpl,iface);
788 FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
790 return E_NOTIMPL;
793 /******************************************************************************
794 * URLMoniker_GetDisplayName
795 ******************************************************************************/
796 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
797 IBindCtx* pbc,
798 IMoniker* pmkToLeft,
799 LPOLESTR *ppszDisplayName)
801 ICOM_THIS(URLMonikerImpl,iface);
803 int len;
805 TRACE("(%p,%p,%p,%p)\n",This,pbc,pmkToLeft,ppszDisplayName);
807 if(!ppszDisplayName)
808 return E_INVALIDARG;
810 /* FIXME: If this is a partial URL, try and get a URL moniker from SZ_URLCONTEXT in the bind context,
811 then look at pmkToLeft to try and complete the URL
813 len = lstrlenW(This->URLName)+1;
814 *ppszDisplayName = CoTaskMemAlloc(len*sizeof(WCHAR));
815 if(!*ppszDisplayName)
816 return E_OUTOFMEMORY;
817 lstrcpyW(*ppszDisplayName, This->URLName);
818 return S_OK;
821 /******************************************************************************
822 * URLMoniker_ParseDisplayName
823 ******************************************************************************/
824 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
825 IBindCtx* pbc,
826 IMoniker* pmkToLeft,
827 LPOLESTR pszDisplayName,
828 ULONG* pchEaten,
829 IMoniker** ppmkOut)
831 ICOM_THIS(URLMonikerImpl,iface);
832 FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
834 return E_NOTIMPL;
837 /******************************************************************************
838 * URLMoniker_IsSystemMoniker
839 ******************************************************************************/
840 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
842 ICOM_THIS(URLMonikerImpl,iface);
843 TRACE("(%p,%p)\n",This,pwdMksys);
845 if(!pwdMksys)
846 return E_INVALIDARG;
848 *pwdMksys = MKSYS_URLMONIKER;
849 return S_OK;
852 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
854 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
856 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
858 /* Perform a sanity check on the parameters.*/
859 if ( (This==0) || (ppvObject==0) )
860 return E_INVALIDARG;
862 /* Initialize the return parameter */
863 *ppvObject = 0;
865 /* Compare the riid with the interface IDs implemented by this object.*/
866 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
867 *ppvObject = iface;
869 /* Check that we obtained an interface.*/
870 if ((*ppvObject)==0)
871 return E_NOINTERFACE;
873 /* Query Interface always increases the reference count by one when it is successful */
874 URLMonikerImpl_IBinding_AddRef(iface);
876 return S_OK;
880 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
882 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
883 TRACE("(%p)\n",This);
885 return URLMonikerImpl_AddRef((IMoniker*)This);
888 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
890 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
891 TRACE("(%p)\n",This);
893 return URLMonikerImpl_Release((IMoniker*)This);
896 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
898 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
899 FIXME("(%p): stub\n", This);
901 return E_NOTIMPL;
904 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
906 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
907 FIXME("(%p)->(%s, %p, %p, %p): stub\n", This, debugstr_guid(pclsidProtocol), pdwResult, pszResult, pdwReserved);
909 return E_NOTIMPL;
912 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
914 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
915 FIXME("(%p)->(%p): stub\n", This, pnPriority);
917 return E_NOTIMPL;
920 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
922 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
923 FIXME("(%p): stub\n", This);
925 return E_NOTIMPL;
928 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
930 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
931 FIXME("(%p)->(%ld): stub\n", This, nPriority);
933 return E_NOTIMPL;
936 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
938 ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
939 FIXME("(%p): stub\n", This);
941 return E_NOTIMPL;
944 /***********************************************************************
945 * CreateAsyncBindCtx (URLMON.@)
947 HRESULT WINAPI CreateAsyncBindCtx(DWORD reserved, IBindStatusCallback *callback,
948 IEnumFORMATETC *format, IBindCtx **pbind)
950 FIXME("stub.\n");
951 return E_INVALIDARG;
953 /***********************************************************************
954 * CreateAsyncBindCtxEx (URLMON.@)
956 * Create an asynchronous bind context.
958 * FIXME
959 * Not implemented.
961 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
962 IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
963 DWORD reserved)
965 FIXME("stub, returns failure\n");
966 return E_INVALIDARG;
970 /***********************************************************************
971 * CreateURLMoniker (URLMON.@)
973 * Create a url moniker.
975 * PARAMS
976 * pmkContext [I] Context
977 * szURL [I] Url to create the moniker for
978 * ppmk [O] Destination for created moniker.
980 * RETURNS
981 * Success: S_OK. ppmk contains the created IMoniker object.
982 * Failure: MK_E_SYNTAX if szURL is not a valid url, or
983 * E_OUTOFMEMORY if memory allocation fails.
985 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
987 URLMonikerImpl *obj;
988 HRESULT hres;
989 IID iid = IID_IMoniker;
990 LPOLESTR lefturl = NULL;
992 TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
994 if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
995 return E_OUTOFMEMORY;
997 if(pmkContext) {
998 CLSID clsid;
999 IBindCtx* bind;
1000 IMoniker_GetClassID(pmkContext, &clsid);
1001 if(IsEqualCLSID(&clsid, &CLSID_StdURLMoniker) && SUCCEEDED(CreateBindCtx(0, &bind))) {
1002 URLMonikerImpl_GetDisplayName(pmkContext, bind, NULL, &lefturl);
1003 IBindCtx_Release(bind);
1007 hres = URLMonikerImpl_Construct(obj, lefturl, szURL);
1008 CoTaskMemFree(lefturl);
1009 if(SUCCEEDED(hres))
1010 hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
1011 else
1012 HeapFree(GetProcessHeap(), 0, obj);
1013 return hres;
1017 /***********************************************************************
1018 * CoInternetGetSession (URLMON.@)
1020 * Create a new internet session and return an IInternetSession interface
1021 * representing it.
1023 * PARAMS
1024 * dwSessionMode [I] Mode for the internet session
1025 * ppIInternetSession [O] Destination for creates IInternetSession object
1026 * dwReserved [I] Reserved, must be 0.
1028 * RETURNS
1029 * Success: S_OK. ppIInternetSession contains the IInternetSession interface.
1030 * Failure: E_INVALIDARG, if any argument is invalid, or
1031 * E_OUTOFMEMORY if memory allocation fails.
1033 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession, DWORD dwReserved)
1035 FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
1037 if(dwSessionMode) {
1038 ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
1041 if(dwReserved) {
1042 ERR("dwReserved: %ld, must be zero\n", dwReserved);
1045 *ppIInternetSession=NULL;
1046 return E_OUTOFMEMORY;
1049 /***********************************************************************
1050 * CoInternetQueryInfo (URLMON.@)
1052 * Retrieves information relevant to a specified URL
1054 * RETURNS
1055 * S_OK success
1056 * S_FALSE buffer too small
1057 * INET_E_QUERYOPTIONUNKNOWN invalid option
1060 HRESULT WINAPI CoInternetQueryInfo(LPCWSTR pwzUrl, QUERYOPTION QueryOption,
1061 DWORD dwQueryFlags, LPVOID pvBuffer, DWORD cbBuffer, DWORD * pcbBuffer,
1062 DWORD dwReserved)
1064 FIXME("(%s, %x, %lx, %p, %lx, %p, %lx): stub\n", debugstr_w(pwzUrl),
1065 QueryOption, dwQueryFlags, pvBuffer, cbBuffer, pcbBuffer, dwReserved);
1066 return S_OK;
1069 static BOOL URLMON_IsBinary(LPVOID pBuffer, DWORD cbSize)
1071 int binarycount = 0;
1072 int i;
1073 unsigned char *buff = pBuffer;
1074 for(i=0; i<cbSize; i++) {
1075 if(buff[i] < 32)
1076 binarycount++;
1078 return binarycount > (cbSize-binarycount);
1081 /***********************************************************************
1082 * FindMimeFromData (URLMON.@)
1084 * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
1086 * NOTE
1087 * See http://msdn.microsoft.com/workshop/networking/moniker/overview/appendix_a.asp
1089 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer,
1090 DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
1091 LPWSTR* ppwzMimeOut, DWORD dwReserved)
1093 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'};
1094 static const WCHAR szTextMime[] = {'t','e','x','t','/','p','l','a','i','n','\0'};
1095 static const WCHAR szContentType[] = {'C','o','n','t','e','n','t',' ','T','y','p','e','\0'};
1096 WCHAR szTmpMime[256];
1097 LPCWSTR mimeType = NULL;
1098 HKEY hKey = NULL;
1100 TRACE("(%p,%s,%p,%ld,%s,0x%lx,%p,0x%lx)\n", pBC, debugstr_w(pwzUrl), pBuffer, cbSize,
1101 debugstr_w(pwzMimeProposed), dwMimeFlags, ppwzMimeOut, dwReserved);
1103 if((!pwzUrl && (!pBuffer || cbSize <= 0)) || !ppwzMimeOut)
1104 return E_INVALIDARG;
1106 if(pwzMimeProposed)
1107 mimeType = pwzMimeProposed;
1108 else {
1109 /* Try and find the mime type in the registry */
1110 if(pwzUrl) {
1111 LPWSTR ext = strrchrW(pwzUrl, '.');
1112 if(ext) {
1113 DWORD dwSize;
1114 if(!RegOpenKeyExW(HKEY_CLASSES_ROOT, ext, 0, 0, &hKey)) {
1115 if(!RegQueryValueExW(hKey, szContentType, NULL, NULL, (LPBYTE)szTmpMime, &dwSize)) {
1116 mimeType = szTmpMime;
1118 RegCloseKey(hKey);
1123 if(!mimeType && pBuffer && cbSize > 0)
1124 mimeType = URLMON_IsBinary(pBuffer, cbSize)?szBinaryMime:szTextMime;
1126 TRACE("Using %s\n", debugstr_w(mimeType));
1127 *ppwzMimeOut = CoTaskMemAlloc((lstrlenW(mimeType)+1)*sizeof(WCHAR));
1128 if(!*ppwzMimeOut) return E_OUTOFMEMORY;
1129 lstrcpyW(*ppwzMimeOut, mimeType);
1130 return S_OK;
1133 /***********************************************************************
1134 * IsAsyncMoniker (URLMON.@)
1136 HRESULT WINAPI IsAsyncMoniker(IMoniker *pmk)
1138 IUnknown *am;
1140 TRACE("(%p)\n", pmk);
1141 if(!pmk)
1142 return E_INVALIDARG;
1143 if(SUCCEEDED(IMoniker_QueryInterface(pmk, &IID_IAsyncMoniker, (void**)&am))) {
1144 IUnknown_Release(am);
1145 return S_OK;
1147 return S_FALSE;
1150 /***********************************************************************
1151 * RegisterBindStatusCallback (URLMON.@)
1153 * Register a bind status callback.
1155 * PARAMS
1156 * pbc [I] Binding context
1157 * pbsc [I] Callback to register
1158 * ppbscPrevious [O] Destination for previous callback
1159 * dwReserved [I] Reserved, must be 0.
1161 * RETURNS
1162 * Success: S_OK.
1163 * Failure: E_INVALIDARG, if any argument is invalid, or
1164 * E_OUTOFMEMORY if memory allocation fails.
1166 HRESULT WINAPI RegisterBindStatusCallback(
1167 IBindCtx *pbc,
1168 IBindStatusCallback *pbsc,
1169 IBindStatusCallback **ppbscPrevious,
1170 DWORD dwReserved)
1172 IBindStatusCallback *prev;
1174 TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
1176 if (pbc == NULL || pbsc == NULL)
1177 return E_INVALIDARG;
1179 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&prev)))
1181 IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1182 if (ppbscPrevious)
1183 *ppbscPrevious = prev;
1184 else
1185 IBindStatusCallback_Release(prev);
1188 return IBindCtx_RegisterObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown *)pbsc);
1191 /***********************************************************************
1192 * RevokeBindStatusCallback (URLMON.@)
1194 * Unregister a bind status callback.
1196 * pbc [I] Binding context
1197 * pbsc [I] Callback to unregister
1199 * RETURNS
1200 * Success: S_OK.
1201 * Failure: E_INVALIDARG, if any argument is invalid, or
1202 * E_FAIL if pbsc wasn't registered with pbc.
1204 HRESULT WINAPI RevokeBindStatusCallback(
1205 IBindCtx *pbc,
1206 IBindStatusCallback *pbsc)
1208 IBindStatusCallback *callback;
1209 HRESULT hr = E_FAIL;
1211 TRACE("(%p,%p)\n", pbc, pbsc);
1213 if (pbc == NULL || pbsc == NULL)
1214 return E_INVALIDARG;
1216 if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, (LPOLESTR)BSCBHolder, (IUnknown **)&callback)))
1218 if (callback == pbsc)
1220 IBindCtx_RevokeObjectParam(pbc, (LPOLESTR)BSCBHolder);
1221 hr = S_OK;
1223 IBindStatusCallback_Release(pbsc);
1226 return hr;
1229 /***********************************************************************
1230 * ReleaseBindInfo (URLMON.@)
1232 * Release the resources used by the specified BINDINFO structure.
1234 * PARAMS
1235 * pbindinfo [I] BINDINFO to release.
1237 * RETURNS
1238 * Nothing.
1240 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
1242 FIXME("(%p)stub!\n", pbindinfo);
1245 /***********************************************************************
1246 * URLDownloadToFileA (URLMON.@)
1248 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1249 * report progress.
1251 * PARAMS
1252 * pCaller [I] controlling IUnknown interface.
1253 * szURL [I] URL of the file to download
1254 * szFileName [I] file name to store the content of the URL
1255 * dwReserved [I] reserved - set to 0
1256 * lpfnCB [I] callback for progress report
1258 * RETURNS
1259 * S_OK on success
1260 * E_OUTOFMEMORY when going out of memory
1262 HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
1263 LPCSTR szURL,
1264 LPCSTR szFileName,
1265 DWORD dwReserved,
1266 LPBINDSTATUSCALLBACK lpfnCB)
1268 UNICODE_STRING szURL_w, szFileName_w;
1270 if ((szURL == NULL) || (szFileName == NULL)) {
1271 FIXME("(%p,%s,%s,%08lx,%p) cannot accept NULL strings !\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB);
1272 return E_INVALIDARG; /* The error code is not specified in this case... */
1275 if (RtlCreateUnicodeStringFromAsciiz(&szURL_w, szURL)) {
1276 if (RtlCreateUnicodeStringFromAsciiz(&szFileName_w, szFileName)) {
1277 HRESULT ret = URLDownloadToFileW(pCaller, szURL_w.Buffer, szFileName_w.Buffer, dwReserved, lpfnCB);
1279 RtlFreeUnicodeString(&szURL_w);
1280 RtlFreeUnicodeString(&szFileName_w);
1282 return ret;
1283 } else {
1284 RtlFreeUnicodeString(&szURL_w);
1288 FIXME("(%p,%s,%s,%08lx,%p) could not allocate W strings !\n", pCaller, szURL, szFileName, dwReserved, lpfnCB);
1289 return E_OUTOFMEMORY;
1292 /***********************************************************************
1293 * URLDownloadToFileW (URLMON.@)
1295 * Downloads URL szURL to rile szFileName and call lpfnCB callback to
1296 * report progress.
1298 * PARAMS
1299 * pCaller [I] controlling IUnknown interface.
1300 * szURL [I] URL of the file to download
1301 * szFileName [I] file name to store the content of the URL
1302 * dwReserved [I] reserved - set to 0
1303 * lpfnCB [I] callback for progress report
1305 * RETURNS
1306 * S_OK on success
1307 * E_OUTOFMEMORY when going out of memory
1309 HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
1310 LPCWSTR szURL,
1311 LPCWSTR szFileName,
1312 DWORD dwReserved,
1313 LPBINDSTATUSCALLBACK lpfnCB)
1315 HINTERNET hinternet, hcon, hreq;
1316 BOOL r;
1317 CHAR buffer[0x1000];
1318 DWORD sz, total, written;
1319 DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
1320 URL_COMPONENTSW url;
1321 WCHAR host[0x80], path[0x100];
1322 HANDLE hfile;
1323 static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
1325 /* Note: all error codes would need to be checked agains real Windows behaviour... */
1326 TRACE("(%p,%s,%s,%08lx,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
1328 if ((szURL == NULL) || (szFileName == NULL)) {
1329 FIXME(" cannot accept NULL strings !\n");
1330 return E_INVALIDARG;
1333 /* Would be better to use the application name here rather than 'urlmon' :-/ */
1334 hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1335 if (hinternet == NULL) {
1336 return E_OUTOFMEMORY;
1339 memset(&url, 0, sizeof(url));
1340 url.dwStructSize = sizeof(url);
1341 url.lpszHostName = host;
1342 url.dwHostNameLength = sizeof(host);
1343 url.lpszUrlPath = path;
1344 url.dwUrlPathLength = sizeof(path);
1346 if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
1347 InternetCloseHandle(hinternet);
1348 return E_OUTOFMEMORY;
1351 if (lpfnCB) {
1352 if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
1353 InternetCloseHandle(hinternet);
1354 return S_OK;
1358 hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
1359 url.lpszUserName, url.lpszPassword,
1360 INTERNET_SERVICE_HTTP, 0, 0);
1361 if (!hcon) {
1362 InternetCloseHandle(hinternet);
1363 return E_OUTOFMEMORY;
1366 hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
1367 if (!hreq) {
1368 InternetCloseHandle(hinternet);
1369 InternetCloseHandle(hcon);
1370 return E_OUTOFMEMORY;
1373 if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
1374 InternetCloseHandle(hinternet);
1375 InternetCloseHandle(hcon);
1376 InternetCloseHandle(hreq);
1377 return E_OUTOFMEMORY;
1380 if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
1381 &total_size, &arg_size, NULL)) {
1382 TRACE(" total size : %ld\n", total_size);
1385 hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1386 FILE_ATTRIBUTE_NORMAL, NULL );
1387 if (hfile == INVALID_HANDLE_VALUE) {
1388 return E_ACCESSDENIED;
1391 if (lpfnCB) {
1392 if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
1393 BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
1394 InternetCloseHandle(hreq);
1395 InternetCloseHandle(hcon);
1396 InternetCloseHandle(hinternet);
1397 CloseHandle(hfile);
1398 return S_OK;
1402 total = 0;
1403 while (1) {
1404 r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
1405 if (!r) {
1406 InternetCloseHandle(hreq);
1407 InternetCloseHandle(hcon);
1408 InternetCloseHandle(hinternet);
1410 CloseHandle(hfile);
1411 return E_OUTOFMEMORY;
1413 if (!sz)
1414 break;
1416 total += sz;
1418 if (lpfnCB) {
1419 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1420 BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
1421 InternetCloseHandle(hreq);
1422 InternetCloseHandle(hcon);
1423 InternetCloseHandle(hinternet);
1424 CloseHandle(hfile);
1425 return S_OK;
1429 if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
1430 InternetCloseHandle(hreq);
1431 InternetCloseHandle(hcon);
1432 InternetCloseHandle(hinternet);
1434 CloseHandle(hfile);
1435 return E_OUTOFMEMORY;
1439 if (lpfnCB) {
1440 if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
1441 BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
1442 InternetCloseHandle(hreq);
1443 InternetCloseHandle(hcon);
1444 InternetCloseHandle(hinternet);
1445 CloseHandle(hfile);
1446 return S_OK;
1450 InternetCloseHandle(hreq);
1451 InternetCloseHandle(hcon);
1452 InternetCloseHandle(hinternet);
1454 CloseHandle(hfile);
1456 return S_OK;