-
Notifications
You must be signed in to change notification settings - Fork 17
/
hotpatch.h
2253 lines (1879 loc) · 47.6 KB
/
hotpatch.h
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
#include <comdef.h>
#include <atlbase.h>
#include <atlsafe.h>
#include <vector>
#include <memory>
#include <DbgHelp.h>
#include "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\DIA SDK\include\dia2.h"
#include <psapi.h>
#include <sstream>
#include <thread>
#include <map>
#include <functional>
#ifdef _WIN64
#pragma comment(lib,"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\DIA SDK\\lib\\amd64\\diaguids.lib")
#else
#pragma comment(lib,"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\DIA SDK\\lib\\diaguids.lib")
#endif
#pragma comment(lib,"psapi.lib")
#pragma comment(lib,"dbghelp.lib")
#define DISPID_GETNAMES 71001
#define DISPID_CALL 71002
//#define HOTPATCH_NO_INTEROP
const HKEY hkr = HKEY_CURRENT_USER;
template <typename T, typename T2, typename C>
std::vector<T> &split(const T &s, C delim, std::vector<T> &elems)
{
T2 ss(s);
T item;
while (std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
template <typename T, typename T2, typename C>
std::vector<T> split(const T &s, C delim)
{
std::vector<T> elems;
split<T, T2, C>(s, delim, elems);
return elems;
}
struct IATRESULTS
{
enum class FAILUREREASON
{
SUCCESS = 0,
OTHER = 1,
NOTFOUND = 2,
CANNOTPATCH = 3,
};
struct FUNCTIONINFO
{
string name;
size_t ord = 0;
FAILUREREASON f = FAILUREREASON::SUCCESS;
};
struct MODULEINFO
{
string name;
HINSTANCE handle = 0;
FAILUREREASON f = FAILUREREASON::SUCCESS;
vector<FUNCTIONINFO> functions;
};
vector<MODULEINFO> modules;
};
#ifndef HOTPATCH_NO_INTEROP
// USM
#ifndef _USM_H
#define _USM_H
#ifndef _NO_MUTUAL
class mutual
{
private:
wstring ev1;
wstring str;
HANDLE h1 = 0;
HANDLE h2 = 0;
std::function<void(unsigned long long lp)> lpf;
unsigned long long lpa = 0;
mutual(const mutual&) = delete;
void operator =(const mutual&) = delete;
HANDLE CreateEventX(int i)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s{401F3B1E-6090-4DF2-95C0-F11C10F9F285}_%u", str.c_str(), i);
HANDLE hX = CreateEvent(0, 0, 0, n);
return hX;
}
void WaitForRequestNoLoop()
{
WaitForSingleObject(h1, INFINITE);
if (lpf)
lpf(lpa);
SetEvent(h2);
}
void WaitForRequestLoop()
{
for (;;)
{
if (WaitForSingleObject(h1, INFINITE) != WAIT_OBJECT_0)
break;
if (lpf)
lpf(lpa);
SetEvent(h2);
}
}
public:
mutual(const wchar_t* strn, std::function<void(unsigned long long lp)> req, unsigned long long lp, bool LoopRequest)
{
str = strn;
lpf = req;
lpa = lp;
h1 = CreateEventX(1);
h2 = CreateEventX(2);
if (req)
{
if (LoopRequest)
{
std::thread t(&mutual::WaitForRequestLoop, this);
t.detach();
}
else
{
std::thread t(&mutual::WaitForRequestNoLoop, this);
t.detach();
}
}
}
void request(DWORD Wait = INFINITE)
{
ResetEvent(h2);
SetEvent(h1);
if (!Wait)
return;
WaitForSingleObject(h2, Wait);
}
~mutual()
{
CloseHandle(h2);
h2 = 0;
CloseHandle(h1);
h1 = 0;
}
};
#endif
template <typename T = char>
class usm
{
private:
struct USMHEADER
{
};
struct USMTHREAD
{
DWORD id;
int evidx;
};
// Strings of handle ids
wstring cwmn;
wstring fmn;
wstring evrn;
wstring evrn2;
wstring evwn;
wstring stringid;
bool WasFirst = false;
// Auto reset event that is set when reading thread finishes reading
HANDLE hEventRead = 0;
// Auto reset event that is set when writing thread finishes writing
HANDLE hEventWrote = 0;
// Locked when this thread is writing
// Or when a thread prepares for initializing or exiting
HANDLE hMutexWriting = 0;
// Set when this thread is not reading
// Unset when this thread is reading
HANDLE hEventMeReading = 0;
HANDLE hFM = 0;
unsigned long long ClientSZ = 0;
DWORD MaxThreads = 0;
PVOID Buff = 0;
bool Executable = false;
SECURITY_ATTRIBUTES sattr;
SECURITY_DESCRIPTOR SD;
void FillSA()
{
sattr.nLength = sizeof(sattr);
BOOL fx = InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION);
fx = SetSecurityDescriptorDacl(&SD, TRUE, NULL, FALSE);
sattr.bInheritHandle = true;
sattr.lpSecurityDescriptor = &SD;
}
HANDLE CreateEvR(int idx)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), idx);
HANDLE hX = CreateEvent(&sattr, TRUE, TRUE, n);
return hX;
}
HANDLE CreateEvR2(int idx)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn2.c_str(), idx);
HANDLE hX = CreateEvent(&sattr, 0, 0, n);
return hX;
}
HANDLE CreateEvW()
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s", evwn.c_str());
HANDLE hX = CreateEvent(&sattr, 0, 0, n);
return hX;
}
HANDLE CreateCWM()
{
HANDLE hX = OpenMutex(MUTEX_MODIFY_STATE | SYNCHRONIZE, false, cwmn.c_str());
if (hX != 0)
return hX;
hX = CreateMutex(&sattr, 0, cwmn.c_str());
return hX;
}
HANDLE CreateFM()
{
// Try to open the map , or else create it
WasFirst = true;
HANDLE hX = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), false, fmn.c_str());
if (hX != 0)
{
WasFirst = false;
return hX;
}
unsigned long long FinalSize = ClientSZ * sizeof(T) + MaxThreads * sizeof(USMTHREAD) + sizeof(USMHEADER);
ULARGE_INTEGER ulx = { 0 };
ulx.QuadPart = FinalSize;
hX = CreateFileMapping(INVALID_HANDLE_VALUE, &sattr, (Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE), ulx.HighPart, ulx.LowPart, fmn.c_str());
if (hX != 0)
{
LPVOID Buff4 = MapViewOfFile(hX, FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
if (Buff4)
{
memset(Buff4, 0, (size_t)FinalSize);
UnmapViewOfFile(Buff4);
}
}
return hX;
}
public:
HANDLE fmh()
{
return hX;
}
HANDLE GetFM() { return hFM; }
wstring GetFMN() { return fmn; }
int GetMaxThreads() { return MaxThreads; }
void End()
{
// Remove the ID from the thread
if (Buff)
{
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
WaitForSingleObject(hMutexWriting, INFINITE);
// Find
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
DWORD myid = GetCurrentThreadId();
if (tt.id == myid)
{
tt.id = 0;
tt.evidx = 0;
break;
}
}
ReleaseMutex(hMutexWriting);
}
if (hEventRead)
CloseHandle(hEventRead);
hEventRead = 0;
if (hEventWrote)
CloseHandle(hEventWrote);
hEventWrote = 0;
if (hFM)
CloseHandle(hFM);
hFM = 0;
if (hEventMeReading)
CloseHandle(hEventMeReading);
hEventMeReading = 0;
if (hMutexWriting)
CloseHandle(hMutexWriting);
hMutexWriting = 0;
}
bool IsFirst() { return WasFirst; }
usm(const wchar_t* string_id = 0, bool Init = false, unsigned long long csz = 1048576, DWORD MaxTh = 100)
{
if (!string_id)
return;
CreateInit(string_id, Init, csz, MaxTh);
}
void operator =(const usm &x)
{
// Terminate current
End();
// Recreate
CreateInit(x.stringid.c_str(), true, x.ClientSZ, x.MaxThreads);
}
usm(const usm& x)
{
operator=(x);
}
void CreateInit(const wchar_t* string_id, bool Init = false, unsigned long long csz = 1048576, DWORD MaxTh = 100, bool LocalOnly = false, bool Exex = false)
{
Executable = Exex;
if (!string_id)
return;
if (wcslen(string_id) == 0)
return;
TCHAR xup[1000] = { 0 };
stringid = string_id;
FillSA();
if (LocalOnly)
{
swprintf_s(xup, 1000, L"%s_cwmn", stringid.c_str());
cwmn = xup;
swprintf_s(xup, 1000, L"%s_evrn", stringid.c_str());
evrn = xup;
swprintf_s(xup, 1000, L"%s_evrn2", stringid.c_str());
evrn2 = xup;
swprintf_s(xup, 1000, L"%s_evwn", stringid.c_str());
evwn = xup;
swprintf_s(xup, 1000, L"%s_fmn", stringid.c_str());
fmn = xup;
}
else
{
swprintf_s(xup, 1000, L"Global\\%s_cwmn", stringid.c_str());
cwmn = xup;
swprintf_s(xup, 1000, L"Global\\%s_evrn", stringid.c_str());
evrn = xup;
swprintf_s(xup, 1000, L"Global\\%s_evrn2", stringid.c_str());
evrn2 = xup;
swprintf_s(xup, 1000, L"Global\\%s_evwn", stringid.c_str());
evwn = xup;
swprintf_s(xup, 1000, L"Global\\%s_fmn", stringid.c_str());
fmn = xup;
}
if (!csz)
csz = 1048576;
ClientSZ = csz;
if (!MaxTh)
MaxTh = 100;
MaxThreads = MaxTh;
if (Init)
{
int iv = Initialize();
if (iv <= 0)
{
End();
throw iv;
}
}
}
~usm()
{
End();
}
int Initialize()
{
hEventRead = 0;
hEventWrote = 0;
hMutexWriting = 0;
hFM = 0;
Buff = 0;
hEventMeReading = 0;
if (hMutexWriting == 0)
hMutexWriting = CreateCWM();
if (hMutexWriting == 0)
return -1;
if (hFM == 0)
hFM = CreateFM();
if (hFM == 0)
return -1;
if (hEventWrote == 0)
hEventWrote = CreateEvW();
if (hEventWrote == 0)
return -1;
if (Buff == 0)
Buff = MapViewOfFile(hFM, FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
if (!Buff)
return -1;
// Acquire lock for Count variable
// USMHEADER* h = (USMHEADER*)Buff;
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
WaitForSingleObject(hMutexWriting, INFINITE);
// Find
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.id == 0)
{
tt.id = GetCurrentThreadId();
tt.evidx = (y + 1);
hEventMeReading = CreateEvR(y + 1);
hEventRead = CreateEvR2(y + 1);
break;
}
}
ReleaseMutex(hMutexWriting);
if (!hEventMeReading)
return -1;
return 1;
}
const T* BeginRead(bool FailOnNotReady = false)
{
if (!Buff)
return 0;
// Is someone writing
if (FailOnNotReady)
{
DWORD x = WaitForSingleObject(hMutexWriting, 0);
if (x != WAIT_OBJECT_0)
return 0;
}
else
WaitForSingleObject(hMutexWriting, INFINITE);
// Reset our reading event
ResetEvent(hEventMeReading);
// Release the mutex, but now any writing thread that locks it must wait for is
ReleaseMutex(hMutexWriting);
// Return the pointer
const char* a1 = (const char*)Buff;
a1 += sizeof(USMHEADER);
a1 += sizeof(USMTHREAD)*MaxThreads;
return (T*)a1;
}
void EndRead()
{
SetEvent(hEventMeReading);
SetEvent(hEventRead);
}
unsigned long long ReadData(T* b, size_t sz, size_t offset = 0, bool FailIfNotReady = false)
{
const T* ptr = BeginRead(FailIfNotReady);
if (!ptr)
return (unsigned long long) - 1;
memcpy(b, ptr + offset, sz);
EndRead();
return sz;
}
DWORD NotifyOnRead(bool Wait)
{
// See if any thread is reading
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
vector<HANDLE> evs;
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
DWORD fi = 0;
if (!S)
return (DWORD)-1;
if (evs.empty())
return (DWORD)-2;
// Wait for any thread to terminate reading
fi = WaitForMultipleObjects((DWORD)evs.size(), &evs[0], FALSE, Wait ? INFINITE : 0);
// Cleanup
for (unsigned int i = 0; i < evs.size(); i++)
CloseHandle(evs[i]);
evs.clear();
return fi;
}
T* BeginWrite(bool FailOnNotReady = false)
{
// Lock the writing mutex
if (FailOnNotReady)
{
DWORD x = WaitForSingleObject(hMutexWriting, 0);
if (x != WAIT_OBJECT_0)
return 0;
}
else
WaitForSingleObject(hMutexWriting, INFINITE);
// Having locked the writing mutex, no reading thread can start now
// After that, no new threads can read
vector<HANDLE> evs;
evs.reserve(MaxThreads);
// Wait for threads that are already in read state
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
DWORD fi = 0;
if (S)
{
// Wait for all these threads to terminate reading
fi = WaitForMultipleObjects((DWORD)evs.size(), &evs[0], TRUE, FailOnNotReady ? 0 : INFINITE);
if (fi == -1 || fi == WAIT_TIMEOUT)
S = false;
}
else
{
fi = (DWORD)-1;
}
// Cleanup
for (unsigned int i = 0; i < evs.size(); i++)
CloseHandle(evs[i]);
evs.clear();
if (!S)
{
ReleaseMutex(hMutexWriting);
return 0;
}
// Return the pointer
char* a1 = (char*)Buff;
a1 += sizeof(USMHEADER);
a1 += sizeof(USMTHREAD)*MaxThreads;
ResetEvent(hEventWrote);
return (T*)a1;
}
void EndWrite()
{
ReleaseMutex(hMutexWriting);
SetEvent(hEventWrote);
}
DWORD NotifyWrite(bool Wait)
{
// Wait for all these threads to terminate reading
return WaitForSingleObject(hEventWrote, Wait ? INFINITE : 0);
}
unsigned long long WriteData(const T* b, size_t sz, size_t offset = 0, bool FailIfNotReady = false)
{
T* ptr = BeginWrite(FailIfNotReady);
if (!ptr)
return (unsigned long long) - 1;
memcpy(ptr + offset, b, sz);
EndWrite();
return sz;
}
// Sends data, then waits until all threads have read that data
unsigned long long SendDataAndWait(const T*b, size_t sz, size_t offset = 0)
{
unsigned long long r = WriteData(b, sz, offset);
if (r != sz)
return r;
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
vector<HANDLE> evs;
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.id == GetCurrentThreadId())
continue;
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn2.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
if (!S)
return (DWORD)-1;
if (evs.empty())
return (DWORD)-2;
// Wait for all thread to terminate reading
WaitForMultipleObjects((DWORD)evs.size(), &evs[0], TRUE, INFINITE);
return r;
}
};
#endif // USM_H
#endif // HOTPATCH_NO_INTEROP
#include "xml\\xml3all.h"
using namespace XML3;
struct HPFUNCTION
{
wstring n;
DWORD loctype = 0;
unsigned long long a = 0;
unsigned long long va = 0;
void ToEl(XML3::XMLElement& e)
{
e.FindVariableZ("n",true)->SetValue(XML3::XMLU(n.c_str()));
e.FindVariableZ("lt",true)->SetValueUInt(loctype);
e.FindVariableZ("a",true)->SetValueULongLong(a);
e.FindVariableZ("va",true)->SetValueULongLong(va);
}
void FromEl(XML3::XMLElement& e)
{
n = XML3::XMLU(e.FindVariableZ("n",true)->GetValue().c_str());
loctype = e.FindVariableZ("lt",true)->GetValueUInt();
a = e.FindVariableZ("a",true)->GetValueULongLong();
va = e.FindVariableZ("va",true)->GetValueULongLong();
}
};
inline void gethpgid(void* hp, TCHAR*, int ms);
#define XCXCALL
#ifndef XCXCALL
size_t COMCALPTR = 0;
#endif
#pragma pack(push,1)
struct COMCALL
{
unsigned char jmp1 = 0xE9;
unsigned char jmp2 = 0x27;
unsigned char jmp3 = 0x01;
unsigned char jmp4 = 0x00;
unsigned char jmp5 = 0x00; // JMP $ + 300
IDispatch* HPPointer = 0;
void* HPClass = 0;
#ifdef _WIN64
char data[179] = { 0 };
#else
char data[187] = { 0 };
#endif
char name[100] = { 0 };
// Push pop to stack
#ifdef _WIN64
struct MOVER
{
unsigned char pushreg = 0;
unsigned char poprax = 0x58;
unsigned short movrax = 0xA348;
unsigned long long addr = 0;
};
#else
struct MOVER
{
unsigned char pushreg = 0x66;
unsigned char popeax = 0x58;
unsigned char moveax = 0xA3;
unsigned long addr = 0;
};
#endif
MOVER m1[8]; // base registers
#ifdef _WIN64
struct MOVER2
{
unsigned char pushregp = 0x41;
unsigned char pushreg = 0;
unsigned char poprax = 0x58;
unsigned short movrax = 0xA348;
unsigned long long addr = 0;
};
MOVER2 m2[8]; // r8-r15 registers
#endif
// Call the COMPatchGeneric
#ifdef _WIN64
#ifndef XCXCALL
unsigned short movd1 = 0xB848;
unsigned long long regaddr = 0;
unsigned short movd2x = 0xA348;
unsigned long long movd2 = 0;
#else
unsigned char pushrbp = 0x55;
unsigned char movrbprsp_1 = 0x48;
unsigned char movrbprsp_2 = 0x89;
unsigned char movrbprsp_3 = 0xe5;
unsigned short movrcx = 0xB948;
unsigned long long regaddr = 0;
unsigned short subrsp100_1 = 0x8148;
unsigned short subrsp100_2 = 0x00EC;
unsigned short subrsp100_3 = 0x0002;
unsigned char subrsp100_4 = 0;
#endif // XCXCALL
unsigned short movrax = 0xB848;
unsigned long long calladdr = 0;
unsigned short callrax = 0xD0FF;
#ifdef XCXCALL
// Restore the stack
unsigned short addrsp100_1 = 0x8148;
unsigned short addrsp100_2 = 0x00C4;
unsigned short addrsp100_3 = 0x0002;
unsigned char addrsp100_4 = 0;
#endif
unsigned char poprbp = 0x5d;
unsigned char ret = 0xC3;
#else
// Call the COMPatchGeneric
#ifndef XCXCALL
unsigned short movd1 = 0x05C7;
unsigned long movd2 = 0;
unsigned long regaddr = 0;
#else
unsigned char pushebp = 0x55;
unsigned char movebpesp_1 = 0x89;
unsigned char movebpesp_2 = 0xe5;
unsigned char movecx = 0xB9;
unsigned long regaddr = 0;
// Give the callee some stack to work with
unsigned short subesp100_1 = 0xEC81;
unsigned short subesp100_2 = 0x0100;
unsigned short subesp100_3 = 0x0000;
#endif
unsigned char moveax = 0xB8;
unsigned long calladdr = 0;
unsigned short calleax = 0xD0FF;
#ifdef XCXCALL
// Restore the stack
unsigned short addesp100_1 = 0xC481;
unsigned short addesp100_2 = 0x0100;
unsigned short addesp100_3 = 0x0000;
#endif
unsigned char popebp = 0x5d;
unsigned char ret = 0xC3;
#endif // WIN64
COMCALL(IDispatch*dispp, class HOTPATCH* hpx, size_t targetcall, const wchar_t* fname)
{
HPPointer = dispp;
HPClass = hpx;
calladdr = targetcall;
regaddr = (size_t)this;
#ifndef XCXCALL
movd2 = (size_t)&COMCALPTR;
#endif
for (int i = 0; i < 8; i++)
{
m1[i].pushreg = (unsigned char)(0x50 + i);
m1[i].addr = (size_t)(data + i * sizeof(size_t));
}
#ifdef _WIN64
for (int i = 0; i < 8; i++)
{
m2[i].pushreg = (unsigned char)(0x50 + i);
m2[i].addr = (unsigned long long)(data + (i + 8) * 8);
}
#endif
size_t le = wcslen(fname);
if (le > 50)
le = 50;
memcpy(name, fname, le * 2);
}
};
#pragma pack(pop)
#pragma optimize("",off)
#ifndef XCXCALL
void __fastcall COMPatchGeneric()
{
COMCALL* cc = (COMCALL*)COMCALPTR;
#else
void __fastcall COMPatchGeneric(size_t dx)
{
COMCALL* cc = (COMCALL*)dx;
#endif
IDispatch* d = cc->HPPointer;
if (!d)
return;
CComSafeArray<size_t> a(16);
for (int i = 0; i < 16; i++)
{
size_t aa = 0;
memcpy(&aa, cc->data + (i * sizeof(size_t)), sizeof(aa));
a.SetAt(i, aa);
}
// d->Call(_bstr_t((wchar_t*)cc->name), a);
DISPPARAMS dp = { 0 };
dp.cArgs = 2;
VARIANT v[2] = { 0 };
BSTR b1 = SysAllocString((wchar_t*)cc->name);
v[0].vt = VT_ARRAY;
v[0].parray = a;
v[1].bstrVal = b1;
v[1].vt = VT_BSTR;
dp.rgvarg = v;
d->Invoke(DISPID_CALL, IID_NULL, 0, 0, &dp, 0, 0, 0);
SysFreeString(b1);
}
#pragma optimize("",on)
#pragma optimize("",off)
#ifndef XCXCALL
void __fastcall USMPatchGeneric()
{
COMCALL* cc = (COMCALL*)COMCALPTR;
#else
void __fastcall USMPatchGeneric(size_t dx)
{
COMCALL* cc = (COMCALL*)dx;
#endif
size_t midx = (size_t)cc->HPPointer;
vector<TCHAR> cidx(1000);
gethpgid(cc->HPClass, cidx.data(), 1000);
swprintf_s(cidx.data() + wcslen(cidx.data()), 500, L"-%u", (unsigned int)midx);
mutual mu(cidx.data(), nullptr, 0,true);
mu.request();
}
#pragma optimize("",on)
class SUSPENDEDPROCESS
{
public:
HANDLE hP = 0;
SUSPENDEDPROCESS(HANDLE hX) { hP = hX; }
void Term()
{
if (hP)
{
TerminateProcess(hP,0);
CloseHandle(hP);
hP = 0;
}
}
~SUSPENDEDPROCESS()
{
Term();
}
};
class HOTPATCH
{
public:
struct NAMEANDPOINTER
{
wstring n;