winspool/tests: Use 0xdeadbeef as magic value.
[wine.git] / dlls / shdocvw / shdocvw_main.c
blob569d3a21b13016d41e385a88170da4bd5f8cd7f7
1 /*
2 * SHDOCVW - Internet Explorer Web Control
4 * Copyright 2001 John R. Sheets (for CodeWeavers)
5 * Copyright 2004 Mike McCormack (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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
30 #include "shdocvw.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
35 #include "initguid.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
39 LONG SHDOCVW_refCount = 0;
41 static const WCHAR szMozDlPath[] = {
42 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
43 's','h','d','o','c','v','w',0
46 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
48 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
49 typedef HRESULT (WINAPI *fnCanUnloadNow)(void);
51 HINSTANCE shdocvw_hinstance = 0;
52 static HMODULE SHDOCVW_hshell32 = 0;
53 static HMODULE hMozCtl = (HMODULE)~0UL;
56 /* convert a guid to a wide character string */
57 static void SHDOCVW_guid2wstr( const GUID *guid, LPWSTR wstr )
59 char str[40];
61 sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
62 guid->Data1, guid->Data2, guid->Data3,
63 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
64 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
65 MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
68 static BOOL SHDOCVW_GetMozctlPath( LPWSTR szPath, DWORD sz )
70 DWORD r, type;
71 BOOL ret = FALSE;
72 HKEY hkey;
73 static const WCHAR szPre[] = {
74 'S','o','f','t','w','a','r','e','\\',
75 'C','l','a','s','s','e','s','\\',
76 'C','L','S','I','D','\\',0 };
77 static const WCHAR szPost[] = {
78 '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
79 WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
81 strcpyW( szRegPath, szPre );
82 SHDOCVW_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
83 strcatW( szRegPath, szPost );
85 TRACE("key = %s\n", debugstr_w( szRegPath ) );
87 r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
88 if( r != ERROR_SUCCESS )
89 return FALSE;
91 r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
92 ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
93 RegCloseKey( hkey );
95 return ret;
98 /*************************************************************************
99 * SHDOCVW DllMain
101 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
103 TRACE("%p 0x%lx %p\n", hinst, fdwReason, fImpLoad);
104 switch (fdwReason)
106 case DLL_PROCESS_ATTACH:
107 shdocvw_hinstance = hinst;
108 register_iewindow_class();
109 break;
110 case DLL_PROCESS_DETACH:
111 if (SHDOCVW_hshell32) FreeLibrary(SHDOCVW_hshell32);
112 if (hMozCtl && hMozCtl != (HMODULE)~0UL) FreeLibrary(hMozCtl);
113 unregister_iewindow_class();
114 break;
116 return TRUE;
119 /*************************************************************************
120 * DllCanUnloadNow (SHDOCVW.@)
122 HRESULT WINAPI DllCanUnloadNow(void)
124 HRESULT moz_can_unload = S_OK;
125 fnCanUnloadNow pCanUnloadNow;
127 if (hMozCtl)
129 pCanUnloadNow = (fnCanUnloadNow)
130 GetProcAddress(hMozCtl, "DllCanUnloadNow");
131 if (pCanUnloadNow)
132 moz_can_unload = pCanUnloadNow();
135 if (moz_can_unload == S_OK && SHDOCVW_refCount == 0)
136 return S_OK;
138 return S_FALSE;
141 /*************************************************************************
142 * SHDOCVW_TryDownloadMozillaControl
144 typedef struct _IBindStatusCallbackImpl {
145 const IBindStatusCallbackVtbl *vtbl;
146 LONG ref;
147 HWND hDialog;
148 BOOL *pbCancelled;
149 } IBindStatusCallbackImpl;
151 static HRESULT WINAPI
152 dlQueryInterface( IBindStatusCallback* This, REFIID riid, void** ppvObject )
154 if (ppvObject == NULL) return E_POINTER;
156 if( IsEqualIID(riid, &IID_IUnknown) ||
157 IsEqualIID(riid, &IID_IBindStatusCallback))
159 IBindStatusCallback_AddRef( This );
160 *ppvObject = This;
161 return S_OK;
163 return E_NOINTERFACE;
166 static ULONG WINAPI dlAddRef( IBindStatusCallback* iface )
168 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
170 SHDOCVW_LockModule();
172 return InterlockedIncrement( &This->ref );
175 static ULONG WINAPI dlRelease( IBindStatusCallback* iface )
177 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
178 DWORD ref = InterlockedDecrement( &This->ref );
180 if( !ref )
182 DestroyWindow( This->hDialog );
183 shdocvw_free(This);
186 SHDOCVW_UnlockModule();
188 return ref;
191 static HRESULT WINAPI
192 dlOnStartBinding( IBindStatusCallback* iface, DWORD dwReserved, IBinding* pib)
194 ERR("\n");
195 return S_OK;
198 static HRESULT WINAPI
199 dlGetPriority( IBindStatusCallback* iface, LONG* pnPriority)
201 ERR("\n");
202 return S_OK;
205 static HRESULT WINAPI
206 dlOnLowResource( IBindStatusCallback* iface, DWORD reserved)
208 ERR("\n");
209 return S_OK;
212 static HRESULT WINAPI
213 dlOnProgress( IBindStatusCallback* iface, ULONG ulProgress,
214 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
216 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
217 HWND hItem;
218 LONG r;
220 hItem = GetDlgItem( This->hDialog, 1000 );
221 if( hItem && ulProgressMax )
222 SendMessageW(hItem,PBM_SETPOS,(ulProgress*100)/ulProgressMax,0);
224 hItem = GetDlgItem(This->hDialog, 104);
225 if( hItem )
226 SendMessageW(hItem,WM_SETTEXT, 0, (LPARAM) szStatusText);
228 SetLastError(0);
229 r = GetWindowLongPtrW( This->hDialog, GWLP_USERDATA );
230 if( r || GetLastError() )
232 *This->pbCancelled = TRUE;
233 ERR("Cancelled\n");
234 return E_ABORT;
237 return S_OK;
240 static HRESULT WINAPI
241 dlOnStopBinding( IBindStatusCallback* iface, HRESULT hresult, LPCWSTR szError)
243 ERR("\n");
244 return S_OK;
247 static HRESULT WINAPI
248 dlGetBindInfo( IBindStatusCallback* iface, DWORD* grfBINDF, BINDINFO* pbindinfo)
250 ERR("\n");
251 return S_OK;
254 static HRESULT WINAPI
255 dlOnDataAvailable( IBindStatusCallback* iface, DWORD grfBSCF,
256 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
258 ERR("\n");
259 return S_OK;
262 static HRESULT WINAPI
263 dlOnObjectAvailable( IBindStatusCallback* iface, REFIID riid, IUnknown* punk)
265 ERR("\n");
266 return S_OK;
269 static const IBindStatusCallbackVtbl dlVtbl =
271 dlQueryInterface,
272 dlAddRef,
273 dlRelease,
274 dlOnStartBinding,
275 dlGetPriority,
276 dlOnLowResource,
277 dlOnProgress,
278 dlOnStopBinding,
279 dlGetBindInfo,
280 dlOnDataAvailable,
281 dlOnObjectAvailable
284 static IBindStatusCallback* create_dl(HWND dlg, BOOL *pbCancelled)
286 IBindStatusCallbackImpl *This;
288 This = shdocvw_alloc(sizeof(*This));
289 This->vtbl = &dlVtbl;
290 This->ref = 1;
291 This->hDialog = dlg;
292 This->pbCancelled = pbCancelled;
294 return (IBindStatusCallback*) This;
297 static DWORD WINAPI ThreadFunc( LPVOID info )
299 IBindStatusCallback *dl;
300 static const WCHAR szUrlVal[] = {'M','o','z','i','l','l','a','U','r','l',0};
301 static const WCHAR szFileProtocol[] = {'f','i','l','e',':','/','/','/',0};
302 static const WCHAR szSilentParameter[] = {' ','/','S',0};
303 WCHAR path[MAX_PATH], szUrl[MAX_PATH];
304 WCHAR szCommandLine[MAX_PATH+(sizeof( szSilentParameter )/sizeof( WCHAR ))];
305 LPWSTR p;
306 STARTUPINFOW si;
307 PROCESS_INFORMATION pi;
308 HWND hDlg = info;
309 DWORD r, sz, type;
310 HKEY hkey;
311 BOOL bCancelled = FALSE;
312 BOOL bTempfile = FALSE;
314 /* find the name of the thing to download */
315 szUrl[0] = 0;
316 /* @@ Wine registry key: HKCU\Software\Wine\shdocvw */
317 r = RegOpenKeyW( HKEY_CURRENT_USER, szMozDlPath, &hkey );
318 if( r == ERROR_SUCCESS )
320 sz = MAX_PATH;
321 r = RegQueryValueExW( hkey, szUrlVal, NULL, &type, (LPBYTE)szUrl, &sz );
322 RegCloseKey( hkey );
324 if( r != ERROR_SUCCESS )
325 goto end;
327 if( !strncmpW(szUrl, szFileProtocol, strlenW(szFileProtocol)) )
328 lstrcpynW( path, szUrl+strlenW(szFileProtocol), MAX_PATH );
329 else
331 /* built the path for the download */
332 p = strrchrW( szUrl, '/' );
333 if (!p)
334 goto end;
335 if (!GetTempPathW( MAX_PATH, path ))
336 goto end;
337 strcatW( path, p+1 );
339 /* download it */
340 bTempfile = TRUE;
341 dl = create_dl(info, &bCancelled);
342 r = URLDownloadToFileW( NULL, szUrl, path, 0, dl );
343 if( dl )
344 IBindStatusCallback_Release( dl );
345 if( (r != S_OK) || bCancelled )
346 goto end;
349 /* run it */
350 memset( &si, 0, sizeof si );
351 si.cb = sizeof si;
352 strcpyW( szCommandLine, path );
353 strcatW( szCommandLine, szSilentParameter );
354 r = CreateProcessW( path, szCommandLine, NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
355 if( !r )
356 goto end;
357 WaitForSingleObject( pi.hProcess, INFINITE );
358 CloseHandle( pi.hProcess );
359 CloseHandle( pi.hThread );
361 end:
362 if( bTempfile )
363 DeleteFileW( path );
364 EndDialog( hDlg, 0 );
365 return 0;
368 static INT_PTR CALLBACK
369 dlProc ( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
371 HANDLE hThread;
372 DWORD ThreadId;
373 HWND hItem;
375 switch (uMsg)
377 case WM_INITDIALOG:
378 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 0 );
379 hItem = GetDlgItem(hwndDlg, 1000);
380 if( hItem )
382 SendMessageW(hItem,PBM_SETRANGE,0,MAKELPARAM(0,100));
383 SendMessageW(hItem,PBM_SETPOS,0,0);
385 hThread = CreateThread(NULL,0,ThreadFunc,hwndDlg,0,&ThreadId);
386 if (!hThread)
387 return FALSE;
388 return TRUE;
389 case WM_COMMAND:
390 if( wParam == IDCANCEL )
391 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 1 );
392 return FALSE;
393 default:
394 return FALSE;
398 static BOOL SHDOCVW_TryDownloadMozillaControl(void)
400 DWORD r;
401 WCHAR buf[0x100];
402 static const WCHAR szWine[] = { 'W','i','n','e',0 };
403 HANDLE hsem;
405 SetLastError( ERROR_SUCCESS );
406 hsem = CreateSemaphoreA( NULL, 0, 1, "mozctl_install_semaphore");
407 if( GetLastError() != ERROR_ALREADY_EXISTS )
409 LoadStringW( shdocvw_hinstance, 1001, buf, sizeof buf/sizeof(WCHAR) );
410 r = MessageBoxW(NULL, buf, szWine, MB_YESNO | MB_ICONQUESTION);
411 if( r != IDYES )
412 return FALSE;
414 DialogBoxW(shdocvw_hinstance, MAKEINTRESOURCEW(100), 0, dlProc);
416 else
417 WaitForSingleObject( hsem, INFINITE );
418 ReleaseSemaphore( hsem, 1, NULL );
419 CloseHandle( hsem );
421 return TRUE;
424 static BOOL SHDOCVW_TryLoadMozillaControl(void)
426 WCHAR szPath[MAX_PATH];
427 BOOL bTried = FALSE;
429 if( hMozCtl != (HMODULE)~0UL )
430 return hMozCtl ? TRUE : FALSE;
432 while( 1 )
434 if( SHDOCVW_GetMozctlPath( szPath, sizeof szPath ) )
436 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
437 if( hMozCtl )
438 return TRUE;
440 if( bTried )
442 MESSAGE("You need to install the Mozilla ActiveX control to\n");
443 MESSAGE("use Wine's builtin CLSID_WebBrowser from SHDOCVW.DLL\n");
444 return FALSE;
446 SHDOCVW_TryDownloadMozillaControl();
447 bTried = TRUE;
451 HRESULT create_mozctl(REFIID riid, void **ppv)
453 fnGetClassObject pGetClassObject;
454 HRESULT hres;
456 if(!SHDOCVW_TryLoadMozillaControl())
457 return CLASS_E_CLASSNOTAVAILABLE;
459 pGetClassObject = (fnGetClassObject)GetProcAddress( hMozCtl, "DllGetClassObject" );
461 if( !pGetClassObject )
462 return CLASS_E_CLASSNOTAVAILABLE;
464 hres = pGetClassObject( &CLSID_MozillaBrowser, riid, ppv );
466 TRACE("hres = %08lx *ppv = %p\n", hres, *ppv );
468 return hres;
471 /***********************************************************************
472 * DllGetVersion (SHDOCVW.@)
474 HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *info)
476 if (info->cbSize != sizeof(DLLVERSIONINFO)) FIXME("support DLLVERSIONINFO2\n");
478 /* this is what IE6 on Windows 98 reports */
479 info->dwMajorVersion = 6;
480 info->dwMinorVersion = 0;
481 info->dwBuildNumber = 2600;
482 info->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
484 return NOERROR;
487 /*************************************************************************
488 * DllInstall (SHDOCVW.@)
490 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
492 FIXME("(%s, %s): stub!\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
494 return S_OK;
497 /*************************************************************************
498 * SHDOCVW_LoadShell32
500 * makes sure the handle to shell32 is valid
502 BOOL SHDOCVW_LoadShell32(void)
504 if (SHDOCVW_hshell32)
505 return TRUE;
506 return ((SHDOCVW_hshell32 = LoadLibraryA("shell32.dll")) != NULL);
509 /***********************************************************************
510 * @ (SHDOCVW.110)
512 * Called by Win98 explorer.exe main binary, definitely has 0
513 * parameters.
515 DWORD WINAPI WinList_Init(void)
517 FIXME("(), stub!\n");
518 return 0x0deadfeed;
521 /***********************************************************************
522 * @ (SHDOCVW.118)
524 * Called by Win98 explorer.exe main binary, definitely has only one
525 * parameter.
527 static BOOL (WINAPI *pShellDDEInit)(BOOL start) = NULL;
529 BOOL WINAPI ShellDDEInit(BOOL start)
531 TRACE("(%d)\n", start);
533 if (!pShellDDEInit)
535 if (!SHDOCVW_LoadShell32())
536 return FALSE;
537 pShellDDEInit = GetProcAddress(SHDOCVW_hshell32, (LPCSTR)188);
540 if (pShellDDEInit)
541 return pShellDDEInit(start);
542 else
543 return FALSE;
546 /***********************************************************************
547 * @ (SHDOCVW.125)
549 * Called by Win98 explorer.exe main binary, definitely has 0
550 * parameters.
552 DWORD WINAPI RunInstallUninstallStubs(void)
554 FIXME("(), stub!\n");
555 return 0x0deadbee;
558 /***********************************************************************
559 * SetQueryNetSessionCount (SHDOCVW.@)
561 DWORD WINAPI SetQueryNetSessionCount(DWORD arg)
563 FIXME("(%lu), stub!\n", arg);
564 return 0;
567 /**********************************************************************
568 * OpenURL (SHDOCVW.@)
570 void WINAPI OpenURL(HWND hWnd, HINSTANCE hInst, LPCSTR lpcstrUrl, int nShowCmd)
572 FIXME("%p %p %s %d\n", hWnd, hInst, debugstr_a(lpcstrUrl), nShowCmd);
575 /**********************************************************************
576 * Some forwards (by ordinal) to SHLWAPI
579 static void* fetch_shlwapi_ordinal(unsigned ord)
581 static const WCHAR shlwapiW[] = {'s','h','l','w','a','p','i','.','d','l','l','\0'};
582 static HANDLE h;
584 if (!h && !(h = GetModuleHandleW(shlwapiW))) return NULL;
585 return (void*)GetProcAddress(h, (const char*)ord);
588 /******************************************************************
589 * WhichPlatformFORWARD (SHDOCVW.@)
591 DWORD WINAPI WhichPlatformFORWARD(void)
593 static DWORD (*WINAPI p)(void);
595 if (p || (p = fetch_shlwapi_ordinal(276))) return p();
596 return 1; /* not integrated, see shlwapi.WhichPlatform */
599 /******************************************************************
600 * StopWatchModeFORWARD (SHDOCVW.@)
602 void WINAPI StopWatchModeFORWARD(void)
604 static void (*WINAPI p)(void);
606 if (p || (p = fetch_shlwapi_ordinal(241))) p();
609 /******************************************************************
610 * StopWatchFlushFORWARD (SHDOCVW.@)
612 void WINAPI StopWatchFlushFORWARD(void)
614 static void (*WINAPI p)(void);
616 if (p || (p = fetch_shlwapi_ordinal(242))) p();
619 /******************************************************************
620 * StopWatchWFORWARD (SHDOCVW.@)
622 DWORD WINAPI StopWatchWFORWARD(DWORD dwClass, LPCWSTR lpszStr, DWORD dwUnknown,
623 DWORD dwMode, DWORD dwTimeStamp)
625 static DWORD (*WINAPI p)(DWORD, LPCWSTR, DWORD, DWORD, DWORD);
627 if (p || (p = fetch_shlwapi_ordinal(243)))
628 return p(dwClass, lpszStr, dwUnknown, dwMode, dwTimeStamp);
629 return ERROR_CALL_NOT_IMPLEMENTED;
632 /******************************************************************
633 * StopWatchAFORWARD (SHDOCVW.@)
635 DWORD WINAPI StopWatchAFORWARD(DWORD dwClass, LPCSTR lpszStr, DWORD dwUnknown,
636 DWORD dwMode, DWORD dwTimeStamp)
638 static DWORD (*WINAPI p)(DWORD, LPCSTR, DWORD, DWORD, DWORD);
640 if (p || (p = fetch_shlwapi_ordinal(244)))
641 return p(dwClass, lpszStr, dwUnknown, dwMode, dwTimeStamp);
642 return ERROR_CALL_NOT_IMPLEMENTED;