wbemdisp: Added WinMGMTS object stub implementation.
[wine/wine-gecko.git] / dlls / vbscript / vbscript_main.c
bloba79a1a06b54f5092d30d209a13359386f248a59c
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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
19 #include "initguid.h"
21 #include "vbscript.h"
22 #include "objsafe.h"
23 #include "mshtmhst.h"
24 #include "rpcproxy.h"
25 #include "vbscript_classes.h"
26 #include "vbsglobal.h"
27 #include "vbsregexp55.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 WINE_DECLARE_DEBUG_CHANNEL(heap);
34 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
36 static HINSTANCE vbscript_hinstance;
38 static ITypeLib *typelib;
39 static ITypeInfo *typeinfos[LAST_tid];
41 static REFIID tid_ids[] = {
42 #define XDIID(iface) &DIID_ ## iface,
43 TID_LIST
44 #undef XDIID
47 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
49 HRESULT hres;
51 if (!typelib) {
52 ITypeLib *tl;
54 static const WCHAR vbscript_dll1W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
56 hres = LoadTypeLib(vbscript_dll1W, &tl);
57 if(FAILED(hres)) {
58 ERR("LoadRegTypeLib failed: %08x\n", hres);
59 return hres;
62 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
63 ITypeLib_Release(tl);
66 if(!typeinfos[tid]) {
67 ITypeInfo *ti;
69 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
70 if(FAILED(hres)) {
71 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
72 return hres;
75 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
76 ITypeInfo_Release(ti);
79 *typeinfo = typeinfos[tid];
80 return S_OK;
83 static void release_typelib(void)
85 unsigned i;
87 if(!typelib)
88 return;
90 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++) {
91 if(typeinfos[i])
92 ITypeInfo_Release(typeinfos[i]);
95 ITypeLib_Release(typelib);
98 const char *debugstr_variant(const VARIANT *v)
100 if(!v)
101 return "(null)";
103 if(V_ISBYREF(v))
104 return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
106 switch(V_VT(v)) {
107 case VT_EMPTY:
108 return "{VT_EMPTY}";
109 case VT_NULL:
110 return "{VT_NULL}";
111 case VT_I2:
112 return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
113 case VT_I4:
114 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
115 case VT_UI4:
116 return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
117 case VT_R8:
118 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
119 case VT_BSTR:
120 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
121 case VT_DISPATCH:
122 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
123 case VT_BOOL:
124 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
125 default:
126 return wine_dbg_sprintf("{vt %d}", V_VT(v));
130 #define MIN_BLOCK_SIZE 128
131 #define ARENA_FREE_FILLER 0xaa
133 static inline DWORD block_size(DWORD block)
135 return MIN_BLOCK_SIZE << block;
138 void heap_pool_init(heap_pool_t *heap)
140 memset(heap, 0, sizeof(*heap));
141 list_init(&heap->custom_blocks);
144 void *heap_pool_alloc(heap_pool_t *heap, size_t size)
146 struct list *list;
147 void *tmp;
149 size = (size+3)&~3;
151 if(!heap->block_cnt) {
152 if(!heap->blocks) {
153 heap->blocks = heap_alloc(sizeof(void*));
154 if(!heap->blocks)
155 return NULL;
158 tmp = heap_alloc(block_size(0));
159 if(!tmp)
160 return NULL;
162 heap->blocks[0] = tmp;
163 heap->block_cnt = 1;
166 if(heap->offset + size <= block_size(heap->last_block)) {
167 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
168 heap->offset += size;
169 return tmp;
172 if(size <= block_size(heap->last_block+1)) {
173 if(heap->last_block+1 == heap->block_cnt) {
174 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
175 if(!tmp)
176 return NULL;
178 heap->blocks = tmp;
179 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
180 if(!heap->blocks[heap->block_cnt])
181 return NULL;
183 heap->block_cnt++;
186 heap->last_block++;
187 heap->offset = size;
188 return heap->blocks[heap->last_block];
191 list = heap_alloc(size + sizeof(struct list));
192 if(!list)
193 return NULL;
195 list_add_head(&heap->custom_blocks, list);
196 return list+1;
199 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
201 void *ret;
203 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
204 && heap->offset+inc < block_size(heap->last_block)) {
205 heap->offset += inc;
206 return mem;
209 ret = heap_pool_alloc(heap, size+inc);
210 if(ret) /* FIXME: avoid copying for custom blocks */
211 memcpy(ret, mem, size);
212 return ret;
215 void heap_pool_clear(heap_pool_t *heap)
217 struct list *tmp;
219 if(!heap)
220 return;
222 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
223 list_remove(tmp);
224 heap_free(tmp);
227 if(WARN_ON(heap)) {
228 DWORD i;
230 for(i=0; i < heap->block_cnt; i++)
231 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
234 heap->last_block = heap->offset = 0;
235 heap->mark = FALSE;
238 void heap_pool_free(heap_pool_t *heap)
240 DWORD i;
242 heap_pool_clear(heap);
244 for(i=0; i < heap->block_cnt; i++)
245 heap_free(heap->blocks[i]);
246 heap_free(heap->blocks);
248 heap_pool_init(heap);
251 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
253 if(heap->mark)
254 return NULL;
256 heap->mark = TRUE;
257 return heap;
260 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
262 *ppv = NULL;
264 if(IsEqualGUID(&IID_IUnknown, riid)) {
265 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
266 *ppv = iface;
267 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
268 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
269 *ppv = iface;
272 if(*ppv) {
273 IUnknown_AddRef((IUnknown*)*ppv);
274 return S_OK;
277 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
278 return E_NOINTERFACE;
281 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
283 TRACE("(%p)\n", iface);
284 return 2;
287 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
289 TRACE("(%p)\n", iface);
290 return 1;
293 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
295 TRACE("(%p)->(%x)\n", iface, fLock);
296 return S_OK;
299 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
300 ClassFactory_QueryInterface,
301 ClassFactory_AddRef,
302 ClassFactory_Release,
303 VBScriptFactory_CreateInstance,
304 ClassFactory_LockServer
307 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
309 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl = {
310 ClassFactory_QueryInterface,
311 ClassFactory_AddRef,
312 ClassFactory_Release,
313 VBScriptRegExpFactory_CreateInstance,
314 ClassFactory_LockServer
317 static IClassFactory VBScriptRegExpFactory = { &VBScriptRegExpFactoryVtbl };
319 /******************************************************************
320 * DllMain (vbscript.@)
322 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
324 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
326 switch(fdwReason)
328 case DLL_WINE_PREATTACH:
329 return FALSE; /* prefer native version */
330 case DLL_PROCESS_ATTACH:
331 DisableThreadLibraryCalls(hInstDLL);
332 vbscript_hinstance = hInstDLL;
333 break;
334 case DLL_PROCESS_DETACH:
335 if (lpv) break;
336 release_typelib();
337 release_regexp_typelib();
340 return TRUE;
343 /***********************************************************************
344 * DllGetClassObject (vbscript.@)
346 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
348 if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
349 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
350 return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
351 }else if(IsEqualGUID(&CLSID_VBScriptRegExp, rclsid)) {
352 TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid), ppv);
353 return IClassFactory_QueryInterface(&VBScriptRegExpFactory, riid, ppv);
356 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
357 return CLASS_E_CLASSNOTAVAILABLE;
360 /***********************************************************************
361 * DllCanUnloadNow (vbscript.@)
363 HRESULT WINAPI DllCanUnloadNow(void)
365 return S_FALSE;
368 /***********************************************************************
369 * DllRegisterServer (vbscript.@)
371 HRESULT WINAPI DllRegisterServer(void)
373 TRACE("()\n");
374 return __wine_register_resources(vbscript_hinstance);
377 /***********************************************************************
378 * DllUnregisterServer (vbscript.@)
380 HRESULT WINAPI DllUnregisterServer(void)
382 TRACE("()\n");
383 return __wine_unregister_resources(vbscript_hinstance);