-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathApiHooks.cpp
More file actions
1711 lines (1335 loc) · 59.1 KB
/
Copy pathApiHooks.cpp
File metadata and controls
1711 lines (1335 loc) · 59.1 KB
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 "MsRdpEx.h"
#include <MsRdpEx/MsRdpEx.h>
#include <MsRdpEx/Sspi.h>
#include <MsRdpEx/KeyMaps.h>
#include <MsRdpEx/NameResolver.h>
#include <MsRdpEx/RdpInstance.h>
#include <MsRdpEx/Environment.h>
#include <MsRdpEx/Stopwatch.h>
#include <MsRdpEx/OutputMirror.h>
#include <MsRdpEx/Detours.h>
#include <wincred.h>
#include <psapi.h>
#include <intrin.h>
#include <windowsx.h>
HMODULE (WINAPI* Real_LoadLibraryA)(LPCSTR lpLibFileName) = LoadLibraryA;
HMODULE (WINAPI* Real_LoadLibraryW)(LPCWSTR lpLibFileName) = LoadLibraryW;
HMODULE MsRdpEx_LoadLibrary(const char* filename)
{
HMODULE hModule = NULL;
WCHAR* filenameW = NULL;
if (!filename)
return NULL;
MsRdpEx_LogPrint(DEBUG, "LoadLibraryX: %s", filename);
if (MsRdpEx_ConvertToUnicode(CP_UTF8, 0, filename, -1, &filenameW, 0) < 1)
return NULL;
hModule = Real_LoadLibraryW(filenameW);
free(filenameW);
return hModule;
}
HMODULE Hook_LoadLibraryA(LPCSTR lpLibFileName)
{
HMODULE hModule = NULL;
bool interceptedCall = false;
const char* filename = MsRdpEx_FileBase(lpLibFileName);
if (MsRdpEx_StringIEquals(filename, "winscard.dll")) {
char* winscardDll = MsRdpEx_GetEnv("MSRDPEX_WINSCARD_DLL");
if (MsRdpEx_FileExists(winscardDll)) {
MsRdpEx_LogPrint(DEBUG, "LoadLibraryA: \"%s\" -> \"%s\"", lpLibFileName, winscardDll);
hModule = Real_LoadLibraryA(winscardDll);
interceptedCall = true;
}
free(winscardDll);
}
if (!interceptedCall) {
// LoadLibraryA calls LoadLibraryExW under the hood, don't log here
//MsRdpEx_LogPrint(DEBUG, "LoadLibraryA: %s", lpLibFileName);
hModule = Real_LoadLibraryA(lpLibFileName);
}
return hModule;
}
HMODULE Hook_LoadLibraryW(LPCWSTR lpLibFileName)
{
HMODULE hModule;
const char* filename;
char* lpLibFileNameA = NULL;
const char* msrdpexLibraryA = NULL;
WCHAR* msrdpexLibraryW = NULL;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, lpLibFileName, -1, &lpLibFileNameA, 0, NULL, NULL);
filename = MsRdpEx_FileBase(lpLibFileNameA);
if (MsRdpEx_StringIEquals(filename, "mstscax.dll")) {
msrdpexLibraryA = MsRdpEx_GetPath(MSRDPEX_LIBRARY_PATH);
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, msrdpexLibraryA, -1, &msrdpexLibraryW, 0);
MsRdpEx_LogPrint(DEBUG, "LoadLibraryW: \"%s\" -> \"%s\"", lpLibFileNameA, msrdpexLibraryA);
hModule = Real_LoadLibraryW(msrdpexLibraryW);
}
else if (MsRdpEx_StringIEquals(filename, "rdclientax.dll")) {
msrdpexLibraryA = MsRdpEx_GetPath(MSRDPEX_LIBRARY_PATH);
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, msrdpexLibraryA, -1, &msrdpexLibraryW, 0);
MsRdpEx_LogPrint(DEBUG, "LoadLibraryW: \"%s\" -> \"%s\"", lpLibFileNameA, msrdpexLibraryA);
hModule = Real_LoadLibraryW(msrdpexLibraryW);
}
else {
// LoadLibraryW calls LoadLibraryExW under the hood, don't log here
//MsRdpEx_LogPrint(DEBUG, "LoadLibraryW: %s", lpLibFileNameA);
hModule = Real_LoadLibraryW(lpLibFileName);
}
free(lpLibFileNameA);
free(msrdpexLibraryW);
return hModule;
}
typedef HMODULE(WINAPI* Func_LoadLibraryExA)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
typedef HMODULE(WINAPI* Func_LoadLibraryExW)(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags);
Func_LoadLibraryExA Real_LoadLibraryExA = NULL;
Func_LoadLibraryExW Real_LoadLibraryExW = NULL;
HMODULE WINAPI Hook_LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
HMODULE hModule = NULL;
// LoadLibraryExA calls LoadLibraryExW under the hood, don't log here
//MsRdpEx_LogPrint(DEBUG, "LoadLibraryExA: %s", lpLibFileName);
hModule = Real_LoadLibraryExA(lpLibFileName, hFile, dwFlags);
return hModule;
}
static LPCWSTR LoadLibraryExW_LastFileName = NULL;
HMODULE WINAPI Hook_LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
{
HMODULE hModule;
const char* filename;
char* lpLibFileNameA = NULL;
bool interceptedCall = false;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, lpLibFileName, -1, &lpLibFileNameA, 0, NULL, NULL);
filename = MsRdpEx_FileBase(lpLibFileNameA);
if (MsRdpEx_StringIEquals(filename, "credssp.dll")) {
char* credsspDllA = MsRdpEx_GetEnv("MSRDPEX_CREDSSP_DLL");
if (credsspDllA)
{
WCHAR* credsspDllW = NULL;
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, credsspDllA, -1, &credsspDllW, 0);
if (credsspDllW) {
MsRdpEx_LogPrint(DEBUG, "LoadLibraryExW: \"%s\" -> \"%s\"", lpLibFileNameA, credsspDllA);
hModule = Real_LoadLibraryExW(credsspDllW, hFile, dwFlags);
interceptedCall = true;
}
free(credsspDllW);
}
free(credsspDllA);
}
if (MsRdpEx_IsAddressInRdpExeModule(_ReturnAddress())) {
// mstsc.exe!CAxHostWnd::CreateControl calls LoadLibraryExW instead of LoadLibraryW
// on Windows 11 23H2 - we want to intercept that call to get ourselves loaded
// instead of the RDP ActiveX, while being careful not to intercept other calls
const char* msrdpexLibraryA = NULL;
WCHAR* msrdpexLibraryW = NULL;
if (MsRdpEx_StringIEquals(filename, "mstscax.dll")) {
msrdpexLibraryA = MsRdpEx_GetPath(MSRDPEX_LIBRARY_PATH);
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, msrdpexLibraryA, -1, &msrdpexLibraryW, 0);
MsRdpEx_LogPrint(DEBUG, "LoadLibraryExW: \"%s\" -> \"%s\"", lpLibFileNameA, msrdpexLibraryA);
hModule = Real_LoadLibraryW(msrdpexLibraryW);
interceptedCall = true;
}
else if (MsRdpEx_StringIEquals(filename, "rdclientax.dll")) {
msrdpexLibraryA = MsRdpEx_GetPath(MSRDPEX_LIBRARY_PATH);
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, msrdpexLibraryA, -1, &msrdpexLibraryW, 0);
MsRdpEx_LogPrint(DEBUG, "LoadLibraryExW: \"%s\" -> \"%s\"", lpLibFileNameA, msrdpexLibraryA);
hModule = Real_LoadLibraryW(msrdpexLibraryW);
interceptedCall = true;
}
free(msrdpexLibraryW);
}
if (!interceptedCall)
{
// reduce log verbosity for repeated LoadLibraryExW calls
// only log .dll calls, exclude .exe and .sys which is noise
if ((lpLibFileName != LoadLibraryExW_LastFileName) &&
MsRdpEx_IStringEndsWithW(lpLibFileName, L".dll")) {
MsRdpEx_LogPrint(DEBUG, "LoadLibraryExW: %s", lpLibFileNameA);
}
hModule = Real_LoadLibraryExW(lpLibFileName, hFile, dwFlags);
LoadLibraryExW_LastFileName = lpLibFileName;
}
free(lpLibFileNameA);
return hModule;
}
typedef struct _DELAYLOAD_PROC_DESCRIPTOR
{
ULONG ImportDescribedByName;
union
{
PCSTR Name;
ULONG Ordinal;
} Description;
} DELAYLOAD_PROC_DESCRIPTOR, *PDELAYLOAD_PROC_DESCRIPTOR;
typedef struct _DELAYLOAD_INFO
{
ULONG Size;
PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor;
PIMAGE_THUNK_DATA ThunkAddress;
PCSTR TargetDllName;
DELAYLOAD_PROC_DESCRIPTOR TargetApiDescriptor;
PVOID TargetModuleBase;
PVOID Unused;
ULONG LastError;
} DELAYLOAD_INFO, *PDELAYLOAD_INFO;
typedef PVOID(NTAPI* PDELAYLOAD_FAILURE_DLL_CALLBACK)(ULONG NotificationReason, PDELAYLOAD_INFO DelayloadInfo);
typedef PVOID(NTAPI* PDELAYLOAD_FAILURE_SYSTEM_ROUTINE)(PCSTR DllName, PCSTR ProcedureName);
typedef PVOID(NTAPI* Func_LdrResolveDelayLoadedAPI)(PVOID ParentModuleBase,
PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor,
PDELAYLOAD_FAILURE_DLL_CALLBACK FailureDllHook,
PDELAYLOAD_FAILURE_SYSTEM_ROUTINE FailureSystemHook,
PIMAGE_THUNK_DATA ThunkAddress, ULONG Flags);
Func_LdrResolveDelayLoadedAPI Real_LdrResolveDelayLoadedAPI = NULL;
PVOID NTAPI Hook_LdrResolveDelayLoadedAPI(PVOID ParentModuleBase,
PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor,
PDELAYLOAD_FAILURE_DLL_CALLBACK FailureDllHook,
PDELAYLOAD_FAILURE_SYSTEM_ROUTINE FailureSystemHook,
PIMAGE_THUNK_DATA ThunkAddress, ULONG Flags)
{
PVOID ldrResult = NULL;
const char* DllName = (const char*) &((uint8_t*)ParentModuleBase)[DelayloadDescriptor->DllNameRVA];
MsRdpEx_LogPrint(DEBUG, "LdrResolveDelayLoadedAPI: DllName: %s", DllName);
ldrResult = Real_LdrResolveDelayLoadedAPI(ParentModuleBase,
DelayloadDescriptor, FailureDllHook, FailureSystemHook, ThunkAddress, Flags);
return ldrResult;
}
typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, * PUNICODE_STRING;
typedef struct _OBJECT_ATTRIBUTES
{
ULONG Length;
HANDLE RootDirectory;
PUNICODE_STRING ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES, * POBJECT_ATTRIBUTES;
typedef struct _IO_STATUS_BLOCK {
union {
NTSTATUS Status;
PVOID Pointer;
};
ULONG_PTR Information;
} IO_STATUS_BLOCK, * PIO_STATUS_BLOCK;
typedef NTSTATUS (NTAPI * Func_NtCreateFile)(
PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize,
ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions,
PVOID EaBuffer, ULONG EaLength);
Func_NtCreateFile Real_NtCreateFile = NULL;
NTSTATUS NTAPI Hook_NtCreateFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize,
ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions,
PVOID EaBuffer, ULONG EaLength)
{
NTSTATUS ntstatus;
char* pObjectNameA = NULL;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, ObjectAttributes->ObjectName->Buffer,
ObjectAttributes->ObjectName->Length, &pObjectNameA, 0, NULL, NULL);
MsRdpEx_LogPrint(DEBUG, "NtCreateFile: %s", pObjectNameA);
ntstatus = Real_NtCreateFile(FileHandle, DesiredAccess,
ObjectAttributes, IoStatusBlock, AllocationSize,
FileAttributes, ShareAccess, CreateDisposition,
CreateOptions, EaBuffer, EaLength);
free(pObjectNameA);
return ntstatus;
}
typedef NTSTATUS(NTAPI* Func_NtOpenFile)(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
ULONG ShareAccess, ULONG OpenOptions);
Func_NtOpenFile Real_NtOpenFile = NULL;
NTSTATUS NTAPI Hook_NtOpenFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock,
ULONG ShareAccess, ULONG OpenOptions)
{
NTSTATUS ntstatus;
char* pObjectNameA = NULL;
bool interceptedCall = false;
if (ObjectAttributes && ObjectAttributes->ObjectName && ObjectAttributes->ObjectName->Buffer &&
MsRdpEx_IStringEndsWithW(ObjectAttributes->ObjectName->Buffer, L"WinSCard.dll"))
{
char* winscardDll = MsRdpEx_GetEnv("MSRDPEX_WINSCARD_DLL");
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, ObjectAttributes->ObjectName->Buffer,
ObjectAttributes->ObjectName->Length, &pObjectNameA, 0, NULL, NULL);
if (MsRdpEx_FileExists(winscardDll)) {
char NewFilePathA[MSRDPEX_MAX_PATH];
WCHAR* NewFilePathW = NULL;
OBJECT_ATTRIBUTES NewObjectAttributes;
UNICODE_STRING NewObjectName;
sprintf_s(NewFilePathA, MSRDPEX_MAX_PATH, "\\??\\%s", winscardDll);
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, NewFilePathA, -1, &NewFilePathW, 0);
CopyMemory(&NewObjectAttributes, ObjectAttributes, sizeof(OBJECT_ATTRIBUTES));
NewObjectName.Buffer = NewFilePathW;
NewObjectName.Length = wcslen(NewFilePathW) * 2;
NewObjectName.MaximumLength = NewObjectAttributes.ObjectName->Length;
NewObjectAttributes.ObjectName = &NewObjectName;
MsRdpEx_LogPrint(DEBUG, "NtOpenFile: replacing %s with %s", pObjectNameA, NewFilePathA);
ntstatus = Real_NtOpenFile(FileHandle, DesiredAccess, &NewObjectAttributes, IoStatusBlock, ShareAccess, OpenOptions);
interceptedCall = true;
free(NewFilePathW);
}
free(winscardDll);
}
if (!interceptedCall)
{
ntstatus = Real_NtOpenFile(FileHandle, DesiredAccess, ObjectAttributes, IoStatusBlock, ShareAccess, OpenOptions);
}
free(pObjectNameA);
return ntstatus;
}
FARPROC(WINAPI* Real_GetProcAddress)(HMODULE hModule, LPCSTR lpProcName) = GetProcAddress;
FARPROC WINAPI Hook_GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
{
FARPROC procAddr;
procAddr = Real_GetProcAddress(hModule, lpProcName);
return procAddr;
}
INT (WINAPI* Real_GetAddrInfoW)(PCWSTR pNodeName, PCWSTR pServiceName,
const ADDRINFOW* pHints, PADDRINFOW* ppResult) = GetAddrInfoW;
INT WINAPI Hook_GetAddrInfoW(PCWSTR pNodeNameW, PCWSTR pServiceName,
const ADDRINFOW* pHints, PADDRINFOW* ppResult)
{
int status;
char* newNameA = NULL;
WCHAR* newNameW = NULL;
char* pNodeNameA = NULL;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, pNodeNameW, -1, &pNodeNameA, 0, NULL, NULL);
MsRdpEx_LogPrint(DEBUG, "GetAddrInfoW: %s", pNodeNameA);
if (MsRdpEx_NameResolver_GetMapping(pNodeNameA, &newNameA)) {
MsRdpEx_ConvertToUnicode(CP_UTF8, 0, newNameA, -1, &newNameW, 0);
pNodeNameW = newNameW;
free(newNameA);
}
status = Real_GetAddrInfoW(pNodeNameW, pServiceName, pHints, ppResult);
free(pNodeNameA);
free(newNameW);
return status;
}
INT (WINAPI* Real_GetAddrInfoExW)(
PCWSTR pName, PCWSTR pServiceName, DWORD dwNameSpace, LPGUID lpNspId, const ADDRINFOEXW* hints,
PADDRINFOEXW* ppResult, struct timeval* timeout, LPOVERLAPPED lpOverlapped,
LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, LPHANDLE lpHandle) = GetAddrInfoExW;
INT WINAPI Hook_GetAddrInfoExW(
PCWSTR pName, PCWSTR pServiceName, DWORD dwNameSpace, LPGUID lpNspId, const ADDRINFOEXW* hints,
PADDRINFOEXW* ppResult, struct timeval* timeout, LPOVERLAPPED lpOverlapped,
LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, LPHANDLE lpHandle)
{
return Real_GetAddrInfoExW(pName, pServiceName, dwNameSpace, lpNspId, hints,
ppResult, timeout, lpOverlapped, lpCompletionRoutine, lpHandle);
}
BOOL (WINAPI * Real_BitBlt)(
HDC hdc, int x, int y, int cx, int cy,
HDC hdcSrc, int x1, int y1, DWORD rop
) = BitBlt;
bool WINAPI MsRdpEx_CaptureBlt(
HDC hdcDst, int dstX, int dstY, int width, int height,
HDC hdcSrc, int srcX, int srcY)
{
RECT rect = { 0 };
uint32_t frameWidth = 0;
uint32_t frameHeight = 0;
bool captured = false;
bool outputMirrorEnabled = false;
bool videoRecordingEnabled = false;
uint32_t videoRecordingQuality = 5;
uint32_t videoRecordingFrameRate = 0;
bool dumpBitmapUpdates = false;
IMsRdpExInstance* instance = NULL;
MsRdpEx_OutputMirror* outputMirror = NULL;
CMsRdpExtendedSettings* pExtendedSettings = NULL;
HWND hWnd = WindowFromDC(hdcDst);
if (!hWnd)
goto end;
instance = (IMsRdpExInstance*) MsRdpEx_InstanceManager_FindByOutputPresenterHwnd(hWnd);
if (!instance)
goto end;
if (instance)
instance->GetExtendedSettings(&pExtendedSettings);
if (!pExtendedSettings)
goto end;
MsRdpEx_LogPrint(TRACE, "CaptureBlt: hWnd: %p instance: %p", hWnd, instance);
outputMirrorEnabled = pExtendedSettings->GetOutputMirrorEnabled();
videoRecordingEnabled = pExtendedSettings->GetVideoRecordingEnabled();
videoRecordingQuality = pExtendedSettings->GetVideoRecordingQuality();
videoRecordingFrameRate = pExtendedSettings->GetVideoRecordingFrameRate();
dumpBitmapUpdates = pExtendedSettings->GetDumpBitmapUpdates();
if (!outputMirrorEnabled)
goto end;
if (!GetClientRect(hWnd, &rect))
goto end;
LONG bitmapWidth = MsRdpEx_GetRectWidth(&rect);
LONG bitmapHeight = MsRdpEx_GetRectHeight(&rect);
instance->GetOutputMirrorObject((LPVOID*) &outputMirror);
if (!outputMirror)
{
outputMirror = MsRdpEx_OutputMirror_New();
MsRdpEx_OutputMirror_SetDumpBitmapUpdates(outputMirror, dumpBitmapUpdates);
MsRdpEx_OutputMirror_SetVideoRecordingEnabled(outputMirror, videoRecordingEnabled);
MsRdpEx_OutputMirror_SetVideoQualityLevel(outputMirror, videoRecordingQuality);
MsRdpEx_OutputMirror_SetVideoFrameRate(outputMirror, videoRecordingFrameRate);
char* recordingPath = pExtendedSettings->GetRecordingPath();
if (recordingPath) {
MsRdpEx_OutputMirror_SetRecordingPath(outputMirror, recordingPath);
free(recordingPath);
}
char* recordingPipeName = pExtendedSettings->GetRecordingPipeName();
if (recordingPipeName) {
MsRdpEx_OutputMirror_SetRecordingPipeName(outputMirror, recordingPipeName);
free(recordingPipeName);
}
const char* sessionId = pExtendedSettings->GetRecordingSessionId();
if (!sessionId) {
sessionId = pExtendedSettings->GetSessionId();
}
MsRdpEx_OutputMirror_SetSessionId(outputMirror, sessionId);
instance->SetOutputMirrorObject((LPVOID) outputMirror);
}
MsRdpEx_OutputMirror_GetFrameSize(outputMirror, &frameWidth, &frameHeight);
MsRdpEx_OutputMirror_Lock(outputMirror);
if ((frameWidth != bitmapWidth) || (frameHeight != bitmapHeight))
{
MsRdpEx_OutputMirror_Uninit(outputMirror);
MsRdpEx_OutputMirror_SetSourceDC(outputMirror, hdcSrc);
MsRdpEx_OutputMirror_SetFrameSize(outputMirror, bitmapWidth, bitmapHeight);
MsRdpEx_OutputMirror_Init(outputMirror);
}
HDC hShadowDC = MsRdpEx_OutputMirror_GetShadowDC(outputMirror);
BitBlt(hShadowDC, dstX, dstY, width, height, hdcSrc, srcX, srcY, SRCCOPY);
MsRdpEx_OutputMirror_DumpFrame(outputMirror);
MsRdpEx_OutputMirror_Unlock(outputMirror);
captured = true;
end:
return captured;
}
BOOL WINAPI Hook_BitBlt(
HDC hdcDst, int dstX, int dstY, int width, int height,
HDC hdcSrc, int srcX, int srcY, DWORD rop)
{
BOOL status;
MsRdpEx_Stopwatch stopwatch;
status = Real_BitBlt(hdcDst, dstX, dstY, width, height, hdcSrc, srcX, srcY, rop);
MsRdpEx_Stopwatch_Init(&stopwatch, MSRDPEX_PROF_TRACE, true);
bool captured = MsRdpEx_CaptureBlt(hdcDst, dstX, dstY, width, height, hdcSrc, srcX, srcY);
if (captured)
{
MsRdpEx_LogPrint(TRACE, "BitBlt: %d,%d %dx%d %d,%d [%.3fms]", dstX, dstY, width, height, srcX, srcY, MsRdpEx_Stopwatch_GetTime(&stopwatch));
}
return status;
}
BOOL (WINAPI * Real_StretchBlt)(
HDC hdcDest, int xDest, int yDest, int wDest, int hDest,
HDC hdcSrc, int xSrc, int ySrc, int wSrc, int hSrc, DWORD rop
) = StretchBlt;
BOOL WINAPI Hook_StretchBlt(
HDC hdcDst, int dstX, int dstY, int dstW, int dstH,
HDC hdcSrc, int srcX, int srcY, int srcW, int srcH, DWORD rop)
{
BOOL status;
MsRdpEx_Stopwatch stopwatch;
status = Real_StretchBlt(hdcDst, dstX, dstY, dstW, dstH, hdcSrc, srcX, srcY, srcW, srcH, rop);
MsRdpEx_Stopwatch_Init(&stopwatch, MSRDPEX_PROF_TRACE, true);
bool captured = MsRdpEx_CaptureBlt(hdcDst, srcX, srcY, srcW, srcH, hdcSrc, srcX, srcY);
if (captured)
{
MsRdpEx_LogPrint(TRACE, "StretchBlt: %d,%d %dx%d %d,%d %dx%d [%.3fms])", dstX, dstY, dstW, dstH, srcX, srcY, srcW, srcH, MsRdpEx_Stopwatch_GetTime(&stopwatch));
}
end:
return status;
}
#define SYSMENU_RDP_RANGE_FIRST_ID 7100
#define SYSMENU_RDP_SEND_CTRL_ALT_DEL_ID 7101
#define SYSMENU_RDP_SEND_CTRL_ALT_END_ID 7102
#define SYSMENU_RDP_RESIZE_TO_FIT_WINDOW_ID 7103
#define SYSMENU_RDP_RANGE_LAST_ID 7104
static WNDPROC Real_TscShellContainerWndProc = NULL;
static HWND g_hTscShellContainerWnd = NULL;
LRESULT CALLBACK Hook_TscShellContainerWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT result;
CMsRdpExInstance* instance = MsRdpEx_InstanceManager_FindByTscShellContainerWnd(hWnd);
//MsRdpEx_LogPrint(DEBUG, "TscShellContainerWnd: %s (%d)", MsRdpEx_GetWindowMessageName(uMsg), uMsg);
if (uMsg == WM_COMMAND)
uMsg = WM_SYSCOMMAND; // TrackPopupMenu sends WM_COMMAND instead of WM_SYSCOMMAND
result = Real_TscShellContainerWndProc(hWnd, uMsg, wParam, lParam);
if (uMsg == WM_NCCREATE)
{
g_hTscShellContainerWnd = hWnd;
}
if (instance)
{
HWND hInputCaptureWnd = 0;
((IMsRdpExInstance*)instance)->GetInputWindow(&hInputCaptureWnd);
if (hInputCaptureWnd)
{
if (uMsg == WM_SYSCOMMAND)
{
if ((wParam >= SYSMENU_RDP_RANGE_FIRST_ID) && (wParam <= SYSMENU_RDP_RANGE_LAST_ID)) {
SendMessage(hInputCaptureWnd, WM_SYSCOMMAND, wParam, lParam);
}
}
}
}
return result;
}
static WNDPROC Real_BBarWndProc = NULL;
static HMENU g_hExtraMenu = NULL;
LRESULT CALLBACK Hook_BBarWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT result;
//MsRdpEx_LogPrint(DEBUG, "BBarWnd: %s (%d)", MsRdpEx_GetWindowMessageName(uMsg), uMsg);
result = Real_BBarWndProc(hWnd, uMsg, wParam, lParam);
if (uMsg == WM_CONTEXTMENU)
{
if (g_hExtraMenu && g_hTscShellContainerWnd)
{
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
HMENU hSystemMenu = GetSystemMenu(g_hTscShellContainerWnd, FALSE);
TrackPopupMenu(hSystemMenu, TPM_RIGHTBUTTON, xPos, yPos, 0, g_hTscShellContainerWnd, NULL);
}
}
return result;
}
static WNDPROC Real_OPWndProc_mstscax = NULL;
static WNDPROC Real_OPWndProc_rdclientax = NULL;
extern void MsRdpEx_OutputWindow_OnCreate(HWND hWnd, void* pUserData);
LRESULT CALLBACK Hook_OPWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isOOBClient)
{
LRESULT result;
char* lpWindowNameA = NULL;
CMsRdpExInstance* instance = NULL;
MsRdpEx_LogPrint(DEBUG, "OPWndProc: %s (%d)", MsRdpEx_GetWindowMessageName(uMsg), uMsg);
if (uMsg == WM_NCCREATE)
{
CREATESTRUCTW* createStruct = (CREATESTRUCTW*) lParam;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, createStruct->lpszName, -1, &lpWindowNameA, 0, NULL, NULL);
MsRdpEx_LogPrint(DEBUG, "Window Create: %s", lpWindowNameA);
}
if (isOOBClient) {
result = Real_OPWndProc_rdclientax(hWnd, uMsg, wParam, lParam);
} else {
result = Real_OPWndProc_mstscax(hWnd, uMsg, wParam, lParam);
}
if (uMsg == WM_NCCREATE)
{
void* pUserData = (void*) GetWindowLongPtrW(hWnd, GWLP_USERDATA);
instance = MsRdpEx_InstanceManager_AttachOutputWindow(hWnd, pUserData);
if (!instance) {
IMsRdpExInstance* instance = (IMsRdpExInstance*) CMsRdpExInstance_New(NULL);
MsRdpEx_LogPrint(DEBUG, "Creating detached RDP instance: %p hWnd: %p", instance, hWnd);
instance->AttachOutputWindow(hWnd, pUserData);
MsRdpEx_InstanceManager_Add((CMsRdpExInstance*) instance);
}
}
else if (uMsg == WM_NCDESTROY)
{
IUnknown* pRdpClient = NULL;
IMsRdpExInstance* instance = (IMsRdpExInstance*) MsRdpEx_InstanceManager_FindByOutputPresenterHwnd(hWnd);
if (instance) {
instance->GetRdpClient((LPVOID*) &pRdpClient);
if (!pRdpClient) {
MsRdpEx_InstanceManager_Remove((CMsRdpExInstance*) instance);
}
}
}
free(lpWindowNameA);
return result;
}
LRESULT CALLBACK Hook_OPWndProc_mstscax(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return Hook_OPWndProc(hWnd, uMsg, wParam, lParam, FALSE);
}
LRESULT CALLBACK Hook_OPWndProc_rdclientax(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return Hook_OPWndProc(hWnd, uMsg, wParam, lParam, TRUE);
}
ATOM (WINAPI * Real_RegisterClassExW)(const WNDCLASSEXW* wndClassEx) = RegisterClassExW;
ATOM WINAPI Hook_RegisterClassExW(WNDCLASSEXW* wndClass)
{
ATOM wndClassAtom;
char* lpClassNameA = NULL;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, wndClass->lpszClassName, -1, &lpClassNameA, 0, NULL, NULL);
MsRdpEx_LogPrint(DEBUG, "RegisterClassExW: %s", lpClassNameA);
if (MsRdpEx_StringEquals(lpClassNameA, "TscShellContainerClass")) {
Real_TscShellContainerWndProc = wndClass->lpfnWndProc;
wndClass->lpfnWndProc = Hook_TscShellContainerWndProc;
}
else if (MsRdpEx_StringEquals(lpClassNameA, "BBarWindowClass")) {
Real_BBarWndProc = wndClass->lpfnWndProc;
wndClass->lpfnWndProc = Hook_BBarWndProc;
}
else if (MsRdpEx_StringEquals(lpClassNameA, "OPWindowClass")) {
if (MsRdpEx_IsAddressInRdclientAxModule(_ReturnAddress())) {
Real_OPWndProc_rdclientax = wndClass->lpfnWndProc;
wndClass->lpszClassName = L"OPWindowClass_rdclientax";
wndClass->lpfnWndProc = Hook_OPWndProc_rdclientax;
} else {
Real_OPWndProc_mstscax = wndClass->lpfnWndProc;
wndClass->lpszClassName = L"OPWindowClass_mstscax";
wndClass->lpfnWndProc = Hook_OPWndProc_mstscax;
}
}
wndClassAtom = Real_RegisterClassExW(wndClass);
free(lpClassNameA);
return wndClassAtom;
}
static WNDPROC Real_IHWndProc_mstscax = NULL;
static WNDPROC Real_IHWndProc_rdclientax = NULL;
#define MOUSE_JIGGLER_MOVE_MOUSE_TIMER_ID 4301
#define MOUSE_JIGGLER_SPECIAL_KEY_TIMER_ID 4302
LRESULT CALLBACK Hook_IHWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL isOOBClient)
{
LRESULT result;
char* lpWindowNameA = NULL;
IMsRdpExInstance* instance = NULL;
CMsRdpExtendedSettings* pExtendedSettings = NULL;
//MsRdpEx_LogPrint(DEBUG, "IHWndProc: %s (%d)", MsRdpEx_GetWindowMessageName(uMsg), uMsg);
if (uMsg == WM_NCCREATE)
{
CREATESTRUCTW* createStruct = (CREATESTRUCTW*)lParam;
MsRdpEx_ConvertFromUnicode(CP_UTF8, 0, createStruct->lpszName, -1, &lpWindowNameA, 0, NULL, NULL);
MsRdpEx_LogPrint(DEBUG, "Window Create: %s", lpWindowNameA);
}
else
{
instance = (IMsRdpExInstance*)MsRdpEx_InstanceManager_FindByInputCaptureHwnd(hWnd);
if (instance)
instance->GetExtendedSettings(&pExtendedSettings);
}
if (isOOBClient) {
result = Real_IHWndProc_rdclientax(hWnd, uMsg, wParam, lParam);
} else {
result = Real_IHWndProc_mstscax(hWnd, uMsg, wParam, lParam);
}
if (uMsg == WM_NCCREATE)
{
CREATESTRUCTW* createStruct = (CREATESTRUCTW*)lParam;
void* pUserData = createStruct->lpCreateParams;
instance = (IMsRdpExInstance*)MsRdpEx_InstanceManager_AttachInputWindow(hWnd, pUserData);
if (instance)
{
instance->GetExtendedSettings(&pExtendedSettings);
if (pExtendedSettings)
{
bool mouseJigglerEnabled = pExtendedSettings->GetMouseJigglerEnabled();
uint32_t mouseJigglerInterval = pExtendedSettings->GetMouseJigglerInterval();
uint32_t mouseJigglerMethod = pExtendedSettings->GetMouseJigglerMethod();
MsRdpEx_LogPrint(DEBUG, "Mouse Jiggler: Enabled=%d, Interval=%d, Method=%d",
mouseJigglerEnabled ? 1 : 0, mouseJigglerInterval, mouseJigglerMethod);
if (mouseJigglerEnabled)
{
uint32_t timerEventId = MOUSE_JIGGLER_MOVE_MOUSE_TIMER_ID;
switch (mouseJigglerMethod)
{
case 0:
timerEventId = MOUSE_JIGGLER_MOVE_MOUSE_TIMER_ID;
break;
case 1:
timerEventId = MOUSE_JIGGLER_SPECIAL_KEY_TIMER_ID;
break;
}
SetTimer(hWnd, timerEventId, mouseJigglerInterval * 1000, NULL);
}
if (pExtendedSettings->GetExtraSystemMenuEnabled())
{
char className[256];
HWND hParentWnd = GetAncestor(hWnd, GA_ROOT);
GetClassNameA(hParentWnd, className, sizeof(className) - 1);
if (MsRdpEx_StringEquals(className, "TscShellContainerClass"))
{
MsRdpEx_LogPrint(DEBUG, "Ancestor: %s hWnd: %p", className, hParentWnd);
HMENU hSystemMenu = GetSystemMenu(hParentWnd, FALSE);
HMENU hExtraMenu = CreateMenu();
AppendMenu(hExtraMenu, MF_STRING, SYSMENU_RDP_SEND_CTRL_ALT_DEL_ID, L"Send Ctrl+Alt+Del");
AppendMenu(hExtraMenu, MF_STRING, SYSMENU_RDP_SEND_CTRL_ALT_END_ID, L"Send Ctrl+Alt+End");
AppendMenu(hExtraMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hExtraMenu, MF_STRING, SYSMENU_RDP_RESIZE_TO_FIT_WINDOW_ID, L"Resize to fit window");
AppendMenu(hSystemMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSystemMenu, MF_POPUP, (::UINT_PTR)hExtraMenu, L"Extra");
g_hExtraMenu = hExtraMenu;
instance->AttachTscShellContainerWindow(hParentWnd);
}
}
}
}
else
{
MsRdpEx_LogPrint(DEBUG, "Failed to attach input window! hWnd: %p pUserData: %p", hWnd, pUserData);
}
}
else if (uMsg == WM_SYSCOMMAND)
{
switch (wParam)
{
case SYSMENU_RDP_SEND_CTRL_ALT_DEL_ID:
MsRdpEx_LogPrint(DEBUG, "Send Ctrl+Alt+Del");
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_CONTROL, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_MENU, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_DELETE, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_DELETE, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_MENU, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_CONTROL, (LPARAM)0x0);
break;
case SYSMENU_RDP_SEND_CTRL_ALT_END_ID:
MsRdpEx_LogPrint(DEBUG, "Send Ctrl+Alt+End");
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_CONTROL, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_MENU, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)VK_END, (LPARAM)0x01000000);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_END, (LPARAM)0x01000000);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_MENU, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYUP, (WPARAM)VK_CONTROL, (LPARAM)0x0);
break;
case SYSMENU_RDP_RESIZE_TO_FIT_WINDOW_ID:
MsRdpEx_LogPrint(DEBUG, "Resize to fit window");
{
IUnknown* pUnknown = NULL;
IMsRdpClient9* pMsRdpClient9 = NULL;
if (instance)
instance->GetRdpClient((LPVOID*)&pUnknown);
if (pUnknown)
pUnknown->QueryInterface(IID_IMsRdpClient9, (LPVOID*)&pMsRdpClient9);
HWND hUIContainerWnd = GetParent(hWnd);
HWND hUIMainWnd = GetParent(hUIContainerWnd);
if (pMsRdpClient9 && hUIMainWnd)
{
RECT clientRect;
GetClientRect(hUIMainWnd, &clientRect);
ULONG ulDesktopWidth = clientRect.right - clientRect.left;
ULONG ulDesktopHeight = clientRect.bottom - clientRect.top;
ULONG ulPhysicalWidth = ulDesktopWidth;
ULONG ulPhysicalHeight = ulDesktopHeight;
ULONG ulOrientation = 0;
ULONG ulDesktopScaleFactor = 100;
ULONG ulDeviceScaleFactor = 100;
MsRdpEx_LogPrint(DEBUG, "UpdateSessionDisplaySettings(%dx%d)",
ulDesktopWidth, ulDesktopHeight);
pMsRdpClient9->UpdateSessionDisplaySettings(
ulDesktopWidth,
ulDesktopHeight,
ulPhysicalWidth,
ulPhysicalHeight,
ulOrientation,
ulDesktopScaleFactor,
ulDeviceScaleFactor);
}
if (pMsRdpClient9)
pMsRdpClient9->Release();
}
break;
}
}
else if (uMsg == WM_TIMER)
{
if (wParam == MOUSE_JIGGLER_MOVE_MOUSE_TIMER_ID)
{
if (instance)
{
int32_t oldPosX = 0;
int32_t oldPosY = 0;
instance->GetLastMousePosition(&oldPosX, &oldPosY);
int32_t newPosX = oldPosX + 1;
int32_t newPosY = oldPosY + 1;
SendMessage(hWnd, WM_MOUSEMOVE, 0, MAKELPARAM(newPosX, newPosY));
SendMessage(hWnd, WM_MOUSEMOVE, 0, MAKELPARAM(oldPosX, oldPosY));
}
}
else if (wParam == MOUSE_JIGGLER_SPECIAL_KEY_TIMER_ID)
{
if (instance)
{
uint32_t specialKey = VK_F15; // 'F15' is ignored by most applications
SendMessage(hWnd, WM_KEYDOWN, (WPARAM)specialKey, (LPARAM)0x0);
SendMessage(hWnd, WM_KEYUP, (WPARAM)specialKey, (LPARAM)0x0);
}
}
}
else if (uMsg == WM_MOUSEMOVE)
{
int32_t mousePosX = GET_X_LPARAM(lParam);
int32_t mousePosY = GET_Y_LPARAM(lParam);
if (instance)
{
instance->SetLastMousePosition(mousePosX, mousePosY);
}
}
else if (uMsg == WM_KEYDOWN)
{
// https://learn.microsoft.com/en-us/windows/win32/termserv/imsrdpclientsecuredsettings-keyboardhookmode
bool ctrlAltDown = ((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState(VK_MENU) & 0x8000));
bool KeyboardHookToggleShortcutEnabled = pExtendedSettings ? pExtendedSettings->GetKeyboardHookToggleShortcutEnabled() : false;
const char* KeyboardHookToggleShortcutKey = pExtendedSettings ? pExtendedSettings->GetKeyboardHookToggleShortcutKey() : "";
if (KeyboardHookToggleShortcutEnabled && ctrlAltDown &&
(wParam == MsRdpEx_KeyNameToVKCode(KeyboardHookToggleShortcutKey)))
{
IUnknown* pUnknown = NULL;
IMsRdpClient9* pMsRdpClient9 = NULL;
IMsRdpClientSecuredSettings2* pMsRdpClientSecuredSettings = NULL;
if (instance)
instance->GetRdpClient((LPVOID*)&pUnknown);
if (pUnknown)
pUnknown->QueryInterface(IID_IMsRdpClient9, (LPVOID*)&pMsRdpClient9);
if (pMsRdpClient9)
pMsRdpClient9->get_SecuredSettings3(&pMsRdpClientSecuredSettings);
LONG keyboardHookMode = 0;
pMsRdpClientSecuredSettings->get_KeyboardHookMode(&keyboardHookMode);
// toggle keyboard hook mode between local and remote
switch (keyboardHookMode)
{
case 0: // Apply key combinations only locally at the client computer.
keyboardHookMode = 1;
break;
case 1: // Apply key combinations at the remote server.
case 2: // Apply key combinations to the remote server only when the client is running in full-screen mode