storage.dll16: Fix get_nth_next_small_blocknr.
[wine.git] / dlls / scrobj / scrobj.c
blob38f1c651f40ede503507c9113fc70627f79a23f7
1 /*
2 * Copyright 2017 Nikolay Sivov
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.
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.
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
18 #define COBJMACROS
20 #include "config.h"
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ole2.h"
26 #include "olectl.h"
27 #include "rpcproxy.h"
29 #include "initguid.h"
30 #include "scrobj.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(scrobj);
35 static HINSTANCE scrobj_instance;
37 typedef enum tid_t
39 NULL_tid,
40 IGenScriptletTLib_tid,
41 LAST_tid
42 } tid_t;
44 static ITypeLib *typelib;
45 static ITypeInfo *typeinfos[LAST_tid];
47 static REFIID tid_ids[] = {
48 &IID_NULL,
49 &IID_IGenScriptletTLib,
52 static HRESULT load_typelib(void)
54 HRESULT hres;
55 ITypeLib *tl;
57 if (typelib)
58 return S_OK;
60 hres = LoadRegTypeLib(&LIBID_Scriptlet, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
61 if (FAILED(hres))
63 ERR("LoadRegTypeLib failed: %08x\n", hres);
64 return hres;
67 if (InterlockedCompareExchangePointer((void **)&typelib, tl, NULL))
68 ITypeLib_Release(tl);
69 return hres;
72 static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
74 HRESULT hres;
76 if (FAILED(hres = load_typelib()))
77 return hres;
79 if (!typeinfos[tid])
81 ITypeInfo *ti;
83 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
84 if (FAILED(hres)) {
85 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
86 return hres;
89 if (InterlockedCompareExchangePointer((void **)(typeinfos+tid), ti, NULL))
90 ITypeInfo_Release(ti);
93 *typeinfo = typeinfos[tid];
94 ITypeInfo_AddRef(typeinfos[tid]);
95 return S_OK;
98 static void release_typelib(void)
100 unsigned i;
102 if (!typelib)
103 return;
105 for (i = 0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++)
106 if (typeinfos[i])
107 ITypeInfo_Release(typeinfos[i]);
109 ITypeLib_Release(typelib);
112 struct scriptlet_typelib
114 IGenScriptletTLib IGenScriptletTLib_iface;
115 LONG ref;
117 BSTR guid;
120 static inline struct scriptlet_typelib *impl_from_IGenScriptletTLib(IGenScriptletTLib *iface)
122 return CONTAINING_RECORD(iface, struct scriptlet_typelib, IGenScriptletTLib_iface);
125 static HRESULT WINAPI scriptlet_typelib_QueryInterface(IGenScriptletTLib *iface, REFIID riid, void **obj)
127 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
129 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), obj);
131 if (IsEqualIID(riid, &IID_IGenScriptletTLib) ||
132 IsEqualIID(riid, &IID_IDispatch) ||
133 IsEqualIID(riid, &IID_IUnknown))
135 *obj = iface;
136 IGenScriptletTLib_AddRef(iface);
137 return S_OK;
140 *obj = NULL;
141 return E_NOINTERFACE;
144 static ULONG WINAPI scriptlet_typelib_AddRef(IGenScriptletTLib *iface)
146 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
147 ULONG ref = InterlockedIncrement(&This->ref);
148 TRACE("(%p)->(%u)\n", This, ref);
149 return ref;
152 static ULONG WINAPI scriptlet_typelib_Release(IGenScriptletTLib *iface)
154 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
155 LONG ref = InterlockedDecrement(&This->ref);
157 TRACE("(%p)->(%u)\n", This, ref);
159 if (!ref)
161 SysFreeString(This->guid);
162 HeapFree(GetProcessHeap(), 0, This);
165 return ref;
168 static HRESULT WINAPI scriptlet_typelib_GetTypeInfoCount(IGenScriptletTLib *iface, UINT *count)
170 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
172 TRACE("(%p, %p)\n", This, count);
174 *count = 1;
175 return S_OK;
178 static HRESULT WINAPI scriptlet_typelib_GetTypeInfo(IGenScriptletTLib *iface, UINT index, LCID lcid,
179 ITypeInfo **tinfo)
181 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
183 TRACE("(%p, %u, %x, %p)\n", This, index, lcid, tinfo);
185 return get_typeinfo(IGenScriptletTLib_tid, tinfo);
188 static HRESULT WINAPI scriptlet_typelib_GetIDsOfNames(IGenScriptletTLib *iface, REFIID riid, LPOLESTR *names,
189 UINT cNames, LCID lcid, DISPID *dispid)
191 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
192 ITypeInfo *typeinfo;
193 HRESULT hr;
195 TRACE("(%p, %s, %p, %u, %x, %p)\n", This, debugstr_guid(riid), names, cNames, lcid, dispid);
197 hr = get_typeinfo(IGenScriptletTLib_tid, &typeinfo);
198 if (SUCCEEDED(hr))
200 hr = ITypeInfo_GetIDsOfNames(typeinfo, names, cNames, dispid);
201 ITypeInfo_Release(typeinfo);
204 return hr;
207 static HRESULT WINAPI scriptlet_typelib_Invoke(IGenScriptletTLib *iface, DISPID dispid, REFIID riid,
208 LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *ei, UINT *argerr)
210 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
211 ITypeInfo *typeinfo;
212 HRESULT hr;
214 TRACE("(%p, %d, %s, %x, %x, %p, %p, %p, %p)\n", This, dispid, debugstr_guid(riid), lcid, flags,
215 params, result, ei, argerr);
217 hr = get_typeinfo(IGenScriptletTLib_tid, &typeinfo);
218 if (SUCCEEDED(hr))
220 hr = ITypeInfo_Invoke(typeinfo, &This->IGenScriptletTLib_iface, dispid, flags,
221 params, result, ei, argerr);
222 ITypeInfo_Release(typeinfo);
225 return hr;
228 static HRESULT WINAPI scriptlet_typelib_AddURL(IGenScriptletTLib *iface, BSTR url)
230 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
232 FIXME("(%p, %s): stub\n", This, debugstr_w(url));
234 return E_NOTIMPL;
237 static HRESULT WINAPI scriptlet_typelib_put_Path(IGenScriptletTLib *iface, BSTR path)
239 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
241 FIXME("(%p, %s): stub\n", This, debugstr_w(path));
243 return E_NOTIMPL;
246 static HRESULT WINAPI scriptlet_typelib_get_Path(IGenScriptletTLib *iface, BSTR *path)
248 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
250 FIXME("(%p, %p): stub\n", This, path);
252 return E_NOTIMPL;
255 static HRESULT WINAPI scriptlet_typelib_put_Doc(IGenScriptletTLib *iface, BSTR doc)
257 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
259 FIXME("(%p, %s): stub\n", This, debugstr_w(doc));
261 return E_NOTIMPL;
264 static HRESULT WINAPI scriptlet_typelib_get_Doc(IGenScriptletTLib *iface, BSTR *doc)
266 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
268 FIXME("(%p, %p): stub\n", This, doc);
270 return E_NOTIMPL;
273 static HRESULT WINAPI scriptlet_typelib_put_Name(IGenScriptletTLib *iface, BSTR name)
275 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
277 FIXME("(%p, %s): stub\n", This, debugstr_w(name));
279 return E_NOTIMPL;
282 static HRESULT WINAPI scriptlet_typelib_get_Name(IGenScriptletTLib *iface, BSTR *name)
284 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
286 FIXME("(%p, %p): stub\n", This, name);
288 return E_NOTIMPL;
291 static HRESULT WINAPI scriptlet_typelib_put_MajorVersion(IGenScriptletTLib *iface, WORD version)
293 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
295 FIXME("(%p, %x): stub\n", This, version);
297 return E_NOTIMPL;
300 static HRESULT WINAPI scriptlet_typelib_get_MajorVersion(IGenScriptletTLib *iface, WORD *version)
302 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
304 FIXME("(%p, %p): stub\n", This, version);
306 return E_NOTIMPL;
309 static HRESULT WINAPI scriptlet_typelib_put_MinorVersion(IGenScriptletTLib *iface, WORD version)
311 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
313 FIXME("(%p, %x): stub\n", This, version);
315 return E_NOTIMPL;
318 static HRESULT WINAPI scriptlet_typelib_get_MinorVersion(IGenScriptletTLib *iface, WORD *version)
320 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
322 FIXME("(%p, %p): stub\n", This, version);
324 return E_NOTIMPL;
327 static HRESULT WINAPI scriptlet_typelib_Write(IGenScriptletTLib *iface)
329 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
331 FIXME("(%p): stub\n", This);
333 return E_NOTIMPL;
336 static HRESULT WINAPI scriptlet_typelib_Reset(IGenScriptletTLib *iface)
338 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
340 FIXME("(%p): stub\n", This);
342 return E_NOTIMPL;
345 static HRESULT WINAPI scriptlet_typelib_put_GUID(IGenScriptletTLib *iface, BSTR guid)
347 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
349 FIXME("(%p, %s): stub\n", This, debugstr_w(guid));
351 return E_NOTIMPL;
354 static HRESULT WINAPI scriptlet_typelib_get_GUID(IGenScriptletTLib *iface, BSTR *ret)
356 struct scriptlet_typelib *This = impl_from_IGenScriptletTLib(iface);
358 TRACE("(%p, %p)\n", This, ret);
360 *ret = NULL;
362 if (!This->guid)
364 WCHAR guidW[39];
365 GUID guid;
366 HRESULT hr;
368 hr = CoCreateGuid(&guid);
369 if (FAILED(hr))
370 return hr;
372 hr = StringFromGUID2(&guid, guidW, sizeof(guidW)/sizeof(guidW[0]));
373 if (FAILED(hr))
374 return hr;
376 if (!(This->guid = SysAllocString(guidW)))
377 return E_OUTOFMEMORY;
380 *ret = SysAllocString(This->guid);
381 return *ret ? S_OK : E_OUTOFMEMORY;
384 static const IGenScriptletTLibVtbl scriptlet_typelib_vtbl =
386 scriptlet_typelib_QueryInterface,
387 scriptlet_typelib_AddRef,
388 scriptlet_typelib_Release,
389 scriptlet_typelib_GetTypeInfoCount,
390 scriptlet_typelib_GetTypeInfo,
391 scriptlet_typelib_GetIDsOfNames,
392 scriptlet_typelib_Invoke,
393 scriptlet_typelib_AddURL,
394 scriptlet_typelib_put_Path,
395 scriptlet_typelib_get_Path,
396 scriptlet_typelib_put_Doc,
397 scriptlet_typelib_get_Doc,
398 scriptlet_typelib_put_Name,
399 scriptlet_typelib_get_Name,
400 scriptlet_typelib_put_MajorVersion,
401 scriptlet_typelib_get_MajorVersion,
402 scriptlet_typelib_put_MinorVersion,
403 scriptlet_typelib_get_MinorVersion,
404 scriptlet_typelib_Write,
405 scriptlet_typelib_Reset,
406 scriptlet_typelib_put_GUID,
407 scriptlet_typelib_get_GUID
410 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
412 TRACE("%p, %u, %p\n", hinst, reason, reserved);
414 switch (reason)
416 case DLL_WINE_PREATTACH:
417 return FALSE; /* prefer native version */
418 case DLL_PROCESS_ATTACH:
419 DisableThreadLibraryCalls(hinst);
420 scrobj_instance = hinst;
421 break;
422 case DLL_PROCESS_DETACH:
423 if (reserved) break;
424 release_typelib();
425 break;
427 return TRUE;
430 /***********************************************************************
431 * DllRegisterServer (scrobj.@)
433 HRESULT WINAPI DllRegisterServer(void)
435 TRACE("()\n");
436 return __wine_register_resources(scrobj_instance);
439 /***********************************************************************
440 * DllUnregisterServer (scrobj.@)
442 HRESULT WINAPI DllUnregisterServer(void)
444 TRACE("()\n");
445 return __wine_unregister_resources(scrobj_instance);
448 HRESULT WINAPI scriptlet_typelib_CreateInstance(IClassFactory *factory, IUnknown *outer, REFIID riid, void **obj)
450 struct scriptlet_typelib *This;
451 HRESULT hr;
453 TRACE("(%p, %p, %s, %p)\n", factory, outer, debugstr_guid(riid), obj);
455 *obj = NULL;
457 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
458 if (!This)
459 return E_OUTOFMEMORY;
461 This->IGenScriptletTLib_iface.lpVtbl = &scriptlet_typelib_vtbl;
462 This->ref = 1;
463 This->guid = NULL;
465 hr = IGenScriptletTLib_QueryInterface(&This->IGenScriptletTLib_iface, riid, obj);
466 IGenScriptletTLib_Release(&This->IGenScriptletTLib_iface);
467 return hr;
470 static HRESULT WINAPI scrruncf_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
472 *ppv = NULL;
474 if (IsEqualGUID(&IID_IUnknown, riid))
476 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
477 *ppv = iface;
479 else if (IsEqualGUID(&IID_IClassFactory, riid))
481 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
482 *ppv = iface;
485 if (*ppv)
487 IUnknown_AddRef((IUnknown *)*ppv);
488 return S_OK;
491 WARN("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
492 return E_NOINTERFACE;
495 static ULONG WINAPI scrruncf_AddRef(IClassFactory *iface)
497 TRACE("(%p)\n", iface);
498 return 2;
501 static ULONG WINAPI scrruncf_Release(IClassFactory *iface)
503 TRACE("(%p)\n", iface);
504 return 1;
507 static HRESULT WINAPI scrruncf_LockServer(IClassFactory *iface, BOOL fLock)
509 TRACE("(%p)->(%x)\n", iface, fLock);
510 return S_OK;
513 static const struct IClassFactoryVtbl scriptlet_typelib_factory_vtbl =
515 scrruncf_QueryInterface,
516 scrruncf_AddRef,
517 scrruncf_Release,
518 scriptlet_typelib_CreateInstance,
519 scrruncf_LockServer
522 static IClassFactory scriptlet_typelib_factory = { &scriptlet_typelib_factory_vtbl };
524 /***********************************************************************
525 * DllGetClassObject (scrobj.@)
527 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
529 if (IsEqualGUID(&CLSID_TypeLib, rclsid))
531 TRACE("(Scriptlet.TypeLib %s %p)\n", debugstr_guid(riid), ppv);
532 return IClassFactory_QueryInterface(&scriptlet_typelib_factory, riid, ppv);
535 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
536 return CLASS_E_CLASSNOTAVAILABLE;
539 /***********************************************************************
540 * DllCanUnloadNow (scrobj.@)
542 HRESULT WINAPI DllCanUnloadNow(void)
544 return S_FALSE;