Removed the DOS version option, specifying the Windows version should
[wine] / dlls / mshtml / main.c
1 /*
2  *    MSHTML Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2003 Mike McCormack
6  * Copyright 2005 Jacek Caban
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "ole2.h"
36 #include "docobj.h"
37
38 #include "mshtml.h"
39 #include "mshtml_private.h"
40
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
45
46 #include "initguid.h"
47
48 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
49
50 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
51 typedef BOOL (WINAPI *fnCanUnloadNow)();
52
53 static HMODULE hMozCtl;
54
55 HINSTANCE hInst;
56
57 /* convert a guid to a wide character string */
58 static void MSHTML_guid2wstr( const GUID *guid, LPWSTR wstr )
59 {
60     char str[40];
61
62     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
63            guid->Data1, guid->Data2, guid->Data3,
64            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
65            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
66     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
67 }
68
69 static BOOL MSHTML_GetMozctlPath( LPWSTR szPath, DWORD sz )
70 {
71     DWORD r, type;
72     BOOL ret = FALSE;
73     HKEY hkey;
74     static const WCHAR szPre[] = {
75         'S','o','f','t','w','a','r','e','\\',
76         'C','l','a','s','s','e','s','\\',
77         'C','L','S','I','D','\\',0 };
78     static const WCHAR szPost[] = {
79         '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
80     WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
81
82     strcpyW( szRegPath, szPre );
83     MSHTML_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
84     strcatW( szRegPath, szPost );
85
86     TRACE("key = %s\n", debugstr_w( szRegPath ) );
87
88     r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
89     if( r != ERROR_SUCCESS )
90         return FALSE;
91
92     r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
93     ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
94     RegCloseKey( hkey );
95
96     return ret;
97 }
98
99 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
100 {
101     WCHAR szPath[MAX_PATH];
102
103     switch(fdwReason) {
104         case DLL_PROCESS_ATTACH:
105             if(MSHTML_GetMozctlPath(szPath, sizeof szPath)) {
106                 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
107                 if(!hMozCtl)
108                     ERR("Can't load the Mozilla ActiveX control\n");
109             }else {
110                 MESSAGE("You need to install the Mozilla ActiveX control to\n");
111                 MESSAGE("use Wine's builtin MSHTML dll.\n");
112             }
113             hInst = hInstDLL;
114             break;
115         case DLL_PROCESS_DETACH:
116             if(hMozCtl)
117                 FreeLibrary( hMozCtl );
118             break;
119     }
120     return TRUE;
121 }
122
123 /***********************************************************
124  *    ClassFactory implementation
125  */
126 typedef HRESULT (*CreateInstanceFunc)(IUnknown*,REFIID,void**);
127 typedef struct {
128     const IClassFactoryVtbl *lpVtbl;
129     ULONG ref;
130     CreateInstanceFunc fnCreateInstance;
131 } ClassFactory;
132
133 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFGUID riid, void **ppvObject)
134 {
135     if(IsEqualGUID(&IID_IClassFactory, riid) || IsEqualGUID(&IID_IUnknown, riid)) {
136         IClassFactory_AddRef(iface);
137         *ppvObject = iface;
138         return S_OK;
139     }
140
141     WARN("not supported iid %s\n", debugstr_guid(riid));
142     *ppvObject = NULL;
143     return E_NOINTERFACE;
144 }
145
146 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
147 {
148     ClassFactory *This = (ClassFactory*)iface;
149     ULONG ref = InterlockedIncrement(&This->ref);
150     TRACE("(%p) ref = %lu\n", This, ref);
151     return ref;
152 }
153
154 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
155 {
156     ClassFactory *This = (ClassFactory*)iface;
157     ULONG ref = InterlockedDecrement(&This->ref);
158
159     TRACE("(%p) ref = %lu\n", This, ref);
160
161     if(!ref)
162         HeapFree(GetProcessHeap(), 0, This);
163
164     return ref;
165 }
166
167 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
168         REFIID riid, void **ppvObject)
169 {
170     ClassFactory *This = (ClassFactory*)iface;
171     return This->fnCreateInstance(pUnkOuter, riid, ppvObject);
172 }
173
174 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
175 {
176     FIXME("(%p)->(%x) stub\n", iface, dolock);
177     return S_OK;
178 }
179
180 static const IClassFactoryVtbl HTMLClassFactoryVtbl = {
181     ClassFactory_QueryInterface,
182     ClassFactory_AddRef,
183     ClassFactory_Release,
184     ClassFactory_CreateInstance,
185     ClassFactory_LockServer
186 };
187
188 static HRESULT ClassFactory_Create(REFIID riid, void **ppv, CreateInstanceFunc fnCreateInstance)
189 {
190     ClassFactory *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactory));
191     HRESULT hres;
192
193     ret->lpVtbl = &HTMLClassFactoryVtbl;
194     ret->ref = 0;
195     ret->fnCreateInstance = fnCreateInstance;
196
197     hres = IClassFactory_QueryInterface((IClassFactory*)ret, riid, ppv);
198     if(FAILED(hres)) {
199         HeapFree(GetProcessHeap(), 0, ret);
200         *ppv = NULL;
201     }
202     return hres;
203 }
204
205 HRESULT WINAPI MSHTML_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
206 {
207     HRESULT hres;
208     fnGetClassObject pGetClassObject;
209
210     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv );
211
212     if(hMozCtl && IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
213         pGetClassObject = (fnGetClassObject) GetProcAddress(hMozCtl, "DllGetClassObject");
214         if(pGetClassObject) {
215             hres = pGetClassObject(&CLSID_MozillaBrowser, riid, ppv);
216             if(SUCCEEDED(hres)) {
217                 TRACE("returning Mozilla ActiveX Control hres = %08lx  *ppv = %p\n", hres, *ppv);
218                 return hres;
219             }
220         }
221     }
222
223     if(IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
224         hres = ClassFactory_Create(riid, ppv, HTMLDocument_Create);
225         TRACE("hres = %08lx\n", hres);
226         return hres;
227     }
228
229     FIXME("Unknown class %s\n", debugstr_guid(rclsid));
230     return CLASS_E_CLASSNOTAVAILABLE;
231 }
232
233 HRESULT WINAPI MSHTML_DllCanUnloadNow(void)
234 {
235     fnCanUnloadNow pCanUnloadNow = NULL;
236     HRESULT hres;
237
238     TRACE("()\n");
239
240     if(hMozCtl)
241         pCanUnloadNow = (fnCanUnloadNow) GetProcAddress(hMozCtl, "DllCanUnloadNow");
242     if(!pCanUnloadNow)
243         return S_FALSE;
244
245     hres = pCanUnloadNow();
246
247     TRACE("hres = %08lx\n", hres);
248
249     return hres;
250 }
251
252 /***********************************************************************
253  *          RunHTMLApplication (MSHTML.@)
254  *
255  * Appears to have the same prototype as WinMain.
256  */
257 INT WINAPI RunHTMLApplication( HINSTANCE hinst, HINSTANCE hPrevInst,
258                                LPCSTR szCmdLine, INT nCmdShow )
259 {
260     FIXME("%p %p %s %d\n", hinst, hPrevInst, debugstr_a(szCmdLine), nCmdShow );
261     return 0;
262 }
263
264 /***********************************************************************
265  *          DllInstall (MSHTML.@)
266  */
267 HRESULT WINAPI MSHTML_DllInstall(BOOL bInstall, LPCWSTR cmdline)
268 {
269     FIXME("stub %d %s: returning S_OK\n", bInstall, debugstr_w(cmdline));
270     return S_OK;
271 }
272
273 /***********************************************************************
274  *          DllRegisterServer (MSHTML.@)
275  */
276 HRESULT WINAPI MSHTML_DllRegisterServer(void)
277 {
278     FIXME("stub: returning S_OK\n");
279     return S_OK;
280 }