-
Notifications
You must be signed in to change notification settings - Fork 4
/
rgf1.hpp
7367 lines (6243 loc) · 184 KB
/
rgf1.hpp
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 once
#pragma comment(lib, "comsupp.lib")
namespace RGF
{
// RKEY
// RKEY, quick registry access
class RKEY
{
private:
HKEY k = 0;
public:
class VALUE
{
public:
std::wstring name;
std::vector<char> value; // For enums
HKEY k = 0;
mutable DWORD ty = 0;
VALUE(const wchar_t* s, HKEY kk)
{
if (s)
name = s;
k = kk;
}
bool operator =(const wchar_t* val)
{
ty = REG_SZ;
if (RegSetValueEx(k, name.c_str(), 0, REG_SZ, (BYTE*)val, (DWORD)(wcslen(val) * sizeof(wchar_t))) == ERROR_SUCCESS)
return true;
return false;
}
bool operator =(unsigned long val)
{
ty = REG_DWORD;
return RegSetValueEx(k, name.c_str(), 0, REG_DWORD, (BYTE*)& val, sizeof(val)) == ERROR_SUCCESS;
}
bool operator =(unsigned long long val)
{
ty = REG_QWORD;
return RegSetValueEx(k, name.c_str(), 0, REG_QWORD, (BYTE*)& val, sizeof(val)) == ERROR_SUCCESS;
}
bool Exists()
{
DWORD ch = 0;
if (RegQueryValueEx(k, name.c_str(), 0, &ty, 0, &ch) == ERROR_FILE_NOT_FOUND)
return false;
return true;
}
template <typename T>
operator T() const
{
T ch = 0;
RegQueryValueEx(k, name.c_str(), 0, &ty, 0, &ch);
std::vector<char> d(ch + 10);
ch += 10;
RegQueryValueEx(k, name.c_str(), 0, &ty, (LPBYTE)d.data(), &ch);
T ret = 0;
memcpy(&ret, d.data(), sizeof(T));
return ret;
}
operator std::wstring() const
{
DWORD ch = 0;
RegQueryValueEx(k, name.c_str(), 0, &ty, 0, &ch);
std::vector<char> d(ch + 10);
ch += 10;
RegQueryValueEx(k, name.c_str(), 0, &ty, (LPBYTE)d.data(), &ch);
return std::wstring((const wchar_t*)d.data());
}
bool Delete()
{
return (RegDeleteValue(k, name.c_str()) == ERROR_SUCCESS);
}
};
RKEY(HKEY kk)
{
k = kk;
}
RKEY(const RKEY & k)
{
operator =(k);
}
void operator =(const RKEY & r)
{
Close();
DuplicateHandle(GetCurrentProcess(), r.k, GetCurrentProcess(), (LPHANDLE)& k, 0, false, DUPLICATE_SAME_ACCESS);
}
RKEY(RKEY && k)
{
operator =(std::forward<RKEY>(k));
}
void operator =(RKEY && r)
{
Close();
k = r.k;
r.k = 0;
}
void operator =(HKEY kk)
{
Close();
k = kk;
}
RKEY(HKEY root, const wchar_t* subkey, DWORD acc = KEY_ALL_ACCESS, bool Op = false)
{
Load(root, subkey, acc, Op);
}
bool Load(HKEY root, const wchar_t* subkey, DWORD acc = KEY_ALL_ACCESS, bool Op = false)
{
Close();
if (Op)
return (RegOpenKeyEx(root, subkey, 0, acc, &k) == ERROR_SUCCESS);
return (RegCreateKeyEx(root, subkey, 0, 0, 0, acc, 0, &k, 0) == ERROR_SUCCESS);
}
void Close()
{
if (k)
RegCloseKey(k);
k = 0;
}
~RKEY()
{
Close();
}
bool Valid() const
{
if (k)
return true;
return false;
}
bool DeleteSingle(const wchar_t* sub)
{
return (RegDeleteKey(k, sub) == ERROR_SUCCESS);
}
bool Delete(const wchar_t* sub = 0)
{
#if _WIN32_WINNT >= 0x600
return (RegDeleteTree(k, sub) == ERROR_SUCCESS);
#else
return false;
#endif
}
bool Flush()
{
return (RegFlushKey(k) == ERROR_SUCCESS);
}
std::vector<std::wstring> EnumSubkeys() const
{
std::vector<std::wstring> data;
for (int i = 0;; i++)
{
std::vector<wchar_t> n(300);
DWORD sz = (DWORD)n.size();
if (RegEnumKeyEx(k, i, n.data(), &sz, 0, 0, 0, 0) != ERROR_SUCCESS)
break;
data.push_back(n.data());
}
return data;
}
std::vector<VALUE> EnumValues() const
{
std::vector<VALUE> data;
for (int i = 0;; i++)
{
std::vector<wchar_t> n(300);
DWORD sz = (DWORD)n.size();
DWORD ay = 0;
RegEnumValue(k, i, n.data(), &sz, 0, 0, 0, &ay);
std::vector<char> v(ay);
DWORD ty = 0;
sz = (DWORD)n.size();
if (RegEnumValue(k, i, n.data(), &sz, 0, &ty, (LPBYTE)v.data(), &ay) != ERROR_SUCCESS)
break;
VALUE x(n.data(), k);
x.ty = ty;
x.value = v;
data.push_back(x);
}
return data;
}
VALUE operator [](const wchar_t* v) const
{
VALUE kv(v, k);
return kv;
}
operator HKEY()
{
return k;
}
};
// AX
namespace AXLIBRARY
{
#pragma warning(disable:4100)
// messages
#define AX_QUERYINTERFACE (WM_USER + 1)
#define AX_INPLACE (WM_USER + 2)
#define AX_GETAXINTERFACE (WM_USER + 3)
#define AX_CONNECTOBJECT (WM_USER + 4)
#define AX_DISCONNECTOBJECT (WM_USER + 5)
#define AX_SETDATAADVISE (WM_USER + 6)
#define AX_DOVERB (WM_USER + 7)
#define AX_SETCOMMANDCALLBACK (WM_USER + 8)
#define AX_SETDISPATCHNOTIFICATION (WM_USER + 9)
// #define AX_SETSERVICEPROVIDER (WM_USER + 10)
#define AX_SETDISPATCHNOTIFICATIONFUNC (WM_USER + 11)
#define AX_RECREATE (WM_USER + 12)
#define AX_RECREATEFROMDATA (WM_USER + 150)
#define AX_SETPARENT (WM_USER + 15)
// notifications
#define AXN_SIZE 1
// Registration function
ATOM AXRegister();
int AXConnectObject(IUnknown* Container, REFIID riid, IUnknown* Advisor, IConnectionPointContainer** picpc, IConnectionPoint** picp);
void AXDisconnectObject(IConnectionPointContainer* icpc, IConnectionPoint* icp, unsigned int Cookie);
struct AX_CONNECTSTRUCT
{
IUnknown* Advisor;
IConnectionPointContainer* icpc;
IConnectionPoint* icp;
DWORD id;
CLSID SpecialIIDForceOK;
};
#pragma warning(disable:4584)
// Class AXClientSide
class AXClientSite :
public IOleClientSite,
public IUnknown,
public IServiceProvider,
public IDispatch,
public IAdviseSink,
public IOleInPlaceSite,
public IOleInPlaceFrame
{
protected:
int refNum;
public:
HWND Window;
HWND Parent;
HMENU Menu;
bool InPlace;
int ExternalPlace;
bool CalledCanInPlace;
CLSID SpecialIIDForceOK;
class AX* ax;
// MyClientSite Methods
AXClientSite();
virtual ~AXClientSite();
STDMETHODIMP_(void) OnDataChange2(FORMATETC*);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID iid, void** ppvObject);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// IServiceProvider methods
STDMETHODIMP QueryService(REFGUID guid, REFIID iid, void** ppvObject);
// IOleClientSite methods
STDMETHODIMP SaveObject();
STDMETHODIMP GetMoniker(DWORD dwA, DWORD dwW, IMoniker** pm);
STDMETHODIMP GetContainer(IOleContainer** pc);
STDMETHODIMP ShowObject();
STDMETHODIMP OnShowWindow(BOOL f);
STDMETHODIMP RequestNewObjectLayout();
// IAdviseSink methods
STDMETHODIMP_(void) OnDataChange(FORMATETC* pFormatEtc, STGMEDIUM* pStgmed);
STDMETHODIMP_(void) OnViewChange(DWORD dwAspect, LONG lIndex);
STDMETHODIMP_(void) OnRename(IMoniker* pmk);
STDMETHODIMP_(void) OnSave();
STDMETHODIMP_(void) OnClose();
// IOleInPlaceSite methods
STDMETHODIMP GetWindow(HWND* p);
STDMETHODIMP ContextSensitiveHelp(BOOL);
STDMETHODIMP CanInPlaceActivate();
STDMETHODIMP OnInPlaceActivate();
STDMETHODIMP OnUIActivate();
STDMETHODIMP GetWindowContext(IOleInPlaceFrame** ppFrame, IOleInPlaceUIWindow** ppDoc, LPRECT r1, LPRECT r2, LPOLEINPLACEFRAMEINFO o);
STDMETHODIMP Scroll(SIZE s);
STDMETHODIMP OnUIDeactivate(int);
STDMETHODIMP OnInPlaceDeactivate();
STDMETHODIMP DiscardUndoState();
STDMETHODIMP DeactivateAndUndo();
STDMETHODIMP OnPosRectChange(LPCRECT);
// IOleInPlaceFrame methods
STDMETHODIMP GetBorder(LPRECT l);
STDMETHODIMP RequestBorderSpace(LPCBORDERWIDTHS);
STDMETHODIMP SetBorderSpace(LPCBORDERWIDTHS w);
STDMETHODIMP SetActiveObject(IOleInPlaceActiveObject* pV, LPCOLESTR s);
STDMETHODIMP InsertMenus(HMENU h, LPOLEMENUGROUPWIDTHS x);
STDMETHODIMP SetMenu(HMENU h, HOLEMENU hO, HWND hw);
STDMETHODIMP RemoveMenus(HMENU h);
STDMETHODIMP SetStatusText(LPCOLESTR t);
STDMETHODIMP EnableModeless(BOOL f);
STDMETHODIMP TranslateAccelerator(LPMSG, WORD);
// IDispatch Methods
HRESULT _stdcall GetTypeInfoCount(unsigned int* pctinfo);
HRESULT _stdcall GetTypeInfo(unsigned int iTInfo, LCID lcid, ITypeInfo FAR* FAR* ppTInfo);
HRESULT _stdcall GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR*, unsigned int cNames, LCID lcid, DISPID FAR*);
HRESULT _stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR* pDispParams, VARIANT FAR* pVarResult, EXCEPINFO FAR* pExcepInfo, unsigned int FAR* puArgErr);
// IOleControlSite Methods
};
// Class AX
class AX
{
public:
friend class AXClientSite;
AX(char* clsid);
CLSID GetCLSID();
~AX();
void Init(char* clsid);
void Clean();
AXClientSite Site;
IID* iid;
//_COM_SMARTPTR_TYPEDEF(IOleObject,__uuidof(IOleObject));
CComPtr<IOleObject> OleObject;
// _com_ptr_t<IOleObject> OleObject;
//_com_ptr_t<IStorage> Storage;
CComPtr<IStorage> Storage;
//_com_ptr_t<IViewObject> View;
CComPtr<IViewObject> View;
// _com_ptr_t<IDataObject> Data;
CComPtr<IDataObject> Data;
//_com_ptr_t<IOleInPlaceActiveObject> Pao;
CComPtr<IOleInPlaceActiveObject> Pao;
//_com_ptr_t<IServiceProvider> CustomServiceProvider;
CComPtr<IServiceProvider> CustomServiceProvider;
AX_CONNECTSTRUCT* tcs;
bool AddMenu;
DWORD AdviseToken;
DWORD DAdviseToken[100];
HWND CommandCallbackWindow;
HWND DispatchNotificationWindow;
void(__stdcall* DispatchNotificationFunction)(class AXDISPATCHNOTIFICATION*);
UINT DispatchNotificationMessage;
void SetProcessCmd(HRESULT(*y)(char*, LPARAM)) { pcmd = y; }
HWND hPar = 0;
UINT hClosingMessage = 0;
private:
CLSID clsid;
HRESULT(*pcmd)(char*, LPARAM);
};
class AXDISPATCHNOTIFICATION
{
public:
class AX* ax;
DISPID dispIdMember;
CLSID riid;
LCID lcid;
WORD wFlags;
DISPPARAMS FAR* pDispParams;
VARIANT FAR* pVarResult;
EXCEPINFO FAR* pExcepInfo;
unsigned int FAR* puArgErr;
/*
AXDISPATCHNOTIFICATION()
{
ax = 0;
dispIdMember = 0;
memset(&riid,0,sizeof(riid));
wFlags = 0;
lcid = 0;
pDispParams = 0;
pVarResult = 0;
pExcepInfo = 0;
puArgErr = 0;
}*/
};
// AXClientSite class
// ------- Implement member functions
inline AXClientSite::AXClientSite()
{
refNum = 0;
CalledCanInPlace = 0;
InPlace = 0;
}
inline AXClientSite :: ~AXClientSite()
{
}
// IUnknown methods
inline STDMETHODIMP AXClientSite::QueryInterface(REFIID iid, void** ppvObject)
{
*ppvObject = 0;
if (iid == IID_IOleClientSite)
* ppvObject = (IOleClientSite*)this;
if (iid == IID_IUnknown)
* ppvObject = this;
if (iid == IID_IAdviseSink)
* ppvObject = (IAdviseSink*)this;
if (iid == IID_IDispatch)
* ppvObject = (IDispatch*)this;
if (iid == SpecialIIDForceOK)
* ppvObject = (IDispatch*)this;
if (iid == IID_IServiceProvider)
* ppvObject = (IServiceProvider*)this;
if (ExternalPlace == false)
{
if (iid == IID_IOleInPlaceSite)
* ppvObject = (IOleInPlaceSite*)this;
if (iid == IID_IOleInPlaceFrame)
* ppvObject = (IOleInPlaceFrame*)this;
if (iid == IID_IOleInPlaceUIWindow)
* ppvObject = (IOleInPlaceUIWindow*)this;
}
//* Log Call
if (*ppvObject)
{
this->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
inline STDMETHODIMP_(ULONG) AXClientSite::AddRef()
{
refNum++;
return refNum;
}
inline STDMETHODIMP_(ULONG) AXClientSite::Release()
{
refNum--;
return refNum;
}
// IServiceManager
inline STDMETHODIMP AXClientSite::QueryService(REFGUID guid, REFIID iid, void** ppvObject)
{
if (!ax->CustomServiceProvider)
return E_NOINTERFACE;
return ax->CustomServiceProvider->QueryService(guid, iid, ppvObject);
}
// IOleClientSite methods
inline STDMETHODIMP AXClientSite::SaveObject()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::GetMoniker(DWORD dwA, DWORD dwW, IMoniker * *pm)
{
*pm = 0;
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::GetContainer(IOleContainer * *pc)
{
*pc = 0;
return E_FAIL;
}
inline STDMETHODIMP AXClientSite::ShowObject()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::OnShowWindow(BOOL f)
{
InvalidateRect(Window, 0, TRUE);
InvalidateRect(Parent, 0, TRUE);
return S_OK;
}
inline STDMETHODIMP AXClientSite::RequestNewObjectLayout()
{
return S_OK;
}
inline STDMETHODIMP_(void) AXClientSite::OnViewChange(DWORD dwAspect, LONG lIndex)
{
}
inline STDMETHODIMP_(void) AXClientSite::OnRename(IMoniker * pmk)
{
}
inline STDMETHODIMP_(void) AXClientSite::OnSave()
{
}
inline STDMETHODIMP_(void) AXClientSite::OnClose()
{
}
// IOleInPlaceSite methods
inline STDMETHODIMP AXClientSite::GetWindow(HWND * p)
{
*p = Window;
return S_OK;
}
inline STDMETHODIMP AXClientSite::ContextSensitiveHelp(BOOL)
{
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::CanInPlaceActivate()
{
if (InPlace)
{
CalledCanInPlace = true;
return S_OK;
}
return S_FALSE;
}
inline STDMETHODIMP AXClientSite::OnInPlaceActivate()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::OnUIActivate()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::GetWindowContext(IOleInPlaceFrame * *ppFrame, IOleInPlaceUIWindow * *ppDoc, LPRECT r1, LPRECT r2, LPOLEINPLACEFRAMEINFO o)
{
*ppFrame = (IOleInPlaceFrame*)this;
AddRef();
*ppDoc = NULL;
GetClientRect(Window, r1);
GetClientRect(Window, r2);
o->cb = sizeof(OLEINPLACEFRAMEINFO);
o->fMDIApp = false;
o->hwndFrame = Parent;
o->haccel = 0;
o->cAccelEntries = 0;
return S_OK;
}
inline STDMETHODIMP AXClientSite::Scroll(SIZE s)
{
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::OnUIDeactivate(int)
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::OnInPlaceDeactivate()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::DiscardUndoState()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::DeactivateAndUndo()
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::OnPosRectChange(LPCRECT)
{
return S_OK;
}
// IOleInPlaceFrame methods
inline STDMETHODIMP AXClientSite::GetBorder(LPRECT l)
{
GetClientRect(Window, l);
return S_OK;
}
inline STDMETHODIMP AXClientSite::RequestBorderSpace(LPCBORDERWIDTHS b)
{
//return S_OK;
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::SetBorderSpace(LPCBORDERWIDTHS b)
{
return S_OK;
}
inline STDMETHODIMP AXClientSite::SetActiveObject(IOleInPlaceActiveObject * pV, LPCOLESTR s)
{
ax->Pao = pV;
return S_OK;
}
inline STDMETHODIMP AXClientSite::SetStatusText(LPCOLESTR t)
{
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::EnableModeless(BOOL f)
{
return E_NOTIMPL;
}
inline STDMETHODIMP AXClientSite::TranslateAccelerator(LPMSG, WORD)
{
return E_NOTIMPL;
}
// IDispatch Methods
inline HRESULT _stdcall AXClientSite::GetTypeInfoCount(
unsigned int* pctinfo) {
return E_NOTIMPL;
}
inline HRESULT _stdcall AXClientSite::GetTypeInfo(
unsigned int iTInfo,
LCID lcid,
ITypeInfo FAR * FAR * ppTInfo) {
return E_NOTIMPL;
}
inline HRESULT _stdcall AXClientSite::GetIDsOfNames(
REFIID riid,
OLECHAR FAR * FAR*,
unsigned int cNames,
LCID lcid,
DISPID FAR*) {
return E_NOTIMPL;
}
// Other Methods
inline void AX::Init(char* cls)
{
pcmd = 0;
wchar_t x[1000] = { 0 };
MultiByteToWideChar(CP_ACP, 0, cls, -1, x, 1000);
CLSIDFromString(x, &clsid);
iid = (IID*)& IID_IOleObject;
OleObject = 0;
Storage = 0;
View = 0;
Data = 0;
Pao = 0;
AdviseToken = 0;
CustomServiceProvider = 0;
memset(DAdviseToken, 0, sizeof(DAdviseToken));
Site.ax = this;
DispatchNotificationFunction = 0;
DispatchNotificationWindow = 0;
DispatchNotificationMessage = 0;
}
inline AX::AX(char* cls)
{
Init(cls);
}
inline void AX::Clean()
{
if (Site.InPlace == true)
{
Site.InPlace = false;
CComPtr<IOleInPlaceObject> iib = 0;
if (OleObject)
OleObject->QueryInterface(IID_IOleInPlaceObject, (void**)& iib);
if (iib)
{
iib->UIDeactivate();
iib->InPlaceDeactivate();
}
}
if (AdviseToken && OleObject)
{
OleObject->Unadvise(AdviseToken);
AdviseToken = 0;
}
if (Data)
{
for (int i = 0; i < 100; i++)
if (DAdviseToken[i])
Data->DUnadvise(DAdviseToken[i]);
memset(DAdviseToken, 0, sizeof(DAdviseToken));
}
Pao = 0;
Data = 0;
View = 0;
Storage = 0;
OleObject = 0;
}
inline AX :: ~AX()
{
Clean();
}
inline CLSID AX::GetCLSID()
{
return clsid;
}
inline HRESULT _stdcall AXClientSite::InsertMenus(HMENU h, LPOLEMENUGROUPWIDTHS x)
{
/* AX * t = (AX*)ax;
if (t->AddMenu)
{
x->width[0] = 0;
x->width[2] = 0;
x->width[4] = 0;
//InsertMenu(h,0,MF_BYPOSITION | MF_POPUP,(int)Menu,"test");
return S_OK;
}
*/
return E_NOTIMPL;
}
inline HRESULT _stdcall AXClientSite::SetMenu(HMENU h, HOLEMENU hO, HWND hw)
{
// AX * t = (AX*)ax;
/* if (t->AddMenu)
{
if (!h && !hO)
{
//::SetMenu(Window,Menu);
//DrawMenuBar(Window);
::SetMenu(Parent,Menu);
DrawMenuBar(Parent);
return S_OK;
}
//::SetMenu(Window,h);
//DrawMenuBar(Window);
//HMENU hm = GetMenu(Parent);
//AppendMenu(hm,MF_POPUP | MF_MENUBREAK,(int)h,0);
//::SetMenu(Parent,hm);
::SetMenu(Parent,h);
DrawMenuBar(Parent);
//hOleWindow = hw;
//OleSetMenuDescriptor(hO,Window,hw,0,0);
OleSetMenuDescriptor(hO,Parent,hw,0,0);
return S_OK;
}
*/
return E_NOTIMPL;
}
inline HRESULT _stdcall AXClientSite::RemoveMenus(HMENU h)
{
#ifdef WINCE
return E_NOTIMPL;
#else
AX* t = (AX*)ax;
if (t->AddMenu)
{
if (!h)
return S_OK;
int c = GetMenuItemCount(h);
for (int i = c; i >= 0; i--)
{
HMENU hh = GetSubMenu(h, i);
if (hh == Menu)
RemoveMenu(h, i, MF_BYPOSITION);
}
if (h == Menu)
DestroyMenu(h);
//DrawMenuBar(Window);
DrawMenuBar(Parent);
return S_OK;
}
return E_NOTIMPL;
#endif
}
//extern HRESULT ProcessCmd(char*);
inline HRESULT _stdcall AXClientSite::Invoke(
DISPID dispIdMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS FAR * pDispParams,
VARIANT FAR * pVarResult,
EXCEPINFO FAR * pExcepInfo,
unsigned int FAR * puArgErr)
{
AXDISPATCHNOTIFICATION axd = { ax,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr };
if (ax->DispatchNotificationWindow)
{
SendMessage(ax->DispatchNotificationWindow, ax->DispatchNotificationMessage, 0, (LPARAM)& axd);
}
if (ax->DispatchNotificationFunction)
{
ax->DispatchNotificationFunction(&axd);
return S_OK;
}
if (ax->pcmd == 0)
return S_OK;
// Check for DWebBrowserEvent2 :: BeforeNavigate2
// dispid = 0xfa
char zv[1000] = { 0 };
if (dispIdMember == 0xfa && pDispParams->cArgs == 7)
{
for (unsigned int i = 0; i < pDispParams->cArgs; i++)
{
if (pDispParams->rgvarg[i].vt == (VT_VARIANT | VT_BYREF))
{
VARIANT* xv = pDispParams->rgvarg[i].pvarVal;
if (xv->vt == VT_BSTR)
WideCharToMultiByte(0, 0, xv->bstrVal, -1, zv, 1000, 0, 0);
if (strncmp(zv, "app:", 4) == 0)
{
for (unsigned int x = 0; x < pDispParams->cArgs; x++)
{
if (pDispParams->rgvarg[x].vt == (VT_BOOL | VT_BYREF))
{
VARIANT_BOOL* y = pDispParams->rgvarg[x].pboolVal;
*y = VARIANT_TRUE;
break;
}
}
return ax->pcmd(zv, (LPARAM)ax);
}
}
if (pDispParams->rgvarg[i].vt == VT_BSTR)
{
VARIANT* xv = &pDispParams->rgvarg[i];
if (xv->vt == VT_BSTR)
WideCharToMultiByte(0, 0, xv->bstrVal, -1, zv, 1000, 0, 0);
if (strncmp(zv, "app:", 4) == 0)
{
for (unsigned int x = 0; x < pDispParams->cArgs; x++)
{
if (pDispParams->rgvarg[x].vt == (VT_BOOL | VT_BYREF))
{
VARIANT_BOOL* y = pDispParams->rgvarg[x].pboolVal;
*y = VARIANT_TRUE;
break;
}
}
return ax->pcmd(zv, (LPARAM)ax);
}
}
}
return S_OK;
}
return S_OK;
}
inline void _stdcall AXClientSite::OnDataChange(FORMATETC * pFormatEtc, STGMEDIUM * pStgmed)
{
// Notify our app that a change is being requested
return;
}
// Window Procedure for AX control
inline LRESULT CALLBACK AXWndProc(HWND hh, UINT mm, WPARAM ww, LPARAM ll)
{
if (mm == WM_CLOSE)
{
AX* ax = (AX*)GetWindowLongPtr(hh, GWLP_USERDATA);
if (!ax)
return 0;
if (!ax->hPar)
return 0;
auto st = GetWindowLong(hh, GWL_STYLE);
if (st & WS_CHILD)
return DefWindowProc(hh, mm, ww, ll);
return SendMessage(ax->hPar, ax->hClosingMessage, (WPARAM)hh, 0);
}
if (mm == WM_CREATE || mm == AX_RECREATE)
{
AX* ax;
char tit[1000] = { 0 };
HRESULT hr;
wchar_t wtit[1000] = { 0 };
GetWindowTextW(hh, wtit, 1000);
WideCharToMultiByte(CP_ACP, 0, wtit, -1, tit, 1000, 0, 0);
if (mm == WM_CREATE)
{
if (tit[0] == '}')
return 0; // No Creation at the momemt
}
if (mm == AX_RECREATE) // ll is the IUnknown
{
tit[0] = '{';
}
ax = new AX(tit);
SetWindowLongPtr(hh, GWLP_USERDATA, (LONG_PTR)ax);
ax->Site.Window = hh;
ax->Site.ax = ax;
ax->Site.Parent = GetParent(hh);
hr = StgCreateDocfile(0, STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_DIRECT | STGM_CREATE, 0, &ax->Storage);