* Sync up to trunk head (r65353).
[reactos.git] / dll / win32 / wshom.ocx / wshom_main.c
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "wshom_private.h"
20
21 #include <rpcproxy.h>
22
23 static HINSTANCE wshom_instance;
24
25 static ITypeLib *typelib;
26 static ITypeInfo *typeinfos[LAST_tid];
27
28 static REFIID tid_ids[] = {
29 &IID_NULL,
30 &IID_IWshCollection,
31 &IID_IWshEnvironment,
32 &IID_IWshShell3,
33 &IID_IWshShortcut
34 };
35
36 static HRESULT load_typelib(void)
37 {
38 HRESULT hres;
39 ITypeLib *tl;
40
41 hres = LoadRegTypeLib(&LIBID_IWshRuntimeLibrary, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
42 if(FAILED(hres)) {
43 ERR("LoadRegTypeLib failed: %08x\n", hres);
44 return hres;
45 }
46
47 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
48 ITypeLib_Release(tl);
49 return hres;
50 }
51
52 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
53 {
54 HRESULT hres;
55
56 if (!typelib)
57 hres = load_typelib();
58 if (!typelib)
59 return hres;
60
61 if(!typeinfos[tid]) {
62 ITypeInfo *ti;
63
64 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
65 if(FAILED(hres)) {
66 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
67 return hres;
68 }
69
70 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
71 ITypeInfo_Release(ti);
72 }
73
74 *typeinfo = typeinfos[tid];
75 ITypeInfo_AddRef(*typeinfo);
76 return S_OK;
77 }
78
79 static
80 void release_typelib(void)
81 {
82 unsigned i;
83
84 if(!typelib)
85 return;
86
87 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
88 if(typeinfos[i])
89 ITypeInfo_Release(typeinfos[i]);
90
91 ITypeLib_Release(typelib);
92 }
93
94 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
95 {
96 *ppv = NULL;
97
98 if(IsEqualGUID(&IID_IUnknown, riid)) {
99 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
100 *ppv = iface;
101 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
102 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
103 *ppv = iface;
104 }
105
106 if(*ppv) {
107 IUnknown_AddRef((IUnknown*)*ppv);
108 return S_OK;
109 }
110
111 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
112 return E_NOINTERFACE;
113 }
114
115 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
116 {
117 TRACE("(%p)\n", iface);
118 return 2;
119 }
120
121 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
122 {
123 TRACE("(%p)\n", iface);
124 return 1;
125 }
126
127 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
128 {
129 TRACE("(%p)->(%x)\n", iface, fLock);
130 return S_OK;
131 }
132
133 static const IClassFactoryVtbl WshShellFactoryVtbl = {
134 ClassFactory_QueryInterface,
135 ClassFactory_AddRef,
136 ClassFactory_Release,
137 WshShellFactory_CreateInstance,
138 ClassFactory_LockServer
139 };
140
141 static IClassFactory WshShellFactory = { &WshShellFactoryVtbl };
142
143 /******************************************************************
144 * DllMain (wshom.ocx.@)
145 */
146 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
147 {
148 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
149
150 switch(fdwReason)
151 {
152 case DLL_WINE_PREATTACH:
153 return FALSE; /* prefer native version */
154 case DLL_PROCESS_ATTACH:
155 wshom_instance = hInstDLL;
156 DisableThreadLibraryCalls(wshom_instance);
157 break;
158 case DLL_PROCESS_DETACH:
159 if (lpv) break;
160 release_typelib();
161 break;
162 }
163
164 return TRUE;
165 }
166
167 /***********************************************************************
168 * DllGetClassObject (wshom.ocx.@)
169 */
170 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
171 {
172 if(IsEqualGUID(&CLSID_WshShell, rclsid)) {
173 TRACE("(CLSID_WshShell %s %p)\n", debugstr_guid(riid), ppv);
174 return IClassFactory_QueryInterface(&WshShellFactory, riid, ppv);
175 }
176
177 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
178 return CLASS_E_CLASSNOTAVAILABLE;
179 }
180
181 /***********************************************************************
182 * DllCanUnloadNow (wshom.ocx.@)
183 */
184 HRESULT WINAPI DllCanUnloadNow(void)
185 {
186 return S_FALSE;
187 }
188
189 /***********************************************************************
190 * DllRegisterServer (wshom.ocx.@)
191 */
192 HRESULT WINAPI DllRegisterServer(void)
193 {
194 TRACE("()\n");
195 return __wine_register_resources(wshom_instance);
196 }
197
198 /***********************************************************************
199 * DllUnregisterServer (wshom.ocx.@)
200 */
201 HRESULT WINAPI DllUnregisterServer(void)
202 {
203 TRACE("()\n");
204 return __wine_unregister_resources(wshom_instance);
205 }