-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinIo.cpp
More file actions
184 lines (146 loc) · 4.13 KB
/
WinIo.cpp
File metadata and controls
184 lines (146 loc) · 4.13 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
// ---------------------------------------------------- //
// WinIo v3.0 //
// Direct Hardware Access Under Windows //
// Copyright 1998-2010 Yariv Kaplan //
// http://www.internals.com
// 初始化与 加载WinIO驱动的相关函数实现 //
// ---------------------------------------------------- //
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winioctl.h>
#include "phys32.h"
// 加载驱动头文件
#include "./WinIoNoteSys/OpenLibSys.h"
#include "winio.h"
HANDLE hDriver = INVALID_HANDLE_VALUE;
bool IsWinIoInitialized = false;
wchar_t szWinIoDriverPath[32768];
bool g_Is64BitOS;
typedef UINT (WINAPI* GETSYSTEMWOW64DIRECTORY)(LPTSTR, UINT);
BOOL Is64BitOS()
{
#ifdef _WIN64
return TRUE;
#else
GETSYSTEMWOW64DIRECTORY getSystemWow64Directory;
HMODULE hKernel32;
TCHAR Wow64Directory[32767];
hKernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (hKernel32 == NULL)
{
//
// This shouldn't happen, but if we can't get
// kernel32's module handle then assume we are
// on x86. We won't ever install 32-bit drivers
// on 64-bit machines, we just want to catch it
// up front to give users a better error message.
//
return FALSE;
}
getSystemWow64Directory = (GETSYSTEMWOW64DIRECTORY)GetProcAddress(hKernel32, "GetSystemWow64DirectoryW");
if (getSystemWow64Directory == NULL)
{
//
// This most likely means we are running
// on Windows 2000, which didn't have this API
// and didn't have a 64-bit counterpart.
//
return FALSE;
}
if ((getSystemWow64Directory(Wow64Directory, _countof(Wow64Directory)) == 0) &&
(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)) {
return FALSE;
}
//
// GetSystemWow64Directory succeeded
// so we are on a 64-bit OS.
//
return TRUE;
#endif
}
bool GetDriverPath()
{
PWSTR pszSlash;
//if (!GetModuleFileName(GetModuleHandle(NULL), szWinIoDriverPath, sizeof(szWinIoDriverPath)))
if (!GetModuleFileName(GetModuleHandle(NULL), szWinIoDriverPath, sizeof(szWinIoDriverPath) / sizeof(szWinIoDriverPath[0])))
return false;
pszSlash = wcsrchr(szWinIoDriverPath, '\\');
if (pszSlash)
pszSlash[1] = 0;
else
return false;
//定义的驱动文件的名称,在此处写时了,也就意味着,sys驱动文件必须为如下名称
// 如果需要请更改如下 sys文件名称,并修改加载时的同级文件名称
if (g_Is64BitOS)
wcscat(szWinIoDriverPath, L"WinIoNote64.sys");
else
wcscat(szWinIoDriverPath, L"WinIoNote64.sys");
return true;
}
bool __stdcall InitializeWinIo()
{
bool bResult;
DWORD dwBytesReturned;
g_Is64BitOS = Is64BitOS();
hDriver = CreateFile(L"\\\\.\\WinIoNote",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// If the driver is not running, install it
if (hDriver == INVALID_HANDLE_VALUE)
{
// 获取驱动文件路径
GetDriverPath();
// 安装RealBomIO驱动服务
bResult = InstallWinIoDriver(szWinIoDriverPath, true);
if (!bResult)
return false;
// 开始RealBomIO驱动
bResult = StartWinIoDriver();
if (!bResult)
return false;
// 打开设备驱动程序
hDriver = CreateFile(L"\\\\.\\WinIoNote", //驱动程序路径
GENERIC_READ | GENERIC_WRITE, // 访问权限 :读写
FILE_SHARE_READ | FILE_SHARE_WRITE, //共享读写权限运行其它进程打开该设备共享权限
NULL,
OPEN_EXISTING, //如果设备已存在则打开它
FILE_ATTRIBUTE_NORMAL, //文件属性正常,没有特殊属性
NULL);
if (hDriver == INVALID_HANDLE_VALUE)
return false;
}
// Enable I/O port access for this process if running on a 32 bit OS
if (!g_Is64BitOS) //如果为32位系统
{
//启用直接 I/O 端口访问的操作
//dwBytesReturned 接受操作后返回的字节数的指针
if (!DeviceIoControl(hDriver, IOCTL_WINIO_ENABLEDIRECTIO, NULL,
0, NULL, 0, &dwBytesReturned, NULL))
{
return false;
}
}
IsWinIoInitialized = true;
return true;
}
void _stdcall ShutdownWinIo()
{
DWORD dwBytesReturned;
if (hDriver != INVALID_HANDLE_VALUE)
{
// Disable I/O port access if running on a 32 bit OS
if (!g_Is64BitOS)
{
DeviceIoControl(hDriver, IOCTL_WINIO_DISABLEDIRECTIO, NULL,
0, NULL, 0, &dwBytesReturned, NULL);
}
CloseHandle(hDriver);
}
//移除WinIO驱动
RemoveWinIoDriver();
IsWinIoInitialized = false;
}