/
tCOMUtil.cpp
1016 lines (746 loc) · 20.5 KB
/
tCOMUtil.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
#pragma warning( disable : 4189) // warning C4189: 'iVersion' : local variable is initialized but not referenced
#pragma warning( disable : 4244) // conversion from 'int' to 'unsigned short', possible loss of data
// tCOMUtil.cpp: implementation of the tCOMUtil class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ocidl.h>
#include <shlwapi.h>
#include <wchar.h>
#include "tCOMUtil.h"
#include "tLuaCOMException.h"
#include "tUtil.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
tCOMUtil::tCOMUtil()
{
}
tCOMUtil::~tCOMUtil()
{
}
ITypeInfo *tCOMUtil::GetCoClassTypeInfo(CLSID clsid)
{
ITypeLib* typelib = tCOMUtil::LoadTypeLibFromCLSID(clsid);
if(typelib == NULL)
return NULL;
return tCOMUtil::GetCoClassTypeInfo(typelib, clsid);
}
ITypeInfo *tCOMUtil::GetCoClassTypeInfo(IUnknown* punk)
{
HRESULT hr = S_OK;
IProvideClassInfo *pIProvideClassInfo = NULL;
ITypeInfo* coclassinfo = NULL;
hr = punk->QueryInterface(IID_IProvideClassInfo,
(void **) &pIProvideClassInfo);
if (FAILED(hr))
return NULL;
hr = pIProvideClassInfo->GetClassInfo(&coclassinfo);
pIProvideClassInfo->Release();
if(SUCCEEDED(hr))
return coclassinfo;
else
return NULL;
}
ITypeInfo *tCOMUtil::GetCoClassTypeInfo(IDispatch* pdisp, CLSID clsid)
{
CHECKPARAM(pdisp);
ITypeInfo* typeinfo = NULL;
ITypeLib* typelib = NULL;
HRESULT hr = S_OK;
{
unsigned int typeinfocount = 0;
hr = pdisp->GetTypeInfoCount(&typeinfocount);
if(FAILED(hr) || typeinfocount == 0)
return NULL;
}
hr = pdisp->GetTypeInfo(0, 0, &typeinfo);
if(FAILED(hr))
return NULL;
{
unsigned int dumb_index = (unsigned int)-1;
hr = typeinfo->GetContainingTypeLib(&typelib, &dumb_index);
COM_RELEASE(typeinfo);
}
if(FAILED(hr))
return NULL;
ITypeInfo* coclasstypeinfo = tCOMUtil::GetCoClassTypeInfo(typelib, clsid);
typelib->Release();
return coclasstypeinfo;
}
ITypeInfo *tCOMUtil::GetCoClassTypeInfo(ITypeLib *typelib,
const char *coclassname)
{
wchar_t* wcCoClass = (wchar_t*)
malloc( (strlen(coclassname) + 1) * sizeof(wchar_t));
mbstowcs(wcCoClass, coclassname, strlen(coclassname)+1);
const short max_typeinfos = 1000;
MEMBERID dumb[max_typeinfos];
ITypeInfo *typeinfos[max_typeinfos];
unsigned short number = max_typeinfos;
HRESULT hr = typelib->FindName(wcCoClass, 0,
typeinfos, dumb, &number);
free(wcCoClass);
wcCoClass = NULL;
// a interface especificada nao existe nesta TypeLib
if(FAILED(hr) || number == 0)
return NULL;
// tries to find the coclass
unsigned int i = 0;
TYPEATTR *typeattr = NULL;
ITypeInfo *found = NULL;
for(i = 0; i < number; i++)
{
hr = typeinfos[i]->GetTypeAttr(&typeattr);
TYPEKIND typekind = typeattr->typekind;
typeinfos[i]->ReleaseTypeAttr(typeattr);
if(!found && typekind == TKIND_COCLASS)
found = typeinfos[i];
else
typeinfos[i]->Release();
}
return found;
}
ITypeInfo *tCOMUtil::GetDefaultInterfaceTypeInfo(ITypeInfo* pCoClassinfo,
bool source)
{
ITypeInfo* typeinfo = NULL;
// if the component does not have a dispinterface typeinfo
// for events, we stay with an interface typeinfo
ITypeInfo* interface_typeinfo = NULL;
TYPEATTR* pTA = NULL;
HRESULT hr = S_OK;
if (SUCCEEDED(pCoClassinfo->GetTypeAttr(&pTA)))
{
UINT i = 0;
int iFlags = 0;
for (i=0; i < pTA->cImplTypes; i++)
{
//Get the implementation type for this interface
hr = pCoClassinfo->GetImplTypeFlags(i, &iFlags);
if (FAILED(hr))
continue;
if (iFlags & IMPLTYPEFLAG_FDEFAULT || pTA->cImplTypes == 1)
{
if(source == false && !(iFlags & IMPLTYPEFLAG_FSOURCE)
|| source == true && (iFlags & IMPLTYPEFLAG_FSOURCE))
{
HREFTYPE hRefType=0;
/*
* This is the interface we want. Get a handle to
* the type description from which we can then get
* the ITypeInfo.
*/
pCoClassinfo->GetRefTypeOfImplType(i, &hRefType);
hr = pCoClassinfo->GetRefTypeInfo(hRefType, &typeinfo);
// gets typeattr info
TYPEATTR *ptypeattr = NULL;
GUID guid;
TYPEKIND typekind;
hr = typeinfo->GetTypeAttr(&ptypeattr);
if(FAILED(hr))
{
COM_RELEASE(typeinfo);
break;
}
guid = ptypeattr->guid;
typekind = ptypeattr->typekind;
typeinfo->ReleaseTypeAttr(ptypeattr);
if(typekind == TKIND_DISPATCH)
{
// found!
COM_RELEASE(interface_typeinfo);
break;
}
else // hold this pointer. If we do not find
// anything better, we stay with this typeinfo
{
COM_RELEASE(interface_typeinfo);
interface_typeinfo = typeinfo;
typeinfo = NULL;
}
}
}
}
pCoClassinfo->ReleaseTypeAttr(pTA);
}
if(!typeinfo)
return interface_typeinfo;
else
return typeinfo;
}
ITypeInfo *tCOMUtil::GetDispatchTypeInfo(IDispatch* pdisp)
{
ITypeInfo* typeinfo = NULL;
HRESULT hr = pdisp->GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT, &typeinfo);
if(FAILED(hr))
return NULL;
TYPEATTR *ptypeattr = NULL;
typeinfo->GetTypeAttr(&ptypeattr);
TYPEKIND typekind = ptypeattr->typekind;
if(typekind == TKIND_DISPATCH)
{
typeinfo->ReleaseTypeAttr(ptypeattr);
return typeinfo;
}
// tries to find another description of the same
// interface in the typelib with TKIND_DISPATCH
ITypeLib *ptypelib = NULL;
unsigned int dumb = 0;
IID iid = ptypeattr->guid;
hr = typeinfo->GetContainingTypeLib(&ptypelib, &dumb);
typeinfo->ReleaseTypeAttr(ptypeattr);
// if there's no containing type lib, we have to
// trust this one is the right type info
if(FAILED(hr))
return typeinfo;
// obtem a typeinfo do iid fornecido
// caso haja uma implementacao dispinterface,
// esta' e' que sera' retornada (segundo
// documentacao do ActiveX
ITypeInfo* typeinfo_guid = NULL;
hr = ptypelib->GetTypeInfoOfGuid(iid, &typeinfo_guid);
COM_RELEASE(ptypelib);
if(FAILED(hr))
return typeinfo;
// verifica se e' dispinterface
TYPEATTR *ptypeattr_iface = NULL;
hr = typeinfo_guid->GetTypeAttr(&ptypeattr_iface);
TYPEKIND typekind_iface = ptypeattr_iface->typekind;
typeinfo_guid->ReleaseTypeAttr(ptypeattr_iface);
if(typekind_iface == TKIND_DISPATCH)
{
// releases original type information
COM_RELEASE(typeinfo);
return typeinfo_guid;
}
else
{
COM_RELEASE(typeinfo_guid);
// returns original type info
return typeinfo;
}
}
ITypeInfo *tCOMUtil::GetInterfaceTypeInfo(ITypeLib* typelib,
const char *interface_name)
{
wchar_t* wcInterface = (wchar_t*)
malloc( (strlen(interface_name) + 1) * sizeof(wchar_t));
mbstowcs(wcInterface, interface_name, strlen(interface_name)+1);
const int max_typeinfos = 30;
MEMBERID dumb[max_typeinfos];
ITypeInfo *typeinfos[max_typeinfos];
unsigned int number = 30;
HRESULT hr = typelib->FindName(wcInterface, 0,
typeinfos, dumb, (unsigned short *) &number);
free(wcInterface);
wcInterface = NULL;
// a interface especificada nao existe nesta TypeLib
if(FAILED(hr) || number == 0)
return NULL;
// Procura por uma implementacao IDispatch
unsigned int i = 0;
TYPEATTR *typeattr = NULL;
ITypeInfo *found = NULL;
for(i = 0; i < number; i++)
{
hr = typeinfos[i]->GetTypeAttr(&typeattr);
TYPEKIND typekind = typeattr->typekind;
typeinfos[i]->ReleaseTypeAttr(typeattr);
if(!found && typekind == TKIND_DISPATCH)
found = typeinfos[i];
else
typeinfos[i]->Release();
}
return found;
}
/*
* Carrega uma Typelib associada a um ProgID
*/
ITypeLib* tCOMUtil::LoadTypeLibFromProgID(const char* ProgID,
unsigned short major_version)
{
CLSID clsid = IID_NULL;
HRESULT hr = tCOMUtil::ProgID2CLSID(&clsid, ProgID);
if(FAILED(hr))
return NULL;
bool version_found = false;
if(major_version == 0)
{
if(sscanf(ProgID, "%*s.%*s.%d", &major_version) == 1)
version_found = true;
}
// tries to get some version information to help finding
// the right type library
if(version_found)
return tCOMUtil::LoadTypeLibFromCLSID(clsid, major_version);
else
return tCOMUtil::LoadTypeLibFromCLSID(clsid);
}
/*
* LoadTypeLibByName
* Carrega uma typelib a partir de um arquivo TLB
*/
ITypeLib *tCOMUtil::LoadTypeLibByName(const char *pcFilename)
{
HRESULT hr;
ITypeLib *ptlib = NULL;
wchar_t *wcFilename = new wchar_t[strlen(pcFilename)+1];
mbstowcs(wcFilename, pcFilename, strlen(pcFilename)+1);
hr = LoadTypeLibEx(wcFilename, REGKIND_NONE, &ptlib);
delete[] wcFilename;
wcFilename = NULL;
if(FAILED(hr))
return NULL;
return ptlib;
}
ITypeLib* tCOMUtil::LoadTypeLibFromCLSID(CLSID clsid,
unsigned short major_version)
{
wchar_t* wcClsid = NULL;
HRESULT hr = StringFromCLSID(clsid, &wcClsid);
if (FAILED(hr))
return NULL;
/* converte CLSID para string normal */
char* pcClsid = (char*) malloc( (wcslen(wcClsid) + 1) * sizeof(char));
wcstombs(pcClsid, wcClsid,wcslen(wcClsid)+1);
CoTaskMemFree(wcClsid);
DWORD size = 38*3; /*{F37C8063-4AD5-101B-B826-00DD01103DE1}*/
BYTE *bLibID = (BYTE *) malloc(size);
BYTE bVersion[100]; // This must hold something like "5.2"
HKEY iid_key, obj_key, typelib_key, version_key;
/* extrai do registry type library (GUID e versao) */
LONG res = 0;
bool version_info_found = true;
try
{
res = RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID", 0, KEY_READ, &iid_key);
WINCHECK(res == ERROR_SUCCESS);
res = RegOpenKeyEx(iid_key, pcClsid, 0, KEY_READ, &obj_key);
RegCloseKey(iid_key);
free(pcClsid);
pcClsid = NULL;
WINCHECK(res == ERROR_SUCCESS);
res = RegOpenKeyEx(obj_key, "TypeLib",0, KEY_READ, &typelib_key);
if(res != ERROR_SUCCESS)
{
RegCloseKey(obj_key);
LUACOM_EXCEPTION(WINDOWS_ERROR);
}
res = RegOpenKeyEx(obj_key, "version",0, KEY_READ, &version_key);
RegCloseKey(obj_key);
if(res != ERROR_SUCCESS)
version_info_found = false;
RegQueryValueEx(typelib_key, NULL, NULL, NULL, bLibID, &size);
RegCloseKey(typelib_key);
RegQueryValueEx(version_key, NULL, NULL, NULL, bVersion, &size);
RegCloseKey(version_key);
}
catch(class tLuaCOMException& e)
{
UNUSED(e);
if(pcClsid)
free(pcClsid);
free(bLibID);
return NULL;
}
// converts libID to multibyte string
wchar_t* wcTypelib= (wchar_t*)
malloc( (strlen((char *) bLibID) + 1) * sizeof(wchar_t));
mbstowcs(wcTypelib, (char *) bLibID, strlen((char *) bLibID)+1);
// extracts version information
int version_major = 0, version_minor = 0;
int found = 0;
if(version_info_found)
{
found = sscanf(
(const char *) bVersion,
"%d.%d",
&version_major, &version_minor);
}
if(major_version > 0 &&
(
(!version_info_found) || (found == 0)
)
)
{
version_major = major_version;
version_minor = 0;
}
else if(!version_info_found)
{
// tries to load the first type library found in
// the registry
bool result = tCOMUtil::GetDefaultTypeLibVersion(
(const char*) bLibID,
&version_major,
&version_minor);
if(!result) {
free(bLibID);
free(wcTypelib);
return NULL;
}
}
free(bLibID);
GUID libid = IID_NULL;
CLSIDFromString(wcTypelib, &libid);
free(wcTypelib);
ITypeLib* typelib = NULL;
hr = LoadRegTypeLib(libid, version_major, version_minor, 0, &typelib);
if(FAILED(hr))
return NULL;
return typelib;
}
ITypeInfo* tCOMUtil::GetCoClassTypeInfo(ITypeLib *typelib, CLSID clsid)
{
ITypeInfo* coclassinfo = NULL;
HRESULT hr = typelib->GetTypeInfoOfGuid(clsid, &coclassinfo);
if(FAILED(hr))
return NULL;
return coclassinfo;
}
HRESULT tCOMUtil::ProgID2CLSID(CLSID *pClsid, const char *ProgID)
{
CHECKPARAM(pClsid);
// tests whether we already have a CLSID
wchar_t* wcProgId = NULL;
HRESULT hr = S_OK;
if(ProgID[0] == '{')
{
wcProgId = (wchar_t*) malloc( (strlen(ProgID) + 1) * sizeof(wchar_t));
mbstowcs(wcProgId, ProgID, strlen(ProgID)+1);
hr = CLSIDFromString(wcProgId, pClsid);
free(wcProgId);
wcProgId = NULL;
return hr;
}
/* converte ProgID para OLESTR */
wcProgId = (wchar_t*) malloc( (strlen(ProgID) + 1) * sizeof(wchar_t));
mbstowcs(wcProgId, ProgID, strlen(ProgID)+1);
hr = CLSIDFromProgID(wcProgId, pClsid);
free(wcProgId);
wcProgId = NULL;
return hr;
}
CLSID tCOMUtil::GetCLSID(ITypeInfo *coclassinfo)
{
TYPEATTR* ptypeattr = NULL;
HRESULT hr = coclassinfo->GetTypeAttr(&ptypeattr);
if(FAILED(hr))
return IID_NULL;
CLSID clsid = ptypeattr->guid;
coclassinfo->ReleaseTypeAttr(ptypeattr);
return clsid;
}
bool tCOMUtil::GetDefaultTypeLibVersion(const char* libid,
int *version_major,
int *version_minor)
{
LONG res = 0;
HKEY typelib_key, this_typelib_key;
res = RegOpenKeyEx(HKEY_CLASSES_ROOT,"TypeLib", 0, KEY_READ, &typelib_key);
RegCloseKey(HKEY_CLASSES_ROOT);
if(res != ERROR_SUCCESS)
return false;
res = RegOpenKeyEx(typelib_key, libid, 0, KEY_READ, &this_typelib_key);
RegCloseKey(typelib_key);
if(res != ERROR_SUCCESS)
return false;
const int bufsize = 1000;
char version_info[bufsize];
DWORD size = bufsize;
res = RegEnumKeyEx(this_typelib_key, 0, version_info, &size,
NULL, NULL, NULL, NULL);
RegCloseKey(this_typelib_key);
if(res != ERROR_SUCCESS)
return false;
sscanf(version_info, "%d.%d", version_major, version_minor);
return true;
}
bool tCOMUtil::GetRegKeyValue(const char* key, char** pValue) {
LONG ec = 0;
// unused: HKEY hKey;
LONG cbValue;
ec = RegQueryValue(HKEY_CLASSES_ROOT,key,NULL,&cbValue);
if(ERROR_SUCCESS == ec) {
*pValue = new char[cbValue+1];
ec = RegQueryValue(HKEY_CLASSES_ROOT,key,*pValue,&cbValue);
if(ERROR_SUCCESS == ec)
return true;
}
return false;
}
bool tCOMUtil::SetRegKeyValue(const char *key,
const char *subkey,
const char *value)
{
bool ok = false;
LONG ec = 0;
HKEY hKey;
const int bufsize = 10000;
char Key[bufsize];
strncpy(Key, key, bufsize - 1);
if (NULL != subkey)
{
strcat(Key, "\\");
strcat(Key, subkey);
}
ec = RegCreateKeyEx(
HKEY_CLASSES_ROOT,
Key,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
if (ERROR_SUCCESS == ec)
{
if (NULL != value)
{
ec = RegSetValueEx(
hKey,
NULL,
0,
REG_SZ,
(BYTE *)value,
(DWORD)(strlen(value)+1));
}
if (ERROR_SUCCESS == ec)
ok = TRUE;
RegCloseKey(hKey);
}
return ok;
}
bool tCOMUtil::DelRegKey(const char *key,
const char *subkey)
{
LONG ec = 0;
const int bufsize = 10000;
char Key[bufsize];
strncpy(Key, key, bufsize - 1);
if (NULL != subkey)
{
strcat(Key, "\\");
strcat(Key, subkey);
}
ec = SHDeleteKey(HKEY_CLASSES_ROOT, Key);
if (ERROR_SUCCESS == ec)
{
return true;
}
return false;
}
void tCOMUtil::DumpTypeInfo(ITypeInfo *typeinfo)
{
HRESULT hr = S_OK;
TYPEATTR* pta = NULL;
CHECKPARAM(typeinfo);
hr = typeinfo->GetTypeAttr(&pta);
CHK_COM_CODE(hr);
// prints IID
LPOLESTR lpsz = NULL;
#ifdef __WINE__
hr = 0;
MessageBox(NULL, "FIX - not implemented - StringFromIID", "LuaCOM", MB_ICONEXCLAMATION);
#warning FIX - not implemented - StringFromIID
#else
hr = StringFromIID(pta->guid, &lpsz);
#endif
if(FAILED(hr))
{
hr = StringFromCLSID(pta->guid, &lpsz);
}
if(SUCCEEDED(hr))
{
wprintf(L"\nInterface: %s\n\n", lpsz);
CoTaskMemFree(lpsz);
}
int i = 0;
FUNCDESC *pfd = NULL;
for(i = 0; i < pta->cFuncs; i++)
{
typeinfo->GetFuncDesc(i, &pfd);
BSTR names[1];
unsigned int dumb;
typeinfo->GetNames(pfd->memid, names, 1, &dumb);
printf("%.3d: %-30s\tid=0x%d\t%d param(s)\n", i,
tUtil::bstr2string(names[0]), pfd->memid, pfd->cParams);
typeinfo->ReleaseFuncDesc(pfd);
SysFreeString(names[0]);
}
typeinfo->ReleaseTypeAttr(pta);
}
const char* tCOMUtil::getPrintableInvokeKind(const INVOKEKIND invkind)
{
switch(invkind)
{
case INVOKE_PROPERTYGET:
return "propget";
case INVOKE_PROPERTYPUT:
return "propput";
case INVOKE_PROPERTYPUTREF:
return "propputref";
case INVOKE_FUNC:
return "func";
}
return NULL;
}
const char* tCOMUtil::getPrintableTypeDesc(const TYPEDESC& tdesc)
{
static char buffer[200];
buffer[0] = '\0';
switch(tdesc.vt & ~(VT_ARRAY | VT_BYREF))
{
case VT_VOID:
strcat(buffer, "void");
break;
case VT_I2:
strcat(buffer, "short");
break;
case VT_I4:
strcat(buffer, "long");
break;
case VT_R4:
strcat(buffer, "float");
break;
case VT_R8:
strcat(buffer, "double");
break;
case VT_CY:
strcat(buffer, "CY");
break;
case VT_DATE:
strcat(buffer, "DATE");
break;
case VT_BSTR:
strcat(buffer, "BSTR");
break;
case VT_DISPATCH:
strcat(buffer, "IDispatch*");
break;
case VT_BOOL:
strcat(buffer, "VARIANT_BOOL");
break;
case VT_VARIANT:
strcat(buffer, "VARIANT");
break;
case VT_UNKNOWN:
strcat(buffer, "IUnknown*");
break;
case VT_DECIMAL:
strcat(buffer, "Decimal");
break;
case VT_UI1:
strcat(buffer, "unsigned char");
break;
case VT_INT:
strcat(buffer, "int");
break;
case VT_HRESULT:
strcat(buffer, "void");
break;
}
if(tdesc.vt & VT_BYREF)
strcat(buffer, "*");
if(tdesc.vt & VT_ARRAY)
strcat(buffer, "[]");
return buffer;
}
const char* tCOMUtil::getPrintableTypeKind(const TYPEKIND tkind)
{
switch(tkind)
{
case TKIND_COCLASS:
return "coclass";
break;
case TKIND_ENUM:
return "enumeration";
break;
case TKIND_RECORD:
return "record";
break;
case TKIND_MODULE:
return "module";
break;
case TKIND_INTERFACE:
return "interface";
break;
case TKIND_DISPATCH:
return "dispinterface";
break;
case TKIND_ALIAS:
return "alias";
break;
case TKIND_UNION:
return "union";
break;
default:
return "";
break;
}
}
HRESULT tCOMUtil::GUID2String(GUID& Guid, char** ppGuid)
{
wchar_t* wcGuid = NULL;
HRESULT hr = StringFromCLSID(Guid, &wcGuid);
if (FAILED(hr))
return hr;
*ppGuid = new char[wcslen(wcGuid) + 1];
wcstombs(*ppGuid, wcGuid, wcslen(wcGuid)+1);
CoTaskMemFree(wcGuid);
return S_OK;
}
CLSID tCOMUtil::FindCLSID(ITypeInfo* interface_typeinfo)
{
ITypeLib* ptypelib = NULL;
ITypeInfo* ptypeinfo = NULL;
long count = 0;
IID iid = IID_NULL;
TYPEATTR* ptypeattr = NULL;
TYPEKIND tkind;
bool found = false;
CLSID clsid = IID_NULL;
// gets IID
interface_typeinfo->GetTypeAttr(&ptypeattr);
iid = ptypeattr->guid;
interface_typeinfo->ReleaseTypeAttr(ptypeattr);
ptypeattr = NULL;
// Gets type library
interface_typeinfo->GetContainingTypeLib(&ptypelib, NULL);
// iterates looking for IID inside some coclass
count = ptypelib->GetTypeInfoCount();
while(count-- && !found)
{
ptypelib->GetTypeInfoType(count, &tkind);
if(tkind != TKIND_COCLASS)
continue;
// look inside
ptypelib->GetTypeInfo(count, &ptypeinfo);
// gets counts and clsid
ptypeinfo->GetTypeAttr(&ptypeattr);
long ifaces_count = ptypeattr->cImplTypes;
clsid = ptypeattr->guid;
ptypeinfo->ReleaseTypeAttr(ptypeattr);
ptypeattr = NULL;
HREFTYPE RefType;
ITypeInfo* piface_typeinfo = NULL;
while(ifaces_count-- && !found)
{
ptypeinfo->GetRefTypeOfImplType(ifaces_count, &RefType);
ptypeinfo->GetRefTypeInfo(RefType, &piface_typeinfo);
piface_typeinfo->GetTypeAttr(&ptypeattr);
if(IsEqualIID(ptypeattr->guid, iid))
{