-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPort32.cpp
More file actions
141 lines (107 loc) · 2.88 KB
/
Port32.cpp
File metadata and controls
141 lines (107 loc) · 2.88 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
// ---------------------------------------------------- //
// WinIo v3.0 //
// Direct Hardware Access Under Windows //
// Copyright 1998-2010 Yariv Kaplan //
// http://www.internals.com
// 对SuperIO访问的相关函数 //
// ---------------------------------------------------- //
#include <windows.h>
#include <winioctl.h>
#include <conio.h>
#include "port32.h"
#include "./WinIoNoteSys/OpenLibSys.h"
#include "winio.h"
bool _stdcall GetPortVal(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize)
{
if (!IsWinIoInitialized)
{
return false;
}
#ifdef _WIN64
tagPortStruct PortStruct;
DWORD dwBytesReturned;
PortStruct.bSize = bSize;
PortStruct.wPortAddr = wPortAddr;
// 与驱动程序通信 控制代码 IOCTL_WINIO_READPORT
return DeviceIoControl(hDriver, // WinIO设备句柄
IOCTL_WINIO_READPORT, // 调用的控制码
&PortStruct, // 输入缓冲区 输入参数 驱动层会解析参数
sizeof(PortStruct), // 输入缓冲区大小
pdwPortVal, // 输出缓冲区 通过指针的形式 传出参数
sizeof(DWORD), // 输出缓冲区大小
&dwBytesReturned, // 返回的字节数
NULL);
#elif _WIN32
// If this is a 64 bit OS, we must use the driver to access I/O ports even if the application is 32 bit
if (g_Is64BitOS)
{
tagPortStruct PortStruct;
DWORD dwBytesReturned;
PortStruct.bSize = bSize;
PortStruct.wPortAddr = wPortAddr;
return DeviceIoControl(hDriver, IOCTL_WINIO_READPORT, &PortStruct, sizeof(PortStruct),
pdwPortVal, sizeof(DWORD), &dwBytesReturned, NULL);
}
else
{
switch (bSize)
{
case 1:
// _inp 32位的IO访问函数, 。 从 Visual Studio 2015 开始,CRT 中不再提供这些函数。
*pdwPortVal = _inp(wPortAddr);
break;
case 2:
*pdwPortVal = _inpw(wPortAddr);
break;
case 4:
*pdwPortVal = _inpd(wPortAddr);
break;
}
}
#endif
return true;
}
bool _stdcall SetPortVal(WORD wPortAddr, DWORD dwPortVal, BYTE bSize)
{
if (!IsWinIoInitialized)
{
return false;
}
#ifdef _WIN64
tagPortStruct PortStruct;
DWORD dwBytesReturned;
PortStruct.bSize = bSize;
PortStruct.dwPortVal = dwPortVal;
PortStruct.wPortAddr = wPortAddr;
return DeviceIoControl(hDriver, IOCTL_WINIO_WRITEPORT, &PortStruct, sizeof(PortStruct),
NULL, 0, &dwBytesReturned, NULL);
#elif _WIN32
// If this is a 64 bit OS, we must use the driver to access I/O ports even if the application is 32 bit
if (g_Is64BitOS)
{
tagPortStruct PortStruct;
DWORD dwBytesReturned;
PortStruct.bSize = bSize;
PortStruct.dwPortVal = dwPortVal;
PortStruct.wPortAddr = wPortAddr;
return DeviceIoControl(hDriver, IOCTL_WINIO_WRITEPORT, &PortStruct, sizeof(PortStruct),
NULL, 0, &dwBytesReturned, NULL);
}
else
{
switch (bSize)
{
case 1:
_outp(wPortAddr, dwPortVal);
break;
case 2:
_outpw(wPortAddr, (WORD)dwPortVal);
break;
case 4:
_outpd(wPortAddr, dwPortVal);
break;
}
}
#endif
return true;
}