strmbase: Avoid a COM object to interface cast.
[wine.git] / dlls / vbscript / global.c
blob129d6ce7ddef65348e790a7f058398858bbeee11
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 <assert.h>
20 #include <math.h>
22 #include "vbscript.h"
23 #include "vbscript_defs.h"
25 #include "mshtmhst.h"
26 #include "objsafe.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 #define VB_E_CANNOT_CREATE_OBJ 0x800a01ad
33 #define VB_E_MK_PARSE_ERROR 0x800a01b0
35 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
36 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
37 {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
39 static const WCHAR emptyW[] = {0};
40 static const WCHAR vbscriptW[] = {'V','B','S','c','r','i','p','t',0};
42 static IInternetHostSecurityManager *get_sec_mgr(script_ctx_t *ctx)
44 IInternetHostSecurityManager *secmgr;
45 IServiceProvider *sp;
46 HRESULT hres;
48 if(!ctx->site)
49 return NULL;
51 if(ctx->secmgr)
52 return ctx->secmgr;
54 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
55 if(FAILED(hres))
56 return NULL;
58 hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager, &IID_IInternetHostSecurityManager,
59 (void**)&secmgr);
60 IServiceProvider_Release(sp);
61 if(FAILED(hres))
62 return NULL;
64 return ctx->secmgr = secmgr;
67 static HRESULT return_string(VARIANT *res, const WCHAR *str)
69 BSTR ret;
71 if(!res)
72 return S_OK;
74 ret = SysAllocString(str);
75 if(!ret)
76 return E_OUTOFMEMORY;
78 V_VT(res) = VT_BSTR;
79 V_BSTR(res) = ret;
80 return S_OK;
83 static HRESULT return_bstr(VARIANT *res, BSTR str)
85 if(res) {
86 V_VT(res) = VT_BSTR;
87 V_BSTR(res) = str;
88 }else {
89 SysFreeString(str);
91 return S_OK;
94 static HRESULT return_bool(VARIANT *res, BOOL val)
96 if(res) {
97 V_VT(res) = VT_BOOL;
98 V_BOOL(res) = val ? VARIANT_TRUE : VARIANT_FALSE;
100 return S_OK;
103 static HRESULT return_short(VARIANT *res, short val)
105 if(res) {
106 V_VT(res) = VT_I2;
107 V_I2(res) = val;
110 return S_OK;
113 static HRESULT return_int(VARIANT *res, int val)
115 if(res) {
116 V_VT(res) = VT_I4;
117 V_I4(res) = val;
120 return S_OK;
123 static inline HRESULT return_double(VARIANT *res, double val)
125 if(res) {
126 V_VT(res) = VT_R8;
127 V_R8(res) = val;
130 return S_OK;
133 static inline HRESULT return_float(VARIANT *res, float val)
135 if(res) {
136 V_VT(res) = VT_R4;
137 V_R4(res) = val;
140 return S_OK;
143 static inline HRESULT return_null(VARIANT *res)
145 if(res)
146 V_VT(res) = VT_NULL;
147 return S_OK;
150 static inline HRESULT return_date(VARIANT *res, double date)
152 if(res) {
153 V_VT(res) = VT_DATE;
154 V_DATE(res) = date;
156 return S_OK;
159 HRESULT to_int(VARIANT *v, int *ret)
161 VARIANT r;
162 HRESULT hres;
164 V_VT(&r) = VT_EMPTY;
165 hres = VariantChangeType(&r, v, 0, VT_I4);
166 if(FAILED(hres))
167 return hres;
169 *ret = V_I4(&r);
170 return S_OK;
173 static HRESULT to_double(VARIANT *v, double *ret)
175 VARIANT dst;
176 HRESULT hres;
178 V_VT(&dst) = VT_EMPTY;
179 hres = VariantChangeType(&dst, v, 0, VT_R8);
180 if(FAILED(hres))
181 return hres;
183 *ret = V_R8(&dst);
184 return S_OK;
187 static HRESULT to_string(VARIANT *v, BSTR *ret)
189 VARIANT dst;
190 HRESULT hres;
192 V_VT(&dst) = VT_EMPTY;
193 hres = VariantChangeType(&dst, v, VARIANT_LOCALBOOL, VT_BSTR);
194 if(FAILED(hres))
195 return hres;
197 *ret = V_BSTR(&dst);
198 return S_OK;
201 static HRESULT set_object_site(script_ctx_t *ctx, IUnknown *obj)
203 IObjectWithSite *obj_site;
204 IUnknown *ax_site;
205 HRESULT hres;
207 hres = IUnknown_QueryInterface(obj, &IID_IObjectWithSite, (void**)&obj_site);
208 if(FAILED(hres))
209 return S_OK;
211 ax_site = create_ax_site(ctx);
212 if(ax_site) {
213 hres = IObjectWithSite_SetSite(obj_site, ax_site);
214 IUnknown_Release(ax_site);
216 else
217 hres = E_OUTOFMEMORY;
218 IObjectWithSite_Release(obj_site);
219 return hres;
222 static IUnknown *create_object(script_ctx_t *ctx, const WCHAR *progid)
224 IInternetHostSecurityManager *secmgr = NULL;
225 struct CONFIRMSAFETY cs;
226 IClassFactoryEx *cfex;
227 IClassFactory *cf;
228 DWORD policy_size;
229 BYTE *bpolicy;
230 IUnknown *obj;
231 DWORD policy;
232 GUID guid;
233 HRESULT hres;
235 hres = CLSIDFromProgID(progid, &guid);
236 if(FAILED(hres))
237 return NULL;
239 TRACE("GUID %s\n", debugstr_guid(&guid));
241 if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
242 secmgr = get_sec_mgr(ctx);
243 if(!secmgr)
244 return NULL;
246 policy = 0;
247 hres = IInternetHostSecurityManager_ProcessUrlAction(secmgr, URLACTION_ACTIVEX_RUN,
248 (BYTE*)&policy, sizeof(policy), (BYTE*)&guid, sizeof(GUID), 0, 0);
249 if(FAILED(hres) || policy != URLPOLICY_ALLOW)
250 return NULL;
253 hres = CoGetClassObject(&guid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void**)&cf);
254 if(FAILED(hres))
255 return NULL;
257 hres = IClassFactory_QueryInterface(cf, &IID_IClassFactoryEx, (void**)&cfex);
258 if(SUCCEEDED(hres)) {
259 FIXME("Use IClassFactoryEx\n");
260 IClassFactoryEx_Release(cfex);
263 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (void**)&obj);
264 if(FAILED(hres))
265 return NULL;
267 if(secmgr) {
268 cs.clsid = guid;
269 cs.pUnk = obj;
270 cs.dwFlags = 0;
271 hres = IInternetHostSecurityManager_QueryCustomPolicy(secmgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
272 &bpolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
273 if(SUCCEEDED(hres)) {
274 policy = policy_size >= sizeof(DWORD) ? *(DWORD*)bpolicy : URLPOLICY_DISALLOW;
275 CoTaskMemFree(bpolicy);
278 if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
279 IUnknown_Release(obj);
280 return NULL;
284 hres = set_object_site(ctx, obj);
285 if(FAILED(hres)) {
286 IUnknown_Release(obj);
287 return NULL;
290 return obj;
293 static HRESULT show_msgbox(script_ctx_t *ctx, BSTR prompt, unsigned type, BSTR orig_title, VARIANT *res)
295 SCRIPTUICHANDLING uic_handling = SCRIPTUICHANDLING_ALLOW;
296 IActiveScriptSiteUIControl *ui_control;
297 IActiveScriptSiteWindow *acts_window;
298 WCHAR *title_buf = NULL;
299 const WCHAR *title;
300 HWND hwnd = NULL;
301 int ret = 0;
302 HRESULT hres;
304 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteUIControl, (void**)&ui_control);
305 if(SUCCEEDED(hres)) {
306 hres = IActiveScriptSiteUIControl_GetUIBehavior(ui_control, SCRIPTUICITEM_MSGBOX, &uic_handling);
307 IActiveScriptSiteUIControl_Release(ui_control);
308 if(FAILED(hres))
309 uic_handling = SCRIPTUICHANDLING_ALLOW;
312 switch(uic_handling) {
313 case SCRIPTUICHANDLING_ALLOW:
314 break;
315 case SCRIPTUICHANDLING_NOUIDEFAULT:
316 return return_short(res, 0);
317 default:
318 FIXME("blocked\n");
319 return E_FAIL;
322 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteWindow, (void**)&acts_window);
323 if(FAILED(hres)) {
324 FIXME("No IActiveScriptSiteWindow\n");
325 return hres;
328 if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
329 if(orig_title && *orig_title) {
330 WCHAR *ptr;
332 title = title_buf = heap_alloc(sizeof(vbscriptW) + (strlenW(orig_title)+2)*sizeof(WCHAR));
333 if(!title)
334 return E_OUTOFMEMORY;
336 memcpy(title_buf, vbscriptW, sizeof(vbscriptW));
337 ptr = title_buf + sizeof(vbscriptW)/sizeof(WCHAR)-1;
339 *ptr++ = ':';
340 *ptr++ = ' ';
341 strcpyW(ptr, orig_title);
342 }else {
343 title = vbscriptW;
345 }else {
346 title = orig_title ? orig_title : emptyW;
349 hres = IActiveScriptSiteWindow_GetWindow(acts_window, &hwnd);
350 if(SUCCEEDED(hres)) {
351 hres = IActiveScriptSiteWindow_EnableModeless(acts_window, FALSE);
352 if(SUCCEEDED(hres)) {
353 ret = MessageBoxW(hwnd, prompt, title, type);
354 hres = IActiveScriptSiteWindow_EnableModeless(acts_window, TRUE);
358 heap_free(title_buf);
359 IActiveScriptSiteWindow_Release(acts_window);
360 if(FAILED(hres)) {
361 FIXME("failed: %08x\n", hres);
362 return hres;
365 return return_short(res, ret);
368 static HRESULT Global_CCur(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
370 VARIANT v;
371 HRESULT hres;
373 TRACE("%s\n", debugstr_variant(arg));
375 assert(args_cnt == 1);
377 V_VT(&v) = VT_EMPTY;
378 hres = VariantChangeType(&v, arg, 0, VT_CY);
379 if(FAILED(hres))
380 return hres;
382 if(!res) {
383 VariantClear(&v);
384 return DISP_E_BADVARTYPE;
387 *res = v;
388 return S_OK;
391 static HRESULT Global_CInt(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
393 VARIANT v;
394 HRESULT hres;
396 TRACE("%s\n", debugstr_variant(arg));
398 assert(args_cnt == 1);
400 V_VT(&v) = VT_EMPTY;
401 hres = VariantChangeType(&v, arg, 0, VT_I2);
402 if(FAILED(hres))
403 return hres;
405 if(!res)
406 return DISP_E_BADVARTYPE;
407 else {
408 *res = v;
409 return S_OK;
413 static HRESULT Global_CLng(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
415 int i;
416 HRESULT hres;
418 TRACE("%s\n", debugstr_variant(arg));
420 assert(args_cnt == 1);
422 hres = to_int(arg, &i);
423 if(FAILED(hres))
424 return hres;
425 if(!res)
426 return DISP_E_BADVARTYPE;
428 return return_int(res, i);
431 static HRESULT Global_CBool(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
433 VARIANT v;
434 HRESULT hres;
436 TRACE("%s\n", debugstr_variant(arg));
438 assert(args_cnt == 1);
440 V_VT(&v) = VT_EMPTY;
441 hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_BOOL);
442 if(FAILED(hres))
443 return hres;
445 if(res)
446 *res = v;
447 else
448 VariantClear(&v);
449 return S_OK;
452 static HRESULT Global_CByte(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
454 VARIANT v;
455 HRESULT hres;
457 TRACE("%s\n", debugstr_variant(arg));
459 assert(args_cnt == 1);
461 V_VT(&v) = VT_EMPTY;
462 hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_UI1);
463 if(FAILED(hres))
464 return hres;
466 if(!res) {
467 VariantClear(&v);
468 return DISP_E_BADVARTYPE;
471 *res = v;
472 return S_OK;
475 static HRESULT Global_CDate(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
477 FIXME("\n");
478 return E_NOTIMPL;
481 static HRESULT Global_CDbl(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
483 VARIANT v;
484 HRESULT hres;
486 TRACE("%s\n", debugstr_variant(arg));
488 assert(args_cnt == 1);
490 V_VT(&v) = VT_EMPTY;
491 hres = VariantChangeType(&v, arg, 0, VT_R8);
492 if(FAILED(hres))
493 return hres;
495 if(!res)
496 return DISP_E_BADVARTYPE;
497 else {
498 *res = v;
499 return S_OK;
503 static HRESULT Global_CSng(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
505 VARIANT v;
506 HRESULT hres;
508 TRACE("%s\n", debugstr_variant(arg));
510 assert(args_cnt == 1);
512 V_VT(&v) = VT_EMPTY;
513 hres = VariantChangeType(&v, arg, 0, VT_R4);
514 if(FAILED(hres))
515 return hres;
517 if(!res)
518 return DISP_E_BADVARTYPE;
520 *res = v;
521 return S_OK;
524 static HRESULT Global_CStr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
526 BSTR str;
527 HRESULT hres;
529 TRACE("%s\n", debugstr_variant(arg));
531 hres = to_string(arg, &str);
532 if(FAILED(hres))
533 return hres;
535 return return_bstr(res, str);
538 static inline WCHAR hex_char(unsigned n)
540 return n < 10 ? '0'+n : 'A'+n-10;
543 static HRESULT Global_Hex(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
545 WCHAR buf[17], *ptr;
546 DWORD n;
547 HRESULT hres;
548 int ret;
550 TRACE("%s\n", debugstr_variant(arg));
552 switch(V_VT(arg)) {
553 case VT_I2:
554 n = (WORD)V_I2(arg);
555 break;
556 case VT_NULL:
557 if(res)
558 V_VT(res) = VT_NULL;
559 return S_OK;
560 default:
561 hres = to_int(arg, &ret);
562 if(FAILED(hres))
563 return hres;
564 else
565 n = ret;
568 buf[16] = 0;
569 ptr = buf+15;
571 if(n) {
572 do {
573 *ptr-- = hex_char(n & 0xf);
574 n >>= 4;
575 }while(n);
576 ptr++;
577 }else {
578 *ptr = '0';
581 return return_string(res, ptr);
584 static HRESULT Global_Oct(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
586 HRESULT hres;
587 WCHAR buf[23], *ptr;
588 DWORD n;
589 int ret;
591 TRACE("%s\n", debugstr_variant(arg));
593 switch(V_VT(arg)) {
594 case VT_I2:
595 n = (WORD)V_I2(arg);
596 break;
597 case VT_NULL:
598 if(res)
599 V_VT(res) = VT_NULL;
600 return S_OK;
601 default:
602 hres = to_int(arg, &ret);
603 if(FAILED(hres))
604 return hres;
605 else
606 n = ret;
609 buf[22] = 0;
610 ptr = buf + 21;
612 if(n) {
613 do {
614 *ptr-- = '0' + (n & 0x7);
615 n >>= 3;
616 }while(n);
617 ptr++;
618 }else {
619 *ptr = '0';
622 return return_string(res, ptr);
625 static HRESULT Global_VarType(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
627 TRACE("(%s)\n", debugstr_variant(arg));
629 assert(args_cnt == 1);
631 if(V_VT(arg) & ~VT_TYPEMASK) {
632 FIXME("not supported %s\n", debugstr_variant(arg));
633 return E_NOTIMPL;
636 return return_short(res, V_VT(arg));
639 static HRESULT Global_IsDate(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
641 FIXME("\n");
642 return E_NOTIMPL;
645 static HRESULT Global_IsEmpty(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
647 TRACE("(%s)\n", debugstr_variant(arg));
649 assert(args_cnt == 1);
651 if(res) {
652 V_VT(res) = VT_BOOL;
653 V_BOOL(res) = V_VT(arg) == VT_EMPTY ? VARIANT_TRUE : VARIANT_FALSE;
655 return S_OK;
658 static HRESULT Global_IsNull(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
660 TRACE("(%s)\n", debugstr_variant(arg));
662 assert(args_cnt == 1);
664 if(res) {
665 V_VT(res) = VT_BOOL;
666 V_BOOL(res) = V_VT(arg) == VT_NULL ? VARIANT_TRUE : VARIANT_FALSE;
668 return S_OK;
671 static HRESULT Global_IsNumeric(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
673 HRESULT hres;
674 double d;
676 TRACE("(%s)\n", debugstr_variant(arg));
678 assert(args_cnt == 1);
680 hres = to_double(arg, &d);
682 return return_bool(res, SUCCEEDED(hres));
685 static HRESULT Global_IsArray(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
687 FIXME("\n");
688 return E_NOTIMPL;
691 static HRESULT Global_IsObject(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
693 TRACE("(%s)\n", debugstr_variant(arg));
695 assert(args_cnt == 1);
697 if(res) {
698 V_VT(res) = VT_BOOL;
699 V_BOOL(res) = V_VT(arg) == VT_DISPATCH ? VARIANT_TRUE : VARIANT_FALSE;
701 return S_OK;
704 static HRESULT Global_Atn(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
706 HRESULT hres;
707 double d;
709 hres = to_double(arg, &d);
710 if(FAILED(hres))
711 return hres;
713 return return_double(res, atan(d));
716 static HRESULT Global_Cos(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
718 HRESULT hres;
719 double d;
721 hres = to_double(arg, &d);
722 if(FAILED(hres))
723 return hres;
725 return return_double(res, cos(d));
728 static HRESULT Global_Sin(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
730 HRESULT hres;
731 double d;
733 hres = to_double(arg, &d);
734 if(FAILED(hres))
735 return hres;
737 return return_double(res, sin(d));
740 static HRESULT Global_Tan(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
742 HRESULT hres;
743 double d;
745 hres = to_double(arg, &d);
746 if(FAILED(hres))
747 return hres;
749 return return_double(res, tan(d));
752 static HRESULT Global_Exp(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
754 HRESULT hres;
755 double d;
757 hres = to_double(arg, &d);
758 if(FAILED(hres))
759 return hres;
761 return return_double(res, exp(d));
764 static HRESULT Global_Log(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
766 HRESULT hres;
767 double d;
769 hres = to_double(arg, &d);
770 if(FAILED(hres))
771 return hres;
773 if(d <= 0)
774 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
775 else
776 return return_double(res, log(d));
779 static HRESULT Global_Sqr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
781 HRESULT hres;
782 double d;
784 hres = to_double(arg, &d);
785 if(FAILED(hres))
786 return hres;
788 if(d < 0)
789 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
790 else
791 return return_double(res, sqrt(d));
794 static HRESULT Global_Randomize(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
796 FIXME("\n");
797 return E_NOTIMPL;
800 static HRESULT Global_Rnd(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
802 FIXME("\n");
803 return E_NOTIMPL;
806 static HRESULT Global_Timer(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
808 SYSTEMTIME lt;
809 double sec;
811 GetLocalTime(&lt);
812 sec = lt.wHour * 3600 + lt.wMinute * 60 + lt.wSecond + lt.wMilliseconds / 1000.0;
813 return return_float(res, sec);
817 static HRESULT Global_LBound(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
819 FIXME("\n");
820 return E_NOTIMPL;
823 static HRESULT Global_UBound(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
825 FIXME("\n");
826 return E_NOTIMPL;
829 static HRESULT Global_RGB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
831 HRESULT hres;
832 int i, color[3];
834 TRACE("%s %s %s\n", debugstr_variant(arg), debugstr_variant(arg + 1), debugstr_variant(arg + 2));
836 assert(args_cnt == 3);
838 for(i = 0; i < 3; i++) {
839 hres = to_int(arg + i, color + i);
840 if(FAILED(hres))
841 return hres;
842 if(color[i] > 255)
843 color[i] = 255;
844 if(color[i] < 0)
845 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
848 return return_int(res, RGB(color[0], color[1], color[2]));
851 static HRESULT Global_Len(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
853 DWORD len;
854 HRESULT hres;
856 TRACE("%s\n", debugstr_variant(arg));
858 if(V_VT(arg) == VT_NULL)
859 return return_null(res);
861 if(V_VT(arg) != VT_BSTR) {
862 BSTR str;
864 hres = to_string(arg, &str);
865 if(FAILED(hres))
866 return hres;
868 len = SysStringLen(str);
869 SysFreeString(str);
870 }else {
871 len = SysStringLen(V_BSTR(arg));
874 return return_int(res, len);
877 static HRESULT Global_LenB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
879 FIXME("\n");
880 return E_NOTIMPL;
883 static HRESULT Global_Left(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
885 BSTR str, ret, conv_str = NULL;
886 int len, str_len;
887 HRESULT hres;
889 TRACE("(%s %s)\n", debugstr_variant(args+1), debugstr_variant(args));
891 if(V_VT(args) == VT_BSTR) {
892 str = V_BSTR(args);
893 }else {
894 hres = to_string(args, &conv_str);
895 if(FAILED(hres))
896 return hres;
897 str = conv_str;
900 hres = to_int(args+1, &len);
901 if(FAILED(hres))
902 return hres;
904 if(len < 0) {
905 FIXME("len = %d\n", len);
906 return E_FAIL;
909 str_len = SysStringLen(str);
910 if(len > str_len)
911 len = str_len;
913 ret = SysAllocStringLen(str, len);
914 SysFreeString(conv_str);
915 if(!ret)
916 return E_OUTOFMEMORY;
918 return return_bstr(res, ret);
921 static HRESULT Global_LeftB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
923 FIXME("\n");
924 return E_NOTIMPL;
927 static HRESULT Global_Right(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
929 BSTR str, ret, conv_str = NULL;
930 int len, str_len;
931 HRESULT hres;
933 TRACE("(%s %s)\n", debugstr_variant(args), debugstr_variant(args+1));
935 if(V_VT(args+1) == VT_BSTR) {
936 str = V_BSTR(args);
937 }else {
938 hres = to_string(args, &conv_str);
939 if(FAILED(hres))
940 return hres;
941 str = conv_str;
944 hres = to_int(args+1, &len);
945 if(FAILED(hres))
946 return hres;
948 if(len < 0) {
949 FIXME("len = %d\n", len);
950 return E_FAIL;
953 str_len = SysStringLen(str);
954 if(len > str_len)
955 len = str_len;
957 ret = SysAllocStringLen(str+str_len-len, len);
958 SysFreeString(conv_str);
959 if(!ret)
960 return E_OUTOFMEMORY;
962 return return_bstr(res, ret);
965 static HRESULT Global_RightB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
967 FIXME("\n");
968 return E_NOTIMPL;
971 static HRESULT Global_Mid(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
973 int len = -1, start, str_len;
974 BSTR str;
975 HRESULT hres;
977 TRACE("(%s %s ...)\n", debugstr_variant(args), debugstr_variant(args+1));
979 assert(args_cnt == 2 || args_cnt == 3);
981 if(V_VT(args) != VT_BSTR) {
982 FIXME("args[0] = %s\n", debugstr_variant(args));
983 return E_NOTIMPL;
986 str = V_BSTR(args);
988 hres = to_int(args+1, &start);
989 if(FAILED(hres))
990 return hres;
992 if(args_cnt == 3) {
993 hres = to_int(args+2, &len);
994 if(FAILED(hres))
995 return hres;
997 if(len < 0) {
998 FIXME("len = %d\n", len);
999 return E_FAIL;
1004 str_len = SysStringLen(str);
1005 start--;
1006 if(start > str_len)
1007 start = str_len;
1009 if(len == -1)
1010 len = str_len-start;
1011 else if(len > str_len-start)
1012 len = str_len-start;
1014 if(res) {
1015 V_VT(res) = VT_BSTR;
1016 V_BSTR(res) = SysAllocStringLen(str+start, len);
1017 if(!V_BSTR(res))
1018 return E_OUTOFMEMORY;
1021 return S_OK;
1024 static HRESULT Global_MidB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1026 FIXME("\n");
1027 return E_NOTIMPL;
1030 static HRESULT Global_StrComp(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1032 FIXME("\n");
1033 return E_NOTIMPL;
1036 static HRESULT Global_LCase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1038 BSTR str;
1039 HRESULT hres;
1041 TRACE("%s\n", debugstr_variant(arg));
1043 if(V_VT(arg) == VT_NULL) {
1044 if(res)
1045 V_VT(res) = VT_NULL;
1046 return S_OK;
1049 hres = to_string(arg, &str);
1050 if(FAILED(hres))
1051 return hres;
1053 if(res) {
1054 WCHAR *ptr;
1056 for(ptr = str; *ptr; ptr++)
1057 *ptr = tolowerW(*ptr);
1059 V_VT(res) = VT_BSTR;
1060 V_BSTR(res) = str;
1061 }else {
1062 SysFreeString(str);
1064 return S_OK;
1067 static HRESULT Global_UCase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1069 BSTR str;
1070 HRESULT hres;
1072 TRACE("%s\n", debugstr_variant(arg));
1074 if(V_VT(arg) == VT_NULL) {
1075 if(res)
1076 V_VT(res) = VT_NULL;
1077 return S_OK;
1080 hres = to_string(arg, &str);
1081 if(FAILED(hres))
1082 return hres;
1084 if(res) {
1085 WCHAR *ptr;
1087 for(ptr = str; *ptr; ptr++)
1088 *ptr = toupperW(*ptr);
1090 V_VT(res) = VT_BSTR;
1091 V_BSTR(res) = str;
1092 }else {
1093 SysFreeString(str);
1095 return S_OK;
1098 static HRESULT Global_LTrim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1100 BSTR str, conv_str = NULL;
1101 WCHAR *ptr;
1102 HRESULT hres;
1104 TRACE("%s\n", debugstr_variant(arg));
1106 if(V_VT(arg) == VT_BSTR) {
1107 str = V_BSTR(arg);
1108 }else {
1109 hres = to_string(arg, &conv_str);
1110 if(FAILED(hres))
1111 return hres;
1112 str = conv_str;
1115 for(ptr = str; *ptr && isspaceW(*ptr); ptr++);
1117 str = SysAllocString(ptr);
1118 SysFreeString(conv_str);
1119 if(!str)
1120 return E_OUTOFMEMORY;
1122 return return_bstr(res, str);
1125 static HRESULT Global_RTrim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1127 BSTR str, conv_str = NULL;
1128 WCHAR *ptr;
1129 HRESULT hres;
1131 TRACE("%s\n", debugstr_variant(arg));
1133 if(V_VT(arg) == VT_BSTR) {
1134 str = V_BSTR(arg);
1135 }else {
1136 hres = to_string(arg, &conv_str);
1137 if(FAILED(hres))
1138 return hres;
1139 str = conv_str;
1142 for(ptr = str+SysStringLen(str); ptr-1 > str && isspaceW(*(ptr-1)); ptr--);
1144 str = SysAllocStringLen(str, ptr-str);
1145 SysFreeString(conv_str);
1146 if(!str)
1147 return E_OUTOFMEMORY;
1149 return return_bstr(res, str);
1152 static HRESULT Global_Trim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1154 BSTR str, conv_str = NULL;
1155 WCHAR *begin_ptr, *end_ptr;
1156 HRESULT hres;
1158 TRACE("%s\n", debugstr_variant(arg));
1160 if(V_VT(arg) == VT_BSTR) {
1161 str = V_BSTR(arg);
1162 }else {
1163 hres = to_string(arg, &conv_str);
1164 if(FAILED(hres))
1165 return hres;
1166 str = conv_str;
1169 for(begin_ptr = str; *begin_ptr && isspaceW(*begin_ptr); begin_ptr++);
1170 for(end_ptr = str+SysStringLen(str); end_ptr-1 > begin_ptr && isspaceW(*(end_ptr-1)); end_ptr--);
1172 str = SysAllocStringLen(begin_ptr, end_ptr-begin_ptr);
1173 SysFreeString(conv_str);
1174 if(!str)
1175 return E_OUTOFMEMORY;
1177 return return_bstr(res, str);
1180 static HRESULT Global_Space(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1182 BSTR str;
1183 int n, i;
1184 HRESULT hres;
1186 TRACE("%s\n", debugstr_variant(arg));
1188 hres = to_int(arg, &n);
1189 if(FAILED(hres))
1190 return hres;
1192 if(n < 0) {
1193 FIXME("n = %d\n", n);
1194 return E_NOTIMPL;
1197 if(!res)
1198 return S_OK;
1200 str = SysAllocStringLen(NULL, n);
1201 if(!str)
1202 return E_OUTOFMEMORY;
1204 for(i=0; i<n; i++)
1205 str[i] = ' ';
1207 V_VT(res) = VT_BSTR;
1208 V_BSTR(res) = str;
1209 return S_OK;
1212 static HRESULT Global_String(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1214 FIXME("\n");
1215 return E_NOTIMPL;
1218 static HRESULT Global_InStr(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1220 VARIANT *startv, *str1v, *str2v;
1221 BSTR str1, str2;
1222 int start, ret;
1223 HRESULT hres;
1225 TRACE("\n");
1227 assert(2 <= args_cnt && args_cnt <= 4);
1229 switch(args_cnt) {
1230 case 2:
1231 startv = NULL;
1232 str1v = args;
1233 str2v = args+1;
1234 break;
1235 case 3:
1236 startv = args;
1237 str1v = args+1;
1238 str2v = args+2;
1239 break;
1240 case 4:
1241 FIXME("unsupported compare argument %s\n", debugstr_variant(args));
1242 return E_NOTIMPL;
1243 DEFAULT_UNREACHABLE;
1246 if(startv) {
1247 hres = to_int(startv, &start);
1248 if(FAILED(hres))
1249 return hres;
1250 if(--start < 0) {
1251 FIXME("start %d\n", start);
1252 return E_FAIL;
1254 }else {
1255 start = 0;
1258 if(V_VT(str1v) == VT_NULL || V_VT(str2v) == VT_NULL)
1259 return return_null(res);
1261 if(V_VT(str1v) != VT_BSTR) {
1262 FIXME("Unsupported str1 type %s\n", debugstr_variant(str1v));
1263 return E_NOTIMPL;
1265 str1 = V_BSTR(str1v);
1267 if(V_VT(str2v) != VT_BSTR) {
1268 FIXME("Unsupported str2 type %s\n", debugstr_variant(str2v));
1269 return E_NOTIMPL;
1271 str2 = V_BSTR(str2v);
1273 if(start < SysStringLen(str1)) {
1274 WCHAR *ptr;
1276 ptr = strstrW(str1+start, str2);
1277 ret = ptr ? ptr-str1+1 : 0;
1278 }else {
1279 ret = 0;
1282 return return_int(res, ret);
1285 static HRESULT Global_InStrB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1287 FIXME("\n");
1288 return E_NOTIMPL;
1291 static HRESULT Global_AscB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1293 FIXME("\n");
1294 return E_NOTIMPL;
1297 static HRESULT Global_ChrB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1299 FIXME("\n");
1300 return E_NOTIMPL;
1303 static HRESULT Global_Asc(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1305 FIXME("\n");
1306 return E_NOTIMPL;
1309 /* The function supports only single-byte and double-byte character sets. It
1310 * ignores language specified by IActiveScriptSite::GetLCID. The argument needs
1311 * to be in range of short or unsigned short. */
1312 static HRESULT Global_Chr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1314 int cp, c, len = 0;
1315 CPINFO cpi;
1316 WCHAR ch;
1317 char buf[2];
1318 HRESULT hres;
1320 TRACE("%s\n", debugstr_variant(arg));
1322 hres = to_int(arg, &c);
1323 if(FAILED(hres))
1324 return hres;
1326 cp = GetACP();
1327 if(!GetCPInfo(cp, &cpi))
1328 cpi.MaxCharSize = 1;
1330 if((c!=(short)c && c!=(unsigned short)c) ||
1331 (unsigned short)c>=(cpi.MaxCharSize>1 ? 0x10000 : 0x100)) {
1332 WARN("invalid arg %d\n", c);
1333 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
1336 if(c>>8)
1337 buf[len++] = c>>8;
1338 if(!len || IsDBCSLeadByteEx(cp, buf[0]))
1339 buf[len++] = c;
1340 if(!MultiByteToWideChar(CP_ACP, 0, buf, len, &ch, 1)) {
1341 WARN("invalid arg %d, cp %d\n", c, cp);
1342 return E_FAIL;
1345 if(res) {
1346 V_VT(res) = VT_BSTR;
1347 V_BSTR(res) = SysAllocStringLen(&ch, 1);
1348 if(!V_BSTR(res))
1349 return E_OUTOFMEMORY;
1351 return S_OK;
1354 static HRESULT Global_AscW(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1356 FIXME("\n");
1357 return E_NOTIMPL;
1360 static HRESULT Global_ChrW(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1362 FIXME("\n");
1363 return E_NOTIMPL;
1366 static HRESULT Global_Abs(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1368 HRESULT hres;
1369 VARIANT dst;
1371 TRACE("(%s)\n", debugstr_variant(arg));
1373 assert(args_cnt == 1);
1375 hres = VarAbs(arg, &dst);
1376 if(FAILED(hres))
1377 return hres;
1379 if (res)
1380 *res = dst;
1381 else
1382 VariantClear(&dst);
1384 return S_OK;
1387 static HRESULT Global_Fix(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1389 HRESULT hres;
1390 VARIANT dst;
1392 TRACE("(%s)\n", debugstr_variant(arg));
1394 assert(args_cnt == 1);
1396 hres = VarFix(arg, &dst);
1397 if(FAILED(hres))
1398 return hres;
1400 if (res)
1401 *res = dst;
1402 else
1403 VariantClear(&dst);
1405 return S_OK;
1408 static HRESULT Global_Int(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1410 HRESULT hres;
1411 VARIANT dst;
1413 TRACE("(%s)\n", debugstr_variant(arg));
1415 assert(args_cnt == 1);
1417 hres = VarInt(arg, &dst);
1418 if(FAILED(hres))
1419 return hres;
1421 if (res)
1422 *res = dst;
1423 else
1424 VariantClear(&dst);
1426 return S_OK;
1429 static HRESULT Global_Sgn(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1431 double v;
1432 short val;
1433 HRESULT hres;
1435 TRACE("(%s)\n", debugstr_variant(arg));
1437 assert(args_cnt == 1);
1439 if(V_VT(arg) == VT_NULL)
1440 return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
1442 hres = to_double(arg, &v);
1443 if (FAILED(hres))
1444 return hres;
1446 val = v == 0 ? 0 : (v > 0 ? 1 : -1);
1447 return return_short(res, val);
1450 static HRESULT Global_Now(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1452 SYSTEMTIME lt;
1453 double date;
1455 TRACE("\n");
1457 GetLocalTime(&lt);
1458 SystemTimeToVariantTime(&lt, &date);
1459 return return_date(res, date);
1462 static HRESULT Global_Date(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1464 SYSTEMTIME lt;
1465 UDATE ud;
1466 DATE date;
1467 HRESULT hres;
1469 TRACE("\n");
1471 GetLocalTime(&lt);
1472 ud.st = lt;
1473 ud.wDayOfYear = 0;
1474 hres = VarDateFromUdateEx(&ud, 0, VAR_DATEVALUEONLY, &date);
1475 if(FAILED(hres))
1476 return hres;
1477 return return_date(res, date);
1480 static HRESULT Global_Time(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1482 SYSTEMTIME lt;
1483 UDATE ud;
1484 DATE time;
1485 HRESULT hres;
1487 TRACE("\n");
1489 GetLocalTime(&lt);
1490 ud.st = lt;
1491 ud.wDayOfYear = 0;
1492 hres = VarDateFromUdateEx(&ud, 0, VAR_TIMEVALUEONLY, &time);
1493 if(FAILED(hres))
1494 return hres;
1495 return return_date(res, time);
1498 static HRESULT Global_Day(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1500 FIXME("\n");
1501 return E_NOTIMPL;
1504 static HRESULT Global_Month(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1506 FIXME("\n");
1507 return E_NOTIMPL;
1510 static HRESULT Global_Weekday(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1512 FIXME("\n");
1513 return E_NOTIMPL;
1516 static HRESULT Global_Year(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1518 FIXME("\n");
1519 return E_NOTIMPL;
1522 static HRESULT Global_Hour(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1524 FIXME("\n");
1525 return E_NOTIMPL;
1528 static HRESULT Global_Minute(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1530 FIXME("\n");
1531 return E_NOTIMPL;
1534 static HRESULT Global_Second(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1536 FIXME("\n");
1537 return E_NOTIMPL;
1540 static HRESULT Global_DateValue(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1542 FIXME("\n");
1543 return E_NOTIMPL;
1546 static HRESULT Global_TimeValue(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1548 FIXME("\n");
1549 return E_NOTIMPL;
1552 static HRESULT Global_DateSerial(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1554 FIXME("\n");
1555 return E_NOTIMPL;
1558 static HRESULT Global_TimeSerial(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1560 FIXME("\n");
1561 return E_NOTIMPL;
1564 static HRESULT Global_InputBox(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1566 FIXME("\n");
1567 return E_NOTIMPL;
1570 static HRESULT Global_MsgBox(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1572 BSTR prompt, title = NULL;
1573 int type = MB_OK;
1574 HRESULT hres;
1576 TRACE("\n");
1578 assert(1 <= args_cnt && args_cnt <= 5);
1580 hres = to_string(args, &prompt);
1581 if(FAILED(hres))
1582 return hres;
1584 if(args_cnt > 1)
1585 hres = to_int(args+1, &type);
1587 if(SUCCEEDED(hres) && args_cnt > 2)
1588 hres = to_string(args+2, &title);
1590 if(SUCCEEDED(hres) && args_cnt > 3) {
1591 FIXME("unsupported arg_cnt %d\n", args_cnt);
1592 hres = E_NOTIMPL;
1595 if(SUCCEEDED(hres))
1596 hres = show_msgbox(This->desc->ctx, prompt, type, title, res);
1598 SysFreeString(prompt);
1599 SysFreeString(title);
1600 return hres;
1603 static HRESULT Global_CreateObject(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1605 IUnknown *obj;
1606 HRESULT hres;
1608 TRACE("(%s)\n", debugstr_variant(arg));
1610 if(V_VT(arg) != VT_BSTR) {
1611 FIXME("non-bstr arg\n");
1612 return E_INVALIDARG;
1615 obj = create_object(This->desc->ctx, V_BSTR(arg));
1616 if(!obj)
1617 return VB_E_CANNOT_CREATE_OBJ;
1619 if(res) {
1620 hres = IUnknown_QueryInterface(obj, &IID_IDispatch, (void**)&V_DISPATCH(res));
1621 if(FAILED(hres))
1622 return hres;
1624 V_VT(res) = VT_DISPATCH;
1627 IUnknown_Release(obj);
1628 return S_OK;
1631 static HRESULT Global_GetObject(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1633 IBindCtx *bind_ctx;
1634 IUnknown *obj_unk;
1635 IDispatch *disp;
1636 ULONG eaten = 0;
1637 IMoniker *mon;
1638 HRESULT hres;
1640 TRACE("%s %s\n", args_cnt ? debugstr_variant(args) : "", args_cnt > 1 ? debugstr_variant(args+1) : "");
1642 if(args_cnt != 1 || V_VT(args) != VT_BSTR) {
1643 FIXME("unsupported args\n");
1644 return E_NOTIMPL;
1647 if(This->desc->ctx->safeopt & (INTERFACE_USES_SECURITY_MANAGER|INTERFACESAFE_FOR_UNTRUSTED_DATA)) {
1648 WARN("blocked in current safety mode\n");
1649 return VB_E_CANNOT_CREATE_OBJ;
1652 hres = CreateBindCtx(0, &bind_ctx);
1653 if(FAILED(hres))
1654 return hres;
1656 hres = MkParseDisplayName(bind_ctx, V_BSTR(args), &eaten, &mon);
1657 if(SUCCEEDED(hres)) {
1658 hres = IMoniker_BindToObject(mon, bind_ctx, NULL, &IID_IUnknown, (void**)&obj_unk);
1659 IMoniker_Release(mon);
1660 }else {
1661 hres = MK_E_SYNTAX;
1663 IBindCtx_Release(bind_ctx);
1664 if(FAILED(hres))
1665 return hres;
1667 hres = set_object_site(This->desc->ctx, obj_unk);
1668 if(FAILED(hres)) {
1669 IUnknown_Release(obj_unk);
1670 return hres;
1673 hres = IUnknown_QueryInterface(obj_unk, &IID_IDispatch, (void**)&disp);
1674 if(SUCCEEDED(hres)) {
1675 if(res) {
1676 V_VT(res) = VT_DISPATCH;
1677 V_DISPATCH(res) = disp;
1678 }else {
1679 IDispatch_Release(disp);
1681 }else {
1682 FIXME("object does not support IDispatch\n");
1685 return hres;
1688 static HRESULT Global_DateAdd(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1690 FIXME("\n");
1691 return E_NOTIMPL;
1694 static HRESULT Global_DateDiff(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1696 FIXME("\n");
1697 return E_NOTIMPL;
1700 static HRESULT Global_DatePart(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1702 FIXME("\n");
1703 return E_NOTIMPL;
1706 static HRESULT Global_TypeName(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1708 static const WCHAR ByteW[] = {'B', 'y', 't', 'e', 0};
1709 static const WCHAR IntegerW[] = {'I', 'n', 't', 'e', 'g', 'e', 'r', 0};
1710 static const WCHAR LongW[] = {'L', 'o', 'n', 'g', 0};
1711 static const WCHAR SingleW[] = {'S', 'i', 'n', 'g', 'l', 'e', 0};
1712 static const WCHAR DoubleW[] = {'D', 'o', 'u', 'b', 'l', 'e', 0};
1713 static const WCHAR CurrencyW[] = {'C', 'u', 'r', 'r', 'e', 'n', 'c', 'y', 0};
1714 static const WCHAR DecimalW[] = {'D', 'e', 'c', 'i', 'm', 'a', 'l', 0};
1715 static const WCHAR DateW[] = {'D', 'a', 't', 'e', 0};
1716 static const WCHAR StringW[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
1717 static const WCHAR BooleanW[] = {'B', 'o', 'o', 'l', 'e', 'a', 'n', 0};
1718 static const WCHAR EmptyW[] = {'E', 'm', 'p', 't', 'y', 0};
1719 static const WCHAR NullW[] = {'N', 'u', 'l', 'l', 0};
1721 TRACE("(%s)\n", debugstr_variant(arg));
1723 assert(args_cnt == 1);
1725 switch(V_VT(arg)) {
1726 case VT_UI1:
1727 return return_string(res, ByteW);
1728 case VT_I2:
1729 return return_string(res, IntegerW);
1730 case VT_I4:
1731 return return_string(res, LongW);
1732 case VT_R4:
1733 return return_string(res, SingleW);
1734 case VT_R8:
1735 return return_string(res, DoubleW);
1736 case VT_CY:
1737 return return_string(res, CurrencyW);
1738 case VT_DECIMAL:
1739 return return_string(res, DecimalW);
1740 case VT_DATE:
1741 return return_string(res, DateW);
1742 case VT_BSTR:
1743 return return_string(res, StringW);
1744 case VT_BOOL:
1745 return return_string(res, BooleanW);
1746 case VT_EMPTY:
1747 return return_string(res, EmptyW);
1748 case VT_NULL:
1749 return return_string(res, NullW);
1750 default:
1751 FIXME("arg %s not supported\n", debugstr_variant(arg));
1752 return E_NOTIMPL;
1756 static HRESULT Global_Array(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1758 FIXME("\n");
1759 return E_NOTIMPL;
1762 static HRESULT Global_Erase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1764 FIXME("\n");
1765 return E_NOTIMPL;
1768 static HRESULT Global_Filter(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1770 FIXME("\n");
1771 return E_NOTIMPL;
1774 static HRESULT Global_Join(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1776 FIXME("\n");
1777 return E_NOTIMPL;
1780 static HRESULT Global_Split(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1782 FIXME("\n");
1783 return E_NOTIMPL;
1786 static HRESULT Global_Replace(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1788 FIXME("\n");
1789 return E_NOTIMPL;
1792 static HRESULT Global_StrReverse(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1794 WCHAR *ptr1, *ptr2, ch;
1795 BSTR ret;
1796 HRESULT hres;
1798 TRACE("%s\n", debugstr_variant(arg));
1800 hres = to_string(arg, &ret);
1801 if(FAILED(hres))
1802 return hres;
1804 ptr1 = ret;
1805 ptr2 = ret + SysStringLen(ret)-1;
1806 while(ptr1 < ptr2) {
1807 ch = *ptr1;
1808 *ptr1++ = *ptr2;
1809 *ptr2-- = ch;
1812 return return_bstr(res, ret);
1815 static HRESULT Global_InStrRev(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1817 int start, ret = 0;
1818 BSTR str1, str2;
1819 HRESULT hres;
1821 TRACE("%s %s arg_cnt=%u\n", debugstr_variant(args), debugstr_variant(args+1), args_cnt);
1823 if(args_cnt > 3) {
1824 FIXME("Unsupported args\n");
1825 return E_NOTIMPL;
1828 assert(2 <= args_cnt && args_cnt <= 4);
1830 if(V_VT(args) == VT_NULL || V_VT(args+1) == VT_NULL || (args_cnt > 2 && V_VT(args+2) == VT_NULL))
1831 return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
1833 hres = to_string(args, &str1);
1834 if(FAILED(hres))
1835 return hres;
1837 hres = to_string(args+1, &str2);
1838 if(SUCCEEDED(hres)) {
1839 if(args_cnt > 2) {
1840 hres = to_int(args+2, &start);
1841 if(SUCCEEDED(hres) && start <= 0) {
1842 FIXME("Unsupported start %d\n", start);
1843 hres = E_NOTIMPL;
1845 }else {
1846 start = SysStringLen(str1);
1848 } else {
1849 str2 = NULL;
1852 if(SUCCEEDED(hres)) {
1853 const WCHAR *ptr;
1854 size_t len;
1856 len = SysStringLen(str2);
1857 if(start >= len && start <= SysStringLen(str1)) {
1858 for(ptr = str1+start-SysStringLen(str2); ptr >= str1; ptr--) {
1859 if(!memcmp(ptr, str2, len*sizeof(WCHAR))) {
1860 ret = ptr-str1+1;
1861 break;
1867 SysFreeString(str1);
1868 SysFreeString(str2);
1869 if(FAILED(hres))
1870 return hres;
1872 return return_int(res, ret);
1875 static HRESULT Global_LoadPicture(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1877 FIXME("\n");
1878 return E_NOTIMPL;
1881 static HRESULT Global_ScriptEngine(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1883 TRACE("%s\n", debugstr_variant(arg));
1885 assert(args_cnt == 0);
1887 return return_string(res, vbscriptW);
1890 static HRESULT Global_ScriptEngineMajorVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1892 TRACE("%s\n", debugstr_variant(arg));
1894 assert(args_cnt == 0);
1896 return return_int(res, VBSCRIPT_MAJOR_VERSION);
1899 static HRESULT Global_ScriptEngineMinorVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1901 TRACE("%s\n", debugstr_variant(arg));
1903 assert(args_cnt == 0);
1905 return return_int(res, VBSCRIPT_MINOR_VERSION);
1908 static HRESULT Global_ScriptEngineBuildVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1910 TRACE("%s\n", debugstr_variant(arg));
1912 assert(args_cnt == 0);
1914 return return_int(res, VBSCRIPT_BUILD_VERSION);
1917 static HRESULT Global_FormatNumber(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1919 FIXME("\n");
1920 return E_NOTIMPL;
1923 static HRESULT Global_FormatCurrency(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1925 FIXME("\n");
1926 return E_NOTIMPL;
1929 static HRESULT Global_FormatPercent(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1931 FIXME("\n");
1932 return E_NOTIMPL;
1935 static HRESULT Global_FormatDateTime(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1937 FIXME("\n");
1938 return E_NOTIMPL;
1941 static HRESULT Global_WeekdayName(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1943 int weekday, first_day = 1, abbrev = 0;
1944 BSTR ret;
1945 HRESULT hres;
1947 TRACE("\n");
1949 assert(1 <= args_cnt && args_cnt <= 3);
1951 hres = to_int(args, &weekday);
1952 if(FAILED(hres))
1953 return hres;
1955 if(args_cnt > 1) {
1956 hres = to_int(args+1, &abbrev);
1957 if(FAILED(hres))
1958 return hres;
1960 if(args_cnt == 3) {
1961 hres = to_int(args+2, &first_day);
1962 if(FAILED(hres))
1963 return hres;
1967 hres = VarWeekdayName(weekday, abbrev, first_day, 0, &ret);
1968 if(FAILED(hres))
1969 return hres;
1971 return return_bstr(res, ret);
1974 static HRESULT Global_MonthName(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1976 int month, abbrev = 0;
1977 BSTR ret;
1978 HRESULT hres;
1980 TRACE("\n");
1982 assert(args_cnt == 1 || args_cnt == 2);
1984 hres = to_int(args, &month);
1985 if(FAILED(hres))
1986 return hres;
1988 if(args_cnt == 2) {
1989 hres = to_int(args+1, &abbrev);
1990 if(FAILED(hres))
1991 return hres;
1994 hres = VarMonthName(month, abbrev, 0, &ret);
1995 if(FAILED(hres))
1996 return hres;
1998 return return_bstr(res, ret);
2001 static HRESULT Global_Round(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2003 double n;
2004 HRESULT hres;
2006 TRACE("%s\n", debugstr_variant(arg));
2008 if(!res)
2009 return S_OK;
2011 switch(V_VT(arg)) {
2012 case VT_I2:
2013 case VT_I4:
2014 case VT_BOOL:
2015 *res = *arg;
2016 return S_OK;
2017 case VT_R8:
2018 n = V_R8(arg);
2019 break;
2020 default:
2021 hres = to_double(arg, &n);
2022 if(FAILED(hres))
2023 return hres;
2026 return return_double(res, round(n));
2029 static HRESULT Global_Escape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2031 FIXME("\n");
2032 return E_NOTIMPL;
2035 static HRESULT Global_Unescape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2037 FIXME("\n");
2038 return E_NOTIMPL;
2041 static HRESULT Global_Eval(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2043 FIXME("\n");
2044 return E_NOTIMPL;
2047 static HRESULT Global_Execute(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2049 FIXME("\n");
2050 return E_NOTIMPL;
2053 static HRESULT Global_ExecuteGlobal(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2055 FIXME("\n");
2056 return E_NOTIMPL;
2059 static HRESULT Global_GetRef(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2061 FIXME("\n");
2062 return E_NOTIMPL;
2065 static const string_constant_t vbCr = {1, {'\r'}};
2066 static const string_constant_t vbCrLf = {2, {'\r','\n'}};
2067 static const string_constant_t vbNewLine = {2, {'\r','\n'}};
2068 static const string_constant_t vbFormFeed = {1, {0xc}};
2069 static const string_constant_t vbLf = {1, {'\n'}};
2070 static const string_constant_t vbNullChar = {1};
2071 static const string_constant_t vbNullString = {0};
2072 static const string_constant_t vbTab = {1, {'\t'}};
2073 static const string_constant_t vbVerticalTab = {1, {0xb}};
2075 static const builtin_prop_t global_props[] = {
2076 {DISPID_GLOBAL_VBUSESYSTEM, NULL, BP_GET, VT_I2, 0},
2077 {DISPID_GLOBAL_USESYSTEMDAYOFWEEK, NULL, BP_GET, VT_I2, 0},
2078 {DISPID_GLOBAL_VBSUNDAY, NULL, BP_GET, VT_I2, 1},
2079 {DISPID_GLOBAL_VBMONDAY, NULL, BP_GET, VT_I2, 2},
2080 {DISPID_GLOBAL_VBTUESDAY, NULL, BP_GET, VT_I2, 3},
2081 {DISPID_GLOBAL_VBWEDNESDAY, NULL, BP_GET, VT_I2, 4},
2082 {DISPID_GLOBAL_VBTHURSDAY, NULL, BP_GET, VT_I2, 5},
2083 {DISPID_GLOBAL_VBFRIDAY, NULL, BP_GET, VT_I2, 6},
2084 {DISPID_GLOBAL_VBSATURDAY, NULL, BP_GET, VT_I2, 7},
2085 {DISPID_GLOBAL_VBFIRSTJAN1, NULL, BP_GET, VT_I2, 1},
2086 {DISPID_GLOBAL_VBFIRSTFOURDAYS, NULL, BP_GET, VT_I2, 2},
2087 {DISPID_GLOBAL_VBFIRSTFULLWEEK, NULL, BP_GET, VT_I2, 3},
2088 {DISPID_GLOBAL_VBOKONLY, NULL, BP_GET, VT_I2, MB_OK},
2089 {DISPID_GLOBAL_VBOKCANCEL, NULL, BP_GET, VT_I2, MB_OKCANCEL},
2090 {DISPID_GLOBAL_VBABORTRETRYIGNORE, NULL, BP_GET, VT_I2, MB_ABORTRETRYIGNORE},
2091 {DISPID_GLOBAL_VBYESNOCANCEL, NULL, BP_GET, VT_I2, MB_YESNOCANCEL},
2092 {DISPID_GLOBAL_VBYESNO, NULL, BP_GET, VT_I2, MB_YESNO},
2093 {DISPID_GLOBAL_VBRETRYCANCEL, NULL, BP_GET, VT_I2, MB_RETRYCANCEL},
2094 {DISPID_GLOBAL_VBCRITICAL, NULL, BP_GET, VT_I2, MB_ICONHAND},
2095 {DISPID_GLOBAL_VBQUESTION, NULL, BP_GET, VT_I2, MB_ICONQUESTION},
2096 {DISPID_GLOBAL_VBEXCLAMATION, NULL, BP_GET, VT_I2, MB_ICONEXCLAMATION},
2097 {DISPID_GLOBAL_VBINFORMATION, NULL, BP_GET, VT_I2, MB_ICONASTERISK},
2098 {DISPID_GLOBAL_VBDEFAULTBUTTON1, NULL, BP_GET, VT_I2, MB_DEFBUTTON1},
2099 {DISPID_GLOBAL_VBDEFAULTBUTTON2, NULL, BP_GET, VT_I2, MB_DEFBUTTON2},
2100 {DISPID_GLOBAL_VBDEFAULTBUTTON3, NULL, BP_GET, VT_I2, MB_DEFBUTTON3},
2101 {DISPID_GLOBAL_VBDEFAULTBUTTON4, NULL, BP_GET, VT_I2, MB_DEFBUTTON4},
2102 {DISPID_GLOBAL_VBAPPLICATIONMODAL, NULL, BP_GET, VT_I2, MB_APPLMODAL},
2103 {DISPID_GLOBAL_VBSYSTEMMODAL, NULL, BP_GET, VT_I2, MB_SYSTEMMODAL},
2104 {DISPID_GLOBAL_VBOK, NULL, BP_GET, VT_I2, IDOK},
2105 {DISPID_GLOBAL_VBCANCEL, NULL, BP_GET, VT_I2, IDCANCEL},
2106 {DISPID_GLOBAL_VBABORT, NULL, BP_GET, VT_I2, IDABORT},
2107 {DISPID_GLOBAL_VBRETRY, NULL, BP_GET, VT_I2, IDRETRY},
2108 {DISPID_GLOBAL_VBIGNORE, NULL, BP_GET, VT_I2, IDIGNORE},
2109 {DISPID_GLOBAL_VBYES, NULL, BP_GET, VT_I2, IDYES},
2110 {DISPID_GLOBAL_VBNO, NULL, BP_GET, VT_I2, IDNO},
2111 {DISPID_GLOBAL_VBEMPTY, NULL, BP_GET, VT_I2, VT_EMPTY},
2112 {DISPID_GLOBAL_VBNULL, NULL, BP_GET, VT_I2, VT_NULL},
2113 {DISPID_GLOBAL_VBINTEGER, NULL, BP_GET, VT_I2, VT_I2},
2114 {DISPID_GLOBAL_VBLONG, NULL, BP_GET, VT_I2, VT_I4},
2115 {DISPID_GLOBAL_VBSINGLE, NULL, BP_GET, VT_I2, VT_R4},
2116 {DISPID_GLOBAL_VBDOUBLE, NULL, BP_GET, VT_I2, VT_R8},
2117 {DISPID_GLOBAL_VBCURRENCY, NULL, BP_GET, VT_I2, VT_CY},
2118 {DISPID_GLOBAL_VBDATE, NULL, BP_GET, VT_I2, VT_DATE},
2119 {DISPID_GLOBAL_VBSTRING, NULL, BP_GET, VT_I2, VT_BSTR},
2120 {DISPID_GLOBAL_VBOBJECT, NULL, BP_GET, VT_I2, VT_DISPATCH},
2121 {DISPID_GLOBAL_VBERROR, NULL, BP_GET, VT_I2, VT_ERROR},
2122 {DISPID_GLOBAL_VBBOOLEAN, NULL, BP_GET, VT_I2, VT_BOOL},
2123 {DISPID_GLOBAL_VBVARIANT, NULL, BP_GET, VT_I2, VT_VARIANT},
2124 {DISPID_GLOBAL_VBDATAOBJECT, NULL, BP_GET, VT_I2, VT_UNKNOWN},
2125 {DISPID_GLOBAL_VBDECIMAL, NULL, BP_GET, VT_I2, VT_DECIMAL},
2126 {DISPID_GLOBAL_VBBYTE, NULL, BP_GET, VT_I2, VT_UI1},
2127 {DISPID_GLOBAL_VBARRAY, NULL, BP_GET, VT_I2, VT_ARRAY},
2128 {DISPID_GLOBAL_VBTRUE, NULL, BP_GET, VT_I2, VARIANT_TRUE},
2129 {DISPID_GLOBAL_VBFALSE, NULL, BP_GET, VT_I2, VARIANT_FALSE},
2130 {DISPID_GLOBAL_VBUSEDEFAULT, NULL, BP_GET, VT_I2, -2},
2131 {DISPID_GLOBAL_VBBINARYCOMPARE, NULL, BP_GET, VT_I2, 0},
2132 {DISPID_GLOBAL_VBTEXTCOMPARE, NULL, BP_GET, VT_I2, 1},
2133 {DISPID_GLOBAL_VBDATABASECOMPARE, NULL, BP_GET, VT_I2, 2},
2134 {DISPID_GLOBAL_VBGENERALDATE, NULL, BP_GET, VT_I2, 0},
2135 {DISPID_GLOBAL_VBLONGDATE, NULL, BP_GET, VT_I2, 1},
2136 {DISPID_GLOBAL_VBSHORTDATE, NULL, BP_GET, VT_I2, 2},
2137 {DISPID_GLOBAL_VBLONGTIME, NULL, BP_GET, VT_I2, 3},
2138 {DISPID_GLOBAL_VBSHORTTIME, NULL, BP_GET, VT_I2, 4},
2139 {DISPID_GLOBAL_VBOBJECTERROR, NULL, BP_GET, VT_I4, 0x80040000},
2140 {DISPID_GLOBAL_VBBLACK, NULL, BP_GET, VT_I4, 0x000000},
2141 {DISPID_GLOBAL_VBBLUE, NULL, BP_GET, VT_I4, 0xff0000},
2142 {DISPID_GLOBAL_VBCYAN, NULL, BP_GET, VT_I4, 0xffff00},
2143 {DISPID_GLOBAL_VBGREEN, NULL, BP_GET, VT_I4, 0x00ff00},
2144 {DISPID_GLOBAL_VBMAGENTA, NULL, BP_GET, VT_I4, 0xff00ff},
2145 {DISPID_GLOBAL_VBRED, NULL, BP_GET, VT_I4, 0x0000ff},
2146 {DISPID_GLOBAL_VBWHITE, NULL, BP_GET, VT_I4, 0xffffff},
2147 {DISPID_GLOBAL_VBYELLOW, NULL, BP_GET, VT_I4, 0x00ffff},
2148 {DISPID_GLOBAL_VBCR, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCr},
2149 {DISPID_GLOBAL_VBCRLF, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCrLf},
2150 {DISPID_GLOBAL_VBNEWLINE, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNewLine},
2151 {DISPID_GLOBAL_VBFORMFEED, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbFormFeed},
2152 {DISPID_GLOBAL_VBLF, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbLf},
2153 {DISPID_GLOBAL_VBNULLCHAR, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullChar},
2154 {DISPID_GLOBAL_VBNULLSTRING, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullString},
2155 {DISPID_GLOBAL_VBTAB, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbTab},
2156 {DISPID_GLOBAL_VBVERTICALTAB, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbVerticalTab},
2157 {DISPID_GLOBAL_CCUR, Global_CCur, 0, 1},
2158 {DISPID_GLOBAL_CINT, Global_CInt, 0, 1},
2159 {DISPID_GLOBAL_CLNG, Global_CLng, 0, 1},
2160 {DISPID_GLOBAL_CBOOL, Global_CBool, 0, 1},
2161 {DISPID_GLOBAL_CBYTE, Global_CByte, 0, 1},
2162 {DISPID_GLOBAL_CDATE, Global_CDate, 0, 1},
2163 {DISPID_GLOBAL_CDBL, Global_CDbl, 0, 1},
2164 {DISPID_GLOBAL_CSNG, Global_CSng, 0, 1},
2165 {DISPID_GLOBAL_CSTR, Global_CStr, 0, 1},
2166 {DISPID_GLOBAL_HEX, Global_Hex, 0, 1},
2167 {DISPID_GLOBAL_OCT, Global_Oct, 0, 1},
2168 {DISPID_GLOBAL_VARTYPE, Global_VarType, 0, 1},
2169 {DISPID_GLOBAL_ISDATE, Global_IsDate, 0, 1},
2170 {DISPID_GLOBAL_ISEMPTY, Global_IsEmpty, 0, 1},
2171 {DISPID_GLOBAL_ISNULL, Global_IsNull, 0, 1},
2172 {DISPID_GLOBAL_ISNUMERIC, Global_IsNumeric, 0, 1},
2173 {DISPID_GLOBAL_ISARRAY, Global_IsArray, 0, 1},
2174 {DISPID_GLOBAL_ISOBJECT, Global_IsObject, 0, 1},
2175 {DISPID_GLOBAL_ATN, Global_Atn, 0, 1},
2176 {DISPID_GLOBAL_COS, Global_Cos, 0, 1},
2177 {DISPID_GLOBAL_SIN, Global_Sin, 0, 1},
2178 {DISPID_GLOBAL_TAN, Global_Tan, 0, 1},
2179 {DISPID_GLOBAL_EXP, Global_Exp, 0, 1},
2180 {DISPID_GLOBAL_LOG, Global_Log, 0, 1},
2181 {DISPID_GLOBAL_SQR, Global_Sqr, 0, 1},
2182 {DISPID_GLOBAL_RANDOMIZE, Global_Randomize, 0, 1},
2183 {DISPID_GLOBAL_RND, Global_Rnd, 0, 1},
2184 {DISPID_GLOBAL_TIMER, Global_Timer, 0, 0},
2185 {DISPID_GLOBAL_LBOUND, Global_LBound, 0, 1},
2186 {DISPID_GLOBAL_UBOUND, Global_UBound, 0, 1},
2187 {DISPID_GLOBAL_RGB, Global_RGB, 0, 3},
2188 {DISPID_GLOBAL_LEN, Global_Len, 0, 1},
2189 {DISPID_GLOBAL_LENB, Global_LenB, 0, 1},
2190 {DISPID_GLOBAL_LEFT, Global_Left, 0, 2},
2191 {DISPID_GLOBAL_LEFTB, Global_LeftB, 0, 2},
2192 {DISPID_GLOBAL_RIGHT, Global_Right, 0, 2},
2193 {DISPID_GLOBAL_RIGHTB, Global_RightB, 0, 2},
2194 {DISPID_GLOBAL_MID, Global_Mid, 0, 2, 3},
2195 {DISPID_GLOBAL_MIDB, Global_MidB, 0, 2, 3},
2196 {DISPID_GLOBAL_STRCOMP, Global_StrComp, 0, 2, 3},
2197 {DISPID_GLOBAL_LCASE, Global_LCase, 0, 1},
2198 {DISPID_GLOBAL_UCASE, Global_UCase, 0, 1},
2199 {DISPID_GLOBAL_LTRIM, Global_LTrim, 0, 1},
2200 {DISPID_GLOBAL_RTRIM, Global_RTrim, 0, 1},
2201 {DISPID_GLOBAL_TRIM, Global_Trim, 0, 1},
2202 {DISPID_GLOBAL_SPACE, Global_Space, 0, 1},
2203 {DISPID_GLOBAL_STRING, Global_String, 0, 0, 2},
2204 {DISPID_GLOBAL_INSTR, Global_InStr, 0, 2, 4},
2205 {DISPID_GLOBAL_INSTRB, Global_InStrB, 0, 3, 4},
2206 {DISPID_GLOBAL_ASCB, Global_AscB, 0, 1},
2207 {DISPID_GLOBAL_CHRB, Global_ChrB, 0, 1},
2208 {DISPID_GLOBAL_ASC, Global_Asc, 0, 1},
2209 {DISPID_GLOBAL_CHR, Global_Chr, 0, 1},
2210 {DISPID_GLOBAL_ASCW, Global_AscW, 0, 1},
2211 {DISPID_GLOBAL_CHRW, Global_ChrW, 0, 1},
2212 {DISPID_GLOBAL_ABS, Global_Abs, 0, 1},
2213 {DISPID_GLOBAL_FIX, Global_Fix, 0, 1},
2214 {DISPID_GLOBAL_INT, Global_Int, 0, 1},
2215 {DISPID_GLOBAL_SGN, Global_Sgn, 0, 1},
2216 {DISPID_GLOBAL_NOW, Global_Now, 0, 0},
2217 {DISPID_GLOBAL_DATE, Global_Date, 0, 0},
2218 {DISPID_GLOBAL_TIME, Global_Time, 0, 0},
2219 {DISPID_GLOBAL_DAY, Global_Day, 0, 1},
2220 {DISPID_GLOBAL_MONTH, Global_Month, 0, 1},
2221 {DISPID_GLOBAL_WEEKDAY, Global_Weekday, 0, 1, 2},
2222 {DISPID_GLOBAL_YEAR, Global_Year, 0, 1},
2223 {DISPID_GLOBAL_HOUR, Global_Hour, 0, 1},
2224 {DISPID_GLOBAL_MINUTE, Global_Minute, 0, 1},
2225 {DISPID_GLOBAL_SECOND, Global_Second, 0, 1},
2226 {DISPID_GLOBAL_DATEVALUE, Global_DateValue, 0, 1},
2227 {DISPID_GLOBAL_TIMEVALUE, Global_TimeValue, 0, 1},
2228 {DISPID_GLOBAL_DATESERIAL, Global_DateSerial, 0, 3},
2229 {DISPID_GLOBAL_TIMESERIAL, Global_TimeSerial, 0, 3},
2230 {DISPID_GLOBAL_INPUTBOX, Global_InputBox, 0, 1, 7},
2231 {DISPID_GLOBAL_MSGBOX, Global_MsgBox, 0, 1, 5},
2232 {DISPID_GLOBAL_CREATEOBJECT, Global_CreateObject, 0, 1},
2233 {DISPID_GLOBAL_GETOBJECT, Global_GetObject, 0, 0, 2},
2234 {DISPID_GLOBAL_DATEADD, Global_DateAdd, 0, 3},
2235 {DISPID_GLOBAL_DATEDIFF, Global_DateDiff, 0, 3, 5},
2236 {DISPID_GLOBAL_DATEPART, Global_DatePart, 0, 2, 4},
2237 {DISPID_GLOBAL_TYPENAME, Global_TypeName, 0, 1},
2238 {DISPID_GLOBAL_ARRAY, Global_Array, 0, 1},
2239 {DISPID_GLOBAL_ERASE, Global_Erase, 0, 1},
2240 {DISPID_GLOBAL_FILTER, Global_Filter, 0, 2, 4},
2241 {DISPID_GLOBAL_JOIN, Global_Join, 0, 1, 2},
2242 {DISPID_GLOBAL_SPLIT, Global_Split, 0, 1, 4},
2243 {DISPID_GLOBAL_REPLACE, Global_Replace, 0, 3, 6},
2244 {DISPID_GLOBAL_STRREVERSE, Global_StrReverse, 0, 1},
2245 {DISPID_GLOBAL_INSTRREV, Global_InStrRev, 0, 2, 4},
2246 {DISPID_GLOBAL_LOADPICTURE, Global_LoadPicture, 0, 1},
2247 {DISPID_GLOBAL_SCRIPTENGINE, Global_ScriptEngine, 0, 0},
2248 {DISPID_GLOBAL_SCRIPTENGINEMAJORVERSION, Global_ScriptEngineMajorVersion, 0, 0},
2249 {DISPID_GLOBAL_SCRIPTENGINEMINORVERSION, Global_ScriptEngineMinorVersion, 0, 0},
2250 {DISPID_GLOBAL_SCRIPTENGINEBUILDVERSION, Global_ScriptEngineBuildVersion, 0, 0},
2251 {DISPID_GLOBAL_FORMATNUMBER, Global_FormatNumber, 0, 1, 5},
2252 {DISPID_GLOBAL_FORMATCURRENCY, Global_FormatCurrency, 0, 1, 5},
2253 {DISPID_GLOBAL_FORMATPERCENT, Global_FormatPercent, 0, 1, 5},
2254 {DISPID_GLOBAL_FORMATDATETIME, Global_FormatDateTime, 0, 1, 2},
2255 {DISPID_GLOBAL_WEEKDAYNAME, Global_WeekdayName, 0, 1, 3},
2256 {DISPID_GLOBAL_MONTHNAME, Global_MonthName, 0, 1, 2},
2257 {DISPID_GLOBAL_ROUND, Global_Round, 0, 1, 2},
2258 {DISPID_GLOBAL_ESCAPE, Global_Escape, 0, 1},
2259 {DISPID_GLOBAL_UNESCAPE, Global_Unescape, 0, 1},
2260 {DISPID_GLOBAL_EVAL, Global_Eval, 0, 1},
2261 {DISPID_GLOBAL_EXECUTE, Global_Execute, 0, 1},
2262 {DISPID_GLOBAL_EXECUTEGLOBAL, Global_ExecuteGlobal, 0, 1},
2263 {DISPID_GLOBAL_GETREF, Global_GetRef, 0, 1},
2264 {DISPID_GLOBAL_VBMSGBOXHELPBUTTON, NULL, BP_GET, VT_I4, MB_HELP},
2265 {DISPID_GLOBAL_VBMSGBOXSETFOREGROUND, NULL, BP_GET, VT_I4, MB_SETFOREGROUND},
2266 {DISPID_GLOBAL_VBMSGBOXRIGHT, NULL, BP_GET, VT_I4, MB_RIGHT},
2267 {DISPID_GLOBAL_VBMSGBOXRTLREADING, NULL, BP_GET, VT_I4, MB_RTLREADING}
2270 static HRESULT Err_Description(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2272 FIXME("\n");
2273 return E_NOTIMPL;
2276 static HRESULT Err_HelpContext(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2278 FIXME("\n");
2279 return E_NOTIMPL;
2282 static HRESULT Err_HelpFile(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2284 FIXME("\n");
2285 return E_NOTIMPL;
2288 static HRESULT Err_Number(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2290 HRESULT hres;
2292 TRACE("\n");
2294 if(!This->desc)
2295 return E_UNEXPECTED;
2297 if(args_cnt) {
2298 FIXME("setter not implemented\n");
2299 return E_NOTIMPL;
2302 hres = This->desc->ctx->err_number;
2303 return return_int(res, HRESULT_FACILITY(hres) == FACILITY_VBS ? HRESULT_CODE(hres) : hres);
2306 static HRESULT Err_Source(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2308 FIXME("\n");
2309 return E_NOTIMPL;
2312 static HRESULT Err_Clear(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2314 TRACE("\n");
2316 if(!This->desc)
2317 return E_UNEXPECTED;
2319 This->desc->ctx->err_number = S_OK;
2320 return S_OK;
2323 static HRESULT Err_Raise(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2325 FIXME("\n");
2326 return E_NOTIMPL;
2329 static const builtin_prop_t err_props[] = {
2330 {DISPID_ERR_DESCRIPTION, Err_Description, BP_GETPUT},
2331 {DISPID_ERR_HELPCONTEXT, Err_HelpContext, BP_GETPUT},
2332 {DISPID_ERR_HELPFILE, Err_HelpFile, BP_GETPUT},
2333 {DISPID_ERR_NUMBER, Err_Number, BP_GETPUT},
2334 {DISPID_ERR_SOURCE, Err_Source, BP_GETPUT},
2335 {DISPID_ERR_CLEAR, Err_Clear},
2336 {DISPID_ERR_RAISE, Err_Raise, 0, 5},
2339 HRESULT init_global(script_ctx_t *ctx)
2341 HRESULT hres;
2343 ctx->global_desc.ctx = ctx;
2344 ctx->global_desc.builtin_prop_cnt = sizeof(global_props)/sizeof(*global_props);
2345 ctx->global_desc.builtin_props = global_props;
2347 hres = get_typeinfo(GlobalObj_tid, &ctx->global_desc.typeinfo);
2348 if(FAILED(hres))
2349 return hres;
2351 hres = create_vbdisp(&ctx->global_desc, &ctx->global_obj);
2352 if(FAILED(hres))
2353 return hres;
2355 hres = create_script_disp(ctx, &ctx->script_obj);
2356 if(FAILED(hres))
2357 return hres;
2359 ctx->err_desc.ctx = ctx;
2360 ctx->err_desc.builtin_prop_cnt = sizeof(err_props)/sizeof(*err_props);
2361 ctx->err_desc.builtin_props = err_props;
2363 hres = get_typeinfo(ErrObj_tid, &ctx->err_desc.typeinfo);
2364 if(FAILED(hres))
2365 return hres;
2367 return create_vbdisp(&ctx->err_desc, &ctx->err_obj);