Fix a copy&paste bug in get_buffer_pe.
[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
37 #include "mshtml.h"
38 #include "mshtml_private.h"
39
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
44
45 #include "initguid.h"
46
47 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
48
49 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
50 typedef BOOL (WINAPI *fnCanUnloadNow)();
51
52 static HMODULE hMozCtl;
53
54
55 /* convert a guid to a wide character string */
56 static void MSHTML_guid2wstr( const GUID *guid, LPWSTR wstr )
57 {
58     char str[40];
59
60     sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
61            guid->Data1, guid->Data2, guid->Data3,
62            guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
63            guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
64     MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
65 }
66
67 static BOOL MSHTML_GetMozctlPath( LPWSTR szPath, DWORD sz )
68 {
69     DWORD r, type;
70     BOOL ret = FALSE;
71     HKEY hkey;
72     static const WCHAR szPre[] = {
73         'S','o','f','t','w','a','r','e','\\',
74         'C','l','a','s','s','e','s','\\',
75         'C','L','S','I','D','\\',0 };
76     static const WCHAR szPost[] = {
77         '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
78     WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
79
80     strcpyW( szRegPath, szPre );
81     MSHTML_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
82     strcatW( szRegPath, szPost );
83
84     TRACE("key = %s\n", debugstr_w( szRegPath ) );
85
86     r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
87     if( r != ERROR_SUCCESS )
88         return FALSE;
89
90     r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
91     ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
92     RegCloseKey( hkey );
93
94     return ret;
95 }
96
97 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
98 {
99     WCHAR szPath[MAX_PATH];
100
101     switch(fdwReason) {
102         case DLL_PROCESS_ATTACH:
103             if(MSHTML_GetMozctlPath(szPath, sizeof szPath)) {
104                 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
105                 if(!hMozCtl)
106                     ERR("Can't load the Mozilla ActiveX control\n");
107             }else {
108                 MESSAGE("You need to install the Mozilla ActiveX control to\n");
109                 MESSAGE("use Wine's builtin MSHTML dll.\n");
110             }
111             break;
112         case DLL_PROCESS_DETACH:
113             if(hMozCtl)
114                 FreeLibrary( hMozCtl );
115             break;
116     }
117     return TRUE;
118 }
119
120 /***********************************************************
121  *    ClassFactory implementation
122  */
123 typedef HRESULT (*CreateInstanceFunc)(IUnknown*,REFIID,void**);
124 typedef struct {
125     const IClassFactoryVtbl *lpVtbl;
126     ULONG ref;
127     CreateInstanceFunc fnCreateInstance;
128 } ClassFactory;
129
130 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFGUID riid, void **ppvObject)
131 {
132     if(IsEqualGUID(&IID_IClassFactory, riid) || IsEqualGUID(&IID_IUnknown, riid)) {
133         IClassFactory_AddRef(iface);
134         *ppvObject = iface;
135         return S_OK;
136     }
137
138     WARN("not supported iid %s\n", debugstr_guid(riid));
139     *ppvObject = NULL;
140     return E_NOINTERFACE;
141 }
142
143 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
144 {
145     ClassFactory *This = (ClassFactory*)iface;
146     ULONG ref = InterlockedIncrement(&This->ref);
147     TRACE("(%p) ref = %lu\n", This, ref);
148     return ref;
149 }
150
151 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
152 {
153     ClassFactory *This = (ClassFactory*)iface;
154     ULONG ref = InterlockedDecrement(&This->ref);
155
156     TRACE("(%p) ref = %lu\n", This, ref);
157
158     if(!ref)
159         HeapFree(GetProcessHeap(), 0, This);
160
161     return ref;
162 }
163
164 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
165         REFIID riid, void **ppvObject)
166 {
167     ClassFactory *This = (ClassFactory*)iface;
168     return This->fnCreateInstance(pUnkOuter, riid, ppvObject);
169 }
170
171 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
172 {
173     FIXME("(%p)->(%x) stub\n", iface, dolock);
174     return S_OK;
175 }
176
177 static const IClassFactoryVtbl HTMLClassFactoryVtbl = {
178     ClassFactory_QueryInterface,
179     ClassFactory_AddRef,
180     ClassFactory_Release,
181     ClassFactory_CreateInstance,
182     ClassFactory_LockServer
183 };
184
185 static HRESULT ClassFactory_Create(REFIID riid, void **ppv, CreateInstanceFunc fnCreateInstance)
186 {
187     ClassFactory *ret = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactory));
188     HRESULT hres;
189
190     ret->lpVtbl = &HTMLClassFactoryVtbl;
191     ret->ref = 0;
192     ret->fnCreateInstance = fnCreateInstance;
193
194     hres = IClassFactory_QueryInterface((IClassFactory*)ret, riid, ppv);
195     if(FAILED(hres)) {
196         HeapFree(GetProcessHeap(), 0, ret);
197         *ppv = NULL;
198     }
199     return hres;
200 }
201
202 HRESULT WINAPI MSHTML_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
203 {
204     HRESULT hres;
205     fnGetClassObject pGetClassObject;
206
207     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv );
208
209     if(hMozCtl && IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
210         pGetClassObject = (fnGetClassObject) GetProcAddress(hMozCtl, "DllGetClassObject");
211         if(pGetClassObject) {
212             hres = pGetClassObject(&CLSID_MozillaBrowser, riid, ppv);
213             if(SUCCEEDED(hres)) {
214                 TRACE("returning Mozilla ActiveX Control hres = %08lx  *ppv = %p\n", hres, *ppv);
215                 return hres;
216             }
217         }
218     }
219
220     if(IsEqualGUID(&CLSID_HTMLDocument, rclsid)) {
221         hres = ClassFactory_Create(riid, ppv, HTMLDocument_Create);
222         TRACE("hres = %08lx\n", hres);
223         return hres;
224     }
225
226     FIXME("Unknown class %s\n", debugstr_guid(rclsid));
227     return CLASS_E_CLASSNOTAVAILABLE;
228 }
229
230 HRESULT WINAPI MSHTML_DllCanUnloadNow(void)
231 {
232     fnCanUnloadNow pCanUnloadNow = NULL;
233     HRESULT hres;
234
235     TRACE("()\n");
236
237     if(hMozCtl)
238         pCanUnloadNow = (fnCanUnloadNow) GetProcAddress(hMozCtl, "DllCanUnloadNow");
239     if(!pCanUnloadNow)
240         return S_FALSE;
241
242     hres = pCanUnloadNow();
243
244     TRACE("hres = %08lx\n", hres);
245
246     return hres;
247 }
248
249 /* appears to have the same prototype as WinMain */
250 INT WINAPI RunHTMLApplication( HINSTANCE hinst, HINSTANCE hPrevInst,
251                                LPCSTR szCmdLine, INT nCmdShow )
252 {
253     FIXME("%p %p %s %d\n", hinst, hPrevInst, debugstr_a(szCmdLine), nCmdShow );
254     return 0;
255 }
256
257 /***********************************************************************
258  *          DllInstall (MSHTML.@)
259  */
260 HRESULT WINAPI MSHTML_DllInstall(BOOL bInstall, LPCWSTR cmdline)
261 {
262     FIXME("stub %d %s: returning S_OK\n", bInstall, debugstr_w(cmdline));
263     return S_OK;
264 }
265
266 /***********************************************************************
267  *          DllRegisterServer (MSHTML.@)
268  */
269 HRESULT WINAPI MSHTML_DllRegisterServer(void)
270 {
271     FIXME("stub: returning S_OK\n");
272     return S_OK;
273 }