-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriverMgr.cpp
89 lines (78 loc) · 2.35 KB
/
DriverMgr.cpp
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
#include "stdafx.h"
#include "DriverMgr.h"
#include <Setupapi.h>
#include <TCHAR.H>
#pragma comment( lib, "setupapi.lib" )
namespace cchips {
CDriverMgr::CDriverMgr()
:m_hdevice(INVALID_HANDLE_VALUE)
, m_error(0)
{
m_drivername = HIPS_DRIVER_NAME;
OpenDevice();
}
CDriverMgr::~CDriverMgr()
{
if (m_hdevice != INVALID_HANDLE_VALUE)
CloseHandle(m_hdevice);
}
BOOL CDriverMgr::OpenDevice()
{
std::string device_name;
//
// Create a \\.\XXX device name that CreateFile can use
//
// NOTE: We're making an assumption here that the driver
// has created a symbolic link using it's own name
// (i.e. if the driver has the name "XXX" we assume
// that it used IoCreateSymbolicLink to create a
// symbolic link "\DosDevices\XXX". Usually, there
// is this understanding between related apps/drivers.
//
// An application might also peruse the DEVICEMAP
// section of the registry, or use the QueryDosDevice
// API to enumerate the existing symbolic links in the
// system.
//
device_name = std::string("\\\\.\\") + m_drivername;
m_hdevice = CreateFile(device_name.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (m_hdevice == INVALID_HANDLE_VALUE || m_hdevice == nullptr)
{
m_error = GetLastError();
return FALSE;
}
return TRUE;
}
BOOL CDriverMgr::IoControl(
DWORD dwIoControlCode,
LPVOID lpInBuffer,
DWORD nInBufferSize,
LPVOID lpOutBuffer,
DWORD nOutBufferSize,
LPDWORD lpBytesReturned
)
{
BOOL bRet = 0;
DWORD dwReturned = 0;
bRet = DeviceIoControl(
m_hdevice,
dwIoControlCode,
lpInBuffer,
nInBufferSize,
lpOutBuffer,
nOutBufferSize,
&dwReturned,
NULL);
if (lpBytesReturned)
*lpBytesReturned = dwReturned;
m_error = GetLastError();
return (bRet);
}
} // namespace cchips