Sync with trunk r63270.
[reactos.git] / dll / win32 / scrrun / scrrun.c
1 /*
2 * Copyright 2011 Hans Leidekker 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 "scrrun_private.h"
20
21 #include <rpcproxy.h>
22
23 static HINSTANCE scrrun_instance;
24
25 typedef HRESULT (*fnCreateInstance)(LPVOID *ppObj);
26
27 static HRESULT WINAPI scrruncf_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppv )
28 {
29 *ppv = NULL;
30
31 if(IsEqualGUID(&IID_IUnknown, riid)) {
32 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
33 *ppv = iface;
34 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
35 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
36 *ppv = iface;
37 }
38
39 if(*ppv) {
40 IUnknown_AddRef((IUnknown*)*ppv);
41 return S_OK;
42 }
43
44 WARN("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI scrruncf_AddRef(IClassFactory *iface )
49 {
50 TRACE("(%p)\n", iface);
51 return 2;
52 }
53
54 static ULONG WINAPI scrruncf_Release(IClassFactory *iface )
55 {
56 TRACE("(%p)\n", iface);
57 return 1;
58 }
59
60 static HRESULT WINAPI scrruncf_LockServer(IClassFactory *iface, BOOL fLock)
61 {
62 TRACE("(%p)->(%x)\n", iface, fLock);
63 return S_OK;
64 }
65
66 static const struct IClassFactoryVtbl scrruncf_vtbl =
67 {
68 scrruncf_QueryInterface,
69 scrruncf_AddRef,
70 scrruncf_Release,
71 FileSystem_CreateInstance,
72 scrruncf_LockServer
73 };
74
75 static const struct IClassFactoryVtbl dictcf_vtbl =
76 {
77 scrruncf_QueryInterface,
78 scrruncf_AddRef,
79 scrruncf_Release,
80 Dictionary_CreateInstance,
81 scrruncf_LockServer
82 };
83
84 static IClassFactory FileSystemFactory = { &scrruncf_vtbl };
85 static IClassFactory DictionaryFactory = { &dictcf_vtbl };
86
87 static ITypeLib *typelib;
88 static ITypeInfo *typeinfos[LAST_tid];
89
90 static REFIID tid_ids[] = {
91 &IID_NULL,
92 &IID_IDictionary,
93 &IID_IDrive,
94 &IID_IDriveCollection,
95 &IID_IFile,
96 &IID_IFileCollection,
97 &IID_IFileSystem3,
98 &IID_IFolder,
99 &IID_IFolderCollection,
100 &IID_ITextStream
101 };
102
103 static HRESULT load_typelib(void)
104 {
105 HRESULT hres;
106 ITypeLib *tl;
107
108 hres = LoadRegTypeLib(&LIBID_Scripting, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
109 if(FAILED(hres)) {
110 ERR("LoadRegTypeLib failed: %08x\n", hres);
111 return hres;
112 }
113
114 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
115 ITypeLib_Release(tl);
116 return hres;
117 }
118
119 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
120 {
121 HRESULT hres;
122
123 if (!typelib)
124 hres = load_typelib();
125 if (!typelib)
126 return hres;
127
128 if(!typeinfos[tid]) {
129 ITypeInfo *ti;
130
131 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
132 if(FAILED(hres)) {
133 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
134 return hres;
135 }
136
137 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
138 ITypeInfo_Release(ti);
139 }
140
141 *typeinfo = typeinfos[tid];
142 return S_OK;
143 }
144
145 static void release_typelib(void)
146 {
147 unsigned i;
148
149 if(!typelib)
150 return;
151
152 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
153 if(typeinfos[i])
154 ITypeInfo_Release(typeinfos[i]);
155
156 ITypeLib_Release(typelib);
157 }
158
159 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
160 {
161 TRACE("%p, %u, %p\n", hinst, reason, reserved);
162
163 switch (reason)
164 {
165 case DLL_WINE_PREATTACH:
166 return FALSE; /* prefer native version */
167 case DLL_PROCESS_ATTACH:
168 DisableThreadLibraryCalls( hinst );
169 scrrun_instance = hinst;
170 break;
171 case DLL_PROCESS_DETACH:
172 if (reserved) break;
173 release_typelib();
174 break;
175 }
176 return TRUE;
177 }
178
179 /***********************************************************************
180 * DllRegisterServer (scrrun.@)
181 */
182 HRESULT WINAPI DllRegisterServer(void)
183 {
184 TRACE("()\n");
185 return __wine_register_resources(scrrun_instance);
186 }
187
188 /***********************************************************************
189 * DllUnregisterServer (scrrun.@)
190 */
191 HRESULT WINAPI DllUnregisterServer(void)
192 {
193 TRACE("()\n");
194 return __wine_unregister_resources(scrrun_instance);
195 }
196
197 /***********************************************************************
198 * DllGetClassObject (scrrun.@)
199 */
200
201 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
202 {
203 if(IsEqualGUID(&CLSID_FileSystemObject, rclsid)) {
204 TRACE("(CLSID_WshShell %s %p)\n", debugstr_guid(riid), ppv);
205 return IClassFactory_QueryInterface(&FileSystemFactory, riid, ppv);
206 }
207 else if(IsEqualGUID(&CLSID_Dictionary, rclsid)) {
208 TRACE("(CLSID_WshShell %s %p)\n", debugstr_guid(riid), ppv);
209 return IClassFactory_QueryInterface(&DictionaryFactory, riid, ppv);
210 }
211
212 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
213 return CLASS_E_CLASSNOTAVAILABLE;
214 }
215
216 /***********************************************************************
217 * DllCanUnloadNow (scrrun.@)
218 */
219 HRESULT WINAPI DllCanUnloadNow(void)
220 {
221 return S_FALSE;
222 }