-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdai_workaround.h
110 lines (86 loc) · 2.02 KB
/
dai_workaround.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
#include "pch.h"
namespace daiworkaround
{
typedef BOOL(WINAPI* NtUserAttachThreadInput)(DWORD, DWORD, BOOL);
typedef HWND(WINAPI* GetFocus)(void);
NtUserAttachThreadInput fpNtUserAttachThreadInput = NULL;
GetFocus fpGetFocus = NULL;
BOOL DetourNtUserAttachThreadInput(DWORD from, DWORD to, BOOL attach)
{
static int visited = 0;
static DWORD fromThreadForHack = 0;
static DWORD toThreadForHack = 0;
if (!visited)
{
fromThreadForHack = from;
toThreadForHack = to;
visited = 1;
}
if (from == 0 && to == 0 && visited)
{
from = fromThreadForHack;
to = toThreadForHack;
}
return fpNtUserAttachThreadInput(from, to, attach);
}
HWND DetourGetFocus(void)
{
HWND retValueWindow;
static HWND prev = 0;
retValueWindow = fpGetFocus();
if (retValueWindow == 0 && prev != 0)
{
DetourNtUserAttachThreadInput(0, 0, 1);
}
else
{
prev = retValueWindow;
}
return retValueWindow;
}
static bool is_wine()
{
HMODULE hntdll = GetModuleHandle(L"ntdll.dll");
if (!hntdll)
{
return false;
}
if (GetProcAddress(hntdll, "wine_get_version") != NULL)
{
return true;
}
return false;
}
static void init()
{
if (!is_wine())
{
logger::info("Windows detected: DAI workaround is not needed.");
return;
}
logger::info("Start DAI workaround.");
if (MH_Initialize() != MH_OK)
{
logger::info("Could not initialize MinHook.");
return;
}
if (MH_CreateHookApiEx(L"win32u", "NtUserAttachThreadInput", &DetourNtUserAttachThreadInput, &fpNtUserAttachThreadInput) != MH_OK)
{
logger::info("Could not hook NtUserAttachThreadInput.");
return;
}
logger::info("Hooked NtUserAttachThreadInput.");
if (MH_CreateHookApiEx(L"user32", "GetFocus", &DetourGetFocus, &fpGetFocus) != MH_OK)
{
logger::info("Could not hook GetFocus.");
return;
}
logger::info("Hooked GetFocus.");
if (MH_EnableHook(MH_ALL_HOOKS) != MH_OK)
{
logger::info("Could not enable hooks.");
return;
}
logger::info("MinHook initialization finished.");
}
}