-
Notifications
You must be signed in to change notification settings - Fork 1
/
win32oleex.cpp
1255 lines (1161 loc) · 36.3 KB
/
win32oleex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright(c) 2000, 2006 arton
*
* $Date: 2006-12-03 04:56:14 +0900 (日, 03 12 2006) $
*/
/*
* (c) 1995 Microsoft Corporation. All rights reserved.
* Developed by ActiveWare Internet Corp., http://www.ActiveWare.com
*
* Other modifications Copyright (c) 1997, 1998 by Gurusamy Sarathy
* <[email protected]> and Jan Dubois <[email protected]>
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file
* of the Perl distribution.
*
*/
/*
$Date: 2006-12-03 04:56:14 +0900 (日, 03 12 2006) $
modified for win32ole (ruby) by Masaki.Suketa <[email protected]>
*/
#include "stdafx.h"
#ifdef _MERGE_PROXYSTUB
#include "RScript.h"
#else
#include "GRScript.h"
#endif
#include "RScriptCore.h"
#include "win32ole.h"
#include "Initializer.h"
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
#include "RubyWrapper.h"
#endif
#include "RubyObject.h"
/////////////////////////////////////////////////////////////////////////////
// CRubyScript
VALUE CRScriptCore::s_valueWin32Ole;
VALUE CRScriptCore::s_valueWin32OleEx;
VALUE CRScriptCore::s_valueWIN32OLERuntimeError;
VALUE CRScriptCore::s_valueActiveRubyScript;
bool CRScriptCore::s_fTrace(false);
bool CRScriptCore::s_fRubyize(false);
// Helper Functions from Win32Ole
/*
$Date: 2006-12-03 04:56:14 +0900 (日, 03 12 2006) $
modified for win32ole (ruby) by M.Suketa <[email protected]>
*/
// modified for ActiveScript (ruby) by arton
//
VALUE __cdecl CRScriptCore::rubyize(VALUE, VALUE v)
{
bool f;
switch (v)
{
case Qtrue:
f = true;
break;
case Qfalse:
f = false;
break;
default:
rb_raise(rb_eTypeError, "not valid value");
break;
}
#ifdef __IRubyEngine_INTERFACE_DEFINED__
IRubyEngine* pEngine = CRubyWrapper::GetCWrapper()->GetCurrentEngine();
if (!pEngine) return Qnil;
pEngine->SetRubyize(f);
#else
s_fRubyize = f;
#endif
return v;
}
void __cdecl CRScriptCore::ole_free(oledata* pole)
{
ATLTRACE(_("Freeing Win32OleEx Object %08x\n"), pole);
if(pole->pDispatch) {
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
CRubyWrapper::GetCWrapper()->ReleaseOle(pole);
// never call Release, because not explicit addrefed
#else
pole->pDispatch->Release();
pole->pDispatch = NULL;
#endif
}
}
VALUE CRScriptCore::ole_ary_m_entry(VALUE val, long* pid)
{
VALUE obj = Qnil;
int i = 0;
obj = val;
while(TYPE(obj) == T_ARRAY) {
obj = rb_ary_entry(obj, pid[i]);
i++;
}
return obj;
}
void CRScriptCore::ole_set_safe_array(long n, SAFEARRAY* psa, long* pid, long* pub, VALUE val, long dim, IRubyEngine* pEngine)
{
if(n < 0) return;
VALUE val1;
VARIANT var;
VariantInit(&var);
if(n == dim) {
val1 = ole_ary_m_entry(val, pid);
ole_val2variant(val1, &var, pEngine);
SafeArrayPutElement(psa, pid, &var);
}
pid[n] += 1;
if (pid[n] < pub[n]) {
ole_set_safe_array(dim, psa, pid, pub, val, dim, pEngine);
}
else {
pid[n] = 0;
ole_set_safe_array(n-1, psa, pid, pub, val, dim, pEngine);
}
}
void CRScriptCore::ole_val2variant(VALUE val, VARIANT* var, IRubyEngine* pEngine, VARTYPE nil_to)
{
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
if (pEngine && pEngine->GetRubyize() == S_OK)
{
pEngine->SetRubyize(false);
V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = CRubyObject::CreateRubyObject(pEngine, val);
return;
}
#else
if (s_fRubyize)
{
s_fRubyize = false;
V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = CRubyObject::CreateRubyObject(pEngine, val);
return;
}
#endif
USES_CONVERSION;
oledata *pole;
if(rb_obj_is_kind_of(val, s_valueWin32Ole) || rb_obj_is_kind_of(val, s_valueWin32OleEx)) {
Data_Get_Struct(val, struct oledata, pole);
pole->pDispatch->AddRef();
V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = pole->pDispatch;
return;
}
if (rb_obj_is_kind_of(val, rb_cTime)) {
SYSTEMTIME st;
ZeroMemory(&st, sizeof(SYSTEMTIME));
st.wYear = (WORD)FIX2INT(rb_funcall(val, rb_intern("year"), 0));
st.wMonth = (WORD)FIX2INT(rb_funcall(val, rb_intern("month"), 0));
st.wDay = (WORD)FIX2INT(rb_funcall(val, rb_intern("mday"), 0));
st.wHour = (WORD)FIX2INT(rb_funcall(val, rb_intern("hour"), 0));
st.wMinute = (WORD)FIX2INT(rb_funcall(val, rb_intern("min"), 0));
st.wSecond = (WORD)FIX2INT(rb_funcall(val, rb_intern("sec"), 0));
V_VT(var) = VT_DATE;
SystemTimeToVariantTime(&st, &var->dblVal);
return;
}
ATLTRACE(_T("Type Of val = %08X\n"), TYPE(val));
switch (TYPE(val)) {
case T_ARRAY:
{
long dim(0);
int i(0);
VALUE val1 = val;
while(TYPE(val1) == T_ARRAY) {
val1 = rb_ary_entry(val1, 0);
dim += 1;
}
SAFEARRAYBOUND* psab = reinterpret_cast<SAFEARRAYBOUND*>(_alloca(dim * sizeof(SAFEARRAYBOUND)));
long* pub = reinterpret_cast<long*>(_alloca(dim * sizeof(long)));
long* pid = reinterpret_cast<long*>(_alloca(dim * sizeof(long)));
val1 = val;
i = 0;
while(TYPE(val1) == T_ARRAY) {
psab[i].cElements = RARRAY_LEN(val1);
psab[i].lLbound = 0;
pub[i] = psab[i].cElements;
pid[i] = 0;
i ++;
val1 = rb_ary_entry(val1, 0);
}
/* Create and fill VARIANT array */
SAFEARRAY* psa = SafeArrayCreate(VT_VARIANT, dim, psab);
if (psa == NULL)
{
V_VT(var) = VT_ERROR;
V_ERROR(var) = E_OUTOFMEMORY;
}
else
{
SafeArrayLock(psa);
ole_set_safe_array(dim-1, psa, pid, pub, val, dim-1, pEngine);
SafeArrayUnlock(psa);
V_VT(var) = VT_VARIANT | VT_ARRAY;
V_ARRAY(var) = psa;
}
break;
}
case T_STRING:
V_VT(var) = VT_BSTR;
V_BSTR(var) = SysAllocString(A2W(StringValuePtr(val)));
break;
case T_FIXNUM:
V_VT(var) = VT_I4;
V_I4(var) = NUM2INT(val);
break;
case T_BIGNUM:
V_VT(var) = VT_R8;
V_R8(var) = rb_big2dbl(val);
break;
case T_FLOAT:
V_VT(var) = VT_R8;
V_R8(var) = NUM2DBL(val);
break;
case T_TRUE:
V_VT(var) = VT_BOOL;
V_BOOL(var) = VARIANT_TRUE;
break;
case T_FALSE:
V_VT(var) = VT_BOOL;
V_BOOL(var) = VARIANT_FALSE;
break;
case T_NIL:
if (nil_to == VT_ERROR)
{
V_VT(var) = VT_ERROR;
V_ERROR(var) = DISP_E_PARAMNOTFOUND;
}
else
{
V_VT(var) = VT_EMPTY;
}
break;
default:
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
if (pEngine)
{
V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = CRubyObject::CreateRubyObject(pEngine, val);
}
else
{
ATLTRACE(_T("Without pEngine, ole_val2variant was called\n"));
VariantInit(var); // VT_EMPTY
}
#else
V_VT(var) = VT_DISPATCH;
V_DISPATCH(var) = CRubyObject::CreateRubyObject(pEngine, val);
#endif
break;
}
}
VALUE CRScriptCore::ole_variant2val(VARIANT* pvar, IRubyEngine* pEngine, IServiceProvider* pProv)
{
USES_CONVERSION;
VALUE obj = Qnil;
HRESULT hr;
while ( V_VT(pvar) == (VT_BYREF | VT_VARIANT) )
pvar = V_VARIANTREF(pvar);
if(V_ISARRAY(pvar)) {
SAFEARRAY *psa = V_ISBYREF(pvar) ? *V_ARRAYREF(pvar) : V_ARRAY(pvar);
long i;
long *pID, *pLB, *pUB;
VARIANT variant;
VALUE val;
VALUE val2;
int dim = SafeArrayGetDim(psa);
VariantInit(&variant);
V_VT(&variant) = (V_VT(pvar) & ~VT_ARRAY) | VT_BYREF;
pID = reinterpret_cast<long*>(_alloca(dim * sizeof(long)));
pLB = reinterpret_cast<long*>(_alloca(dim * sizeof(long)));
pUB = reinterpret_cast<long*>(_alloca(dim * sizeof(long)));
obj = Qnil;
for(i = 0; i < dim; ++i) {
SafeArrayGetLBound(psa, i+1, &pLB[i]);
SafeArrayGetLBound(psa, i+1, &pID[i]);
SafeArrayGetUBound(psa, i+1, &pUB[i]);
}
hr = SafeArrayLock(psa);
if (SUCCEEDED(hr)) {
val2 = rb_ary_new();
while (i >= 0) {
hr = SafeArrayPtrOfIndex(psa, pID, &V_BYREF(&variant));
if (FAILED(hr))
break;
val = ole_variant2val(&variant, pEngine);
rb_ary_push(val2, val);
for (i = dim-1 ; i >= 0 ; --i) {
if (++pID[i] <= pUB[i])
break;
pID[i] = pLB[i];
if (i > 0) {
if (obj == Qnil)
obj = rb_ary_new();
rb_ary_push(obj, val2);
val2 = rb_ary_new();
}
}
}
SafeArrayUnlock(psa);
}
return (obj == Qnil) ? val2 : obj;
}
switch(V_VT(pvar) & ~VT_BYREF){
case VT_EMPTY:
break;
case VT_NULL:
break;
case VT_UI1:
if(V_ISBYREF(pvar))
obj = INT2NUM((long)*V_UI1REF(pvar));
else
obj = INT2NUM((long)V_UI1(pvar));
break;
case VT_I2:
if(V_ISBYREF(pvar))
obj = INT2NUM((long)*V_I2REF(pvar));
else
obj = INT2NUM((long)V_I2(pvar));
break;
case VT_I4:
if(V_ISBYREF(pvar))
obj = INT2NUM((long)*V_I4REF(pvar));
else
obj = INT2NUM((long)V_I4(pvar));
break;
case VT_R4:
if(V_ISBYREF(pvar))
obj = rb_float_new(*V_R4REF(pvar));
else
obj = rb_float_new(V_R4(pvar));
break;
case VT_R8:
if(V_ISBYREF(pvar))
obj = rb_float_new(*V_R8REF(pvar));
else
obj = rb_float_new(V_R8(pvar));
break;
case VT_BSTR:
{
char *p;
LPOLESTR pw;
if(V_ISBYREF(pvar))
pw = *V_BSTRREF(pvar);
else
pw = V_BSTR(pvar);
if (!pw) p = "";
else p = W2A(pw);
obj = rb_enc_str_new(p, strlen(p), s_pEncoding);
break;
}
case VT_ERROR:
if(V_ISBYREF(pvar))
obj = INT2NUM(*V_ERRORREF(pvar));
else
obj = INT2NUM(V_ERROR(pvar));
break;
case VT_BOOL:
if (V_ISBYREF(pvar))
obj = (*V_BOOLREF(pvar) ? Qtrue : Qfalse);
else
obj = (V_BOOL(pvar) ? Qtrue : Qfalse);
break;
case VT_DISPATCH:
{
IDispatch *pDispatch;
if (V_ISBYREF(pvar))
pDispatch = *V_DISPATCHREF(pvar);
else
pDispatch = V_DISPATCH(pvar);
obj = ole_createWin32OleEx(pDispatch, pEngine, pProv);
break;
}
case VT_UNKNOWN:
{
/* get IDispatch interface from IUnknown interface */
IUnknown *punk;
if (V_ISBYREF(pvar))
punk = *V_UNKNOWNREF(pvar);
else
punk = V_UNKNOWN(pvar);
obj = ole_createWin32OleEx(punk, pEngine, pProv);
break;
}
case VT_DATE:
{
DATE date;
if(V_ISBYREF(pvar))
date = *V_DATEREF(pvar);
else
date = V_DATE(pvar);
SYSTEMTIME st;
VariantTimeToSystemTime(date, &st);
char szTime[20];
int cb = wsprintfA(szTime,
"%4.4d/%02.2d/%02.2d %02.2d:%02.2d:%02.2d",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
obj = rb_str_new(szTime, cb);
break;
}
case VT_CY:
default:
{
VARIANT variant;
VariantInit(&variant);
HRESULT hr = VariantChangeTypeEx(&variant, pvar,
LOCALE_SYSTEM_DEFAULT, 0, VT_BSTR);
if (hr == S_OK && V_VT(&variant) == VT_BSTR) {
char *p = W2A(V_BSTR(&variant));
obj = rb_str_new2(p);
}
VariantClear(&variant);
break;
}
}
return obj;
}
VALUE CRScriptCore::ole_createWin32OleEx(IUnknown* pUnk, IRubyEngine* pEngine, IServiceProvider* pProv)
{
VALUE obj = Qnil;
if (!pUnk) return obj;
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
_ASSERT(pEngine);
VALUE module;
pEngine->GetModule((DWORD*)&module);
CRubyWrapper* pWrapper = CRubyWrapper::GetCWrapper();
IDispatchEx* pDispEx;
if (pProv && pUnk->QueryInterface(IID_IDispatchEx, (void**)&pDispEx) == S_OK)
{
oledataex* polex;
obj = Data_Make_Struct(s_valueWin32OleEx, struct oledataex,
0, (void(*)(void))ole_free, polex);
polex->pDispatchEx = pDispEx;
polex->pServiceProvider = pProv;
polex->pEngine = pEngine;
pWrapper->AddOle(module, polex);
return obj;
}
#endif
IDispatch *pDispatch;
if (pUnk->QueryInterface(IID_IDispatch, (void **)&pDispatch) == S_OK)
{
struct oledataex *pole;
obj = Data_Make_Struct(s_valueWin32OleEx, struct oledataex,
0, (void(*)(void))ole_free,pole);
pole->pDispatch = pDispatch;
pole->pEngine = pEngine;
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
pWrapper->AddOle(module, pole);
#endif
}
return obj;
}
VALUE CRScriptCore::ole_hresult2msg(HRESULT hr)
{
VALUE msg = Qnil;
char *p_msg;
DWORD dwCount;
dwCount = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, hr, LOCALE_SYSTEM_DEFAULT,
(LPTSTR)&p_msg, 0, NULL);
if (dwCount > 0) {
/* remove dots and CRs/LFs */
while (dwCount > 0 &&
(p_msg[dwCount-1] < ' ' || p_msg[dwCount-1] == '.')) {
p_msg[--dwCount] = '\0';
}
if (p_msg[0] != '\0') {
msg = rb_str_new2(p_msg);
}
}
return msg;
}
VALUE CRScriptCore::ole_excepinfo2msg(EXCEPINFO* pExInfo)
{
USES_CONVERSION;
char error_code[40];
char *pSource = NULL;
char *pDescription = NULL;
VALUE error_msg;
if(pExInfo->pfnDeferredFillIn != NULL) {
(*pExInfo->pfnDeferredFillIn)(pExInfo);
}
if (pExInfo->bstrSource != NULL) {
pSource = W2A(pExInfo->bstrSource);
SysFreeString(pExInfo->bstrSource);
}
if (pExInfo->bstrDescription != NULL) {
pDescription = W2A(pExInfo->bstrDescription);
SysFreeString(pExInfo->bstrDescription);
}
if(pExInfo->wCode == 0) {
sprintf(error_code, "\n OLE rb_compile_error:%lX in ", pExInfo->scode);
}
else{
sprintf(error_code, "\n OLE rb_compile_error:%u in ", pExInfo->wCode);
}
error_msg = rb_str_new2(error_code);
if(pSource != NULL) {
rb_str_cat(error_msg, pSource, (long)strlen(pSource));
}
else {
rb_str_cat(error_msg, "<Unknown>", 9);
}
rb_str_cat(error_msg, "\n ", 5);
if(pDescription != NULL) {
rb_str_cat(error_msg, pDescription, (long)strlen(pDescription));
}
else {
rb_str_cat(error_msg, "<No Description>", 16);
}
if (pExInfo->bstrHelpFile != NULL) {
SysFreeString(pExInfo->bstrHelpFile);
}
return error_msg;
}
void CRScriptCore::ole_raise(HRESULT hr, VALUE ecs, LPCSTR fmt, ...)
{
va_list args;
char buf[512];
va_start(args, fmt);
vsnprintf(buf, sizeof(buf)/sizeof(char), fmt, args);
va_end(args);
VALUE err_msg = ole_hresult2msg(hr);
if(err_msg != Qnil) {
rb_raise(ecs, "%s\n%s", buf, StringValuePtr(err_msg));
}
else {
rb_raise(ecs, "%s", buf);
}
}
//
// Check Newly Create Object is safe or not.
//
VALUE __cdecl CRScriptCore::fole_s_new(int argc, VALUE* argv, VALUE self)
{
VALUE err_msg, svr_name, host_name;
CLSID clsid;
IDispatch *pDispatch = NULL;
IDispatchEx *pDispatchEx = NULL;
VALUE obj;
rb_scan_args(argc, argv, "11", &svr_name, &host_name);
USES_CONVERSION;
/* get CLSID from OLE server name */
LPOLESTR pBuf = A2W(StringValuePtr(svr_name));
HRESULT hr = CLSIDFromProgID(pBuf, &clsid);
if(hr != S_OK)
{
err_msg = rb_str_new2("Unknown OLE server : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
CRubyWrapper* pWrapper = CRubyWrapper::GetCWrapper();
IRubyEngine* pEngine = pWrapper->GetCurrentEngine();
if (pEngine)
{
HRESULT hr = pEngine->CheckActiveXIsSafety(clsid);
if (hr != S_OK)
{
err_msg = rb_str_new2("Running ActiveX is disallowed : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
}
#endif
/* get IDispatch interface */
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
if (pEngine)
{
IUnknown* pUnk = NULL;
LPWSTR psz;
if (host_name == Qnil)
psz = NULL;
else
psz = A2W(StringValuePtr(host_name));
hr = pEngine->CreateObject(clsid, psz, IID_IUnknown, &pUnk);
if (hr == S_OK)
{
hr = pUnk->QueryInterface(IID_IDispatchEx, (void**)&pDispatchEx);
if (hr != S_OK)
{
pDispatchEx = NULL;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDispatch);
}
pUnk->Release();
}
}
else
{
#endif
MULTI_QI mqi;
mqi.pIID = &IID_IDispatch;
mqi.pItf = NULL;
COSERVERINFO svrInfo;
if (host_name != Qnil)
{
memset(&svrInfo, 0, sizeof(COSERVERINFO));
svrInfo.pwszName = A2W(StringValuePtr(host_name));
hr = CoCreateInstanceEx(clsid, NULL, CLSCTX_REMOTE_SERVER, &svrInfo, 1, &mqi);
}
else
{
hr = CoCreateInstanceEx(clsid, NULL, CLSCTX_SERVER, NULL, 1, &mqi);
}
if (hr == S_OK)
{
hr = mqi.hr;
pDispatch = reinterpret_cast<IDispatch*>(mqi.pItf);
}
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
}
#endif
if(FAILED(hr) || (!pDispatch && !pDispatchEx))
{
err_msg = rb_str_new2("Fail to create WIN32OLE object from ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
/* create WIN32OLE or WIN32OLEEX object */
oledataex *pole;
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
obj = Data_Make_Struct(self, oledataex, 0, (void(*)(void))ole_free, pole);
if (pDispatchEx)
{
pole->pDispatchEx = pDispatchEx;
}
else
{
pole->pDispatch = pDispatch;
}
if (pEngine)
{
VALUE module;
pEngine->GetModule((DWORD*)&module);
pWrapper->AddOle(module, pole);
pole->pEngine = pEngine;
if (pDispatchEx)
{
IServiceProvider* p;
if (pEngine->QueryInterface(IID_IServiceProvider, (void**)&p) == S_OK)
{
pole->pServiceProvider = p;
// Release because the extent of this object is same as the engine.
p->Release();
}
}
}
#else
obj = Data_Make_Struct(s_valueWin32OleEx, struct oledataex,0,(void(*)(void))ole_free,pole);
pole->pDispatch = pDispatch;
#endif
return obj;
}
VALUE __cdecl CRScriptCore::fole_s_connect(VALUE self, VALUE svr_name)
{
if (rb_safe_level() >= 3)
{
rb_raise(rb_eSecurityError, "Connecting ActiveX is disallowed.");
}
CLSID clsid;
IDispatch *pDispatch;
LPWSTR pDispName = NULL;
IUnknown *pUnknown;
VALUE obj;
VALUE err_msg;
HRESULT hr;
USES_CONVERSION;
// Check New moniker
if (strchr(StringValuePtr(svr_name), ':') > 0
&& strrchr(StringValuePtr(svr_name), ':') > 0)
{
pDispName = A2W(StringValuePtr(svr_name));
}
else
{
/* get CLSID from OLE server name */
LPOLESTR pBuf = A2W(StringValuePtr(svr_name));
hr = CLSIDFromProgID(pBuf, &clsid);
if(hr != S_OK)
{
if (hr != S_OK)
{
err_msg = rb_str_new2("Unknown OLE server : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
}
}
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
IRubyEngine* pEngine = CRubyWrapper::GetCWrapper()->GetCurrentEngine();
if (pEngine)
{
hr = pEngine->CheckActiveXIsSafety(clsid);
if (hr != S_OK)
{
err_msg = rb_str_new2("Running ActiveX is disallowed : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
}
#endif
if (pDispName)
hr = CoGetObject(pDispName, NULL, IID_IUnknown, (void**)&pUnknown);
else
hr = GetActiveObject(clsid, 0, &pUnknown);
if (FAILED(hr)) {
err_msg = rb_str_new2("Not Running OLE server : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
if (pEngine)
{
hr = pEngine->CheckInterfaceIsSafety(clsid, pUnknown);
if (hr != S_OK)
{
pUnknown->Release();
err_msg = rb_str_new2("Fail to connect WIN32OLE server : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
}
#endif
hr = pUnknown->QueryInterface(IID_IDispatch, (void **)&pDispatch);
if(FAILED(hr)) {
pUnknown->Release();
err_msg = rb_str_new2("Fail to create WIN32OLE server : ");
rb_str_concat(err_msg, svr_name);
ole_raise(hr, s_valueWIN32OLERuntimeError, StringValuePtr(err_msg));
}
pUnknown->Release();
/* create WIN32OLE object */
struct oledataex *pole;
obj = Data_Make_Struct(s_valueWin32OleEx,struct oledataex,0,(void(*)(void))ole_free,pole);
pole->pDispatch = pDispatch;
#ifdef __IRubyWrapper_INTERFACE_DEFINED__
pole->pEngine = pEngine;
if (pEngine)
{
VALUE module;
pEngine->GetModule((DWORD*)&module);
CRubyWrapper::GetCWrapper()->AddOle(module, pole);
}
#endif
return obj;
}
//
// Attach is always allowed because the objects are given by IE itself.
//
VALUE __cdecl CRScriptCore::foleex_attach(VALUE self, VALUE ItemName, VALUE Cookie)
{
ATLTRACE(_T("attach %s - %d\n"), StringValuePtr(ItemName), FIX2LONG(Cookie));
CRScriptCore* p = CRScriptCore::GetEngine(FIX2LONG(Cookie));
if (!p) return Qnil;
return p->GetOleObject(self, StringValuePtr(ItemName));
}
VALUE CRScriptCore::fole_propertyput(VALUE self, VALUE property, VALUE value)
{
oledataex *pole;
unsigned int index;
HRESULT hr;
EXCEPINFO excepinfo;
DISPID dispID = DISPID_VALUE;
DISPID dispIDParam = DISPID_PROPERTYPUT;
USHORT wFlags = DISPATCH_PROPERTYPUT;
DISPPARAMS dispParams;
VARIANTARG propertyValue[2];
LCID lcid = LOCALE_SYSTEM_DEFAULT;
dispParams.rgdispidNamedArgs = &dispIDParam;
dispParams.rgvarg = propertyValue;
dispParams.cNamedArgs = 1;
dispParams.cArgs = 1;
VariantInit(&propertyValue[0]);
VariantInit(&propertyValue[1]);
memset(&excepinfo, 0, sizeof(excepinfo));
Data_Get_Struct(self, struct oledataex, pole);
/* get ID from property name */
USES_CONVERSION;
if (pole->pServiceProvider)
{
BSTR bstr = SysAllocString(A2W(StringValuePtr(property)));
hr = pole->pDispatchEx->GetDispID(bstr, fdexNameCaseSensitive, &dispID);
ATLTRACE(_T("win32ole propput call %ls, id=%x, result=%x\n"), bstr, dispID, hr);
SysFreeString(bstr);
}
else
{
LPOLESTR pw = A2W(StringValuePtr(property));
hr = pole->pDispatch->GetIDsOfNames(IID_NULL, &pw, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
}
if (hr != S_OK)
{
dispID = DISPID_VALUE;
dispParams.cArgs = 2;
V_VT(&propertyValue[1]) = VT_BSTR;
V_BSTR(&propertyValue[1]) = SysAllocString(A2W(StringValuePtr(property)));
}
/* set property value */
VARIANT vResult;
VariantInit(&vResult);
ole_val2variant(value, &propertyValue[0], pole->pEngine);
if (pole->pServiceProvider)
{
hr = pole->pDispatchEx->InvokeEx(dispID, LOCALE_SYSTEM_DEFAULT,
wFlags, &dispParams, &vResult, &excepinfo, pole->pServiceProvider);
if (hr == S_FALSE)
{
hr = pole->pDispatchEx->InvokeEx(dispID, LOCALE_SYSTEM_DEFAULT,
wFlags, &dispParams, &vResult, &excepinfo, NULL);
}
}
else
{
UINT ul(0);
hr = pole->pDispatch->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
wFlags, &dispParams, &vResult, &excepinfo, &ul);
}
VariantClear(&vResult);
for(index = 0; index < dispParams.cArgs; ++index)
VariantClear(&propertyValue[index]);
return Qnil;
}
/**
* helper functions from Win32ole
*/
VALUE CRScriptCore::hash2named_arg(VALUE pair, oleparam* pOp)
{
USES_CONVERSION;
unsigned int index, i;
VALUE key, value;
const char* name;
index = pOp->dp.cNamedArgs;
/*-------------------------------------
the data-type of key must be String
---------------------------------------*/
key = rb_ary_entry(pair, 0);
if (SYMBOL_P(key))
{
name = rb_id2name(SYM2ID(key));
}
else if (TYPE(key) == T_STRING)
{
name = StringValuePtr(key);
}
else
{
/* clear name of dispatch parameters */
for(i = 1; i < index + 1; i++) {
SysFreeString(pOp->pNamedArgs[i]);
}
/* clear dispatch parameters */
for(i = 0; i < index; i++ ) {
VariantClear(&(pOp->dp.rgvarg[i]));
}
/* raise an exception */
Check_Type(key, T_STRING);
}
/* pNamedArgs[0] is <method name>, so "index + 1" */
pOp->pNamedArgs[index + 1] = SysAllocString(A2W(name));
value = rb_ary_entry(pair, 1);
VariantInit(&(pOp->dp.rgvarg[index]));
ole_val2variant(value, &(pOp->dp.rgvarg[index]), pOp->pole->pEngine);
pOp->dp.cNamedArgs += 1;
return Qnil;
}
VALUE CRScriptCore::set_argv(VARIANTARG* realargs, unsigned int beg, unsigned int end, struct oledataex* pole, IServiceProvider* pProv)
{
VALUE argv = rb_const_get(s_valueWin32Ole, rb_intern("ARGV"));
Check_Type(argv, T_ARRAY);
rb_ary_clear(argv);
while (end-- > beg) {
rb_ary_push(argv, ole_variant2val(&realargs[end], pole->pEngine, pProv));
VariantClear(&realargs[end]);
}
return argv;
}
static HRESULT invoke(struct oledataex* pole, DISPID DispID, LCID lcid, WORD wFlags,
VARIANT* result, EXCEPINFO& excepinfo, unsigned int& argErr, oleparam& op)
{
HRESULT hr;
if (pole->pServiceProvider)
{
hr = pole->pDispatchEx->InvokeEx(DispID, lcid,
wFlags, &op.dp, result, &excepinfo, pole->pServiceProvider);
if (hr == S_FALSE)
{
hr = pole->pDispatchEx->InvokeEx(DispID, lcid,
wFlags, &op.dp, result, &excepinfo, NULL);
}
else if (hr == S_OK)
{
op.pProv = pole->pServiceProvider;
}
}
else
{
hr = pole->pDispatch->Invoke(DispID, IID_NULL, lcid, wFlags,
&op.dp, result, &excepinfo, &argErr);
}
return hr;
}
VALUE CRScriptCore::ole_invoke(int argc, VALUE* argv, VALUE self, WORD wFlags)
{
ATLTRACE(_T("ole_invoke in thread:%08X\n"), GetCurrentThreadId());
LCID lcid = LOCALE_SYSTEM_DEFAULT;
struct oledataex *pole;
HRESULT hr;
VALUE cmd;
VALUE paramS;
VALUE param;
VALUE obj;
VALUE error_msg;
DISPID DispID;
DISPID* pDispID;
EXCEPINFO excepinfo;
VARIANTARG* prgvarg = NULL;
VARIANT result;
VARIANTARG* realargs = NULL;
unsigned int argErr = 0;
unsigned int cNamedArgs;
struct oleparam op;
memset(&excepinfo, 0, sizeof(EXCEPINFO));
VariantInit(&result);
op.dp.rgvarg = NULL;