📄 devicefile.cpp
字号:
// DeviceFile.cpp: implementation of the CDeviceFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "scrtest.h"
#include "DeviceFile.h"
#include "Helper.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDeviceFile::CDeviceFile()
{
m_nCurDev = 0;
}
CDeviceFile::~CDeviceFile()
{
}
BOOLEAN CDeviceFile::ListDevices(LPGUID pGuid)
{
ULONG NumberDevices;
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG i;
BOOLEAN done;
BOOLEAN find = FALSE;
NumberDevices = 0;
m_asDevName.RemoveAll();
//
// Open a handle to the plug and play dev node.
// SetupDiGetClassDevs() returns a device information set that contains info on all
// installed devices of a specified class.
//
hardwareDeviceInfo = SetupDiGetClassDevs (
pGuid,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_INTERFACEDEVICE)); // Function class devices.
//
// Take a wild guess at the number of devices we have;
// Be prepared to realloc and retry if there are more than we guessed
//
NumberDevices = 4;
done = FALSE;
deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
i=0;
while (!done) {
NumberDevices *= 2;
for (; i < NumberDevices; i++) {
// SetupDiEnumDeviceInterfaces() returns information about device interfaces
// exposed by one or more devices. Each call returns information about one interface;
// the routine can be called repeatedly to get information about several interfaces
// exposed by one or more devices.
if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,
0, // We don't care about specific PDOs
pGuid,
i,
&deviceInfoData)) {
TCHAR devName[_MAX_PATH];
hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, devName);
if ( hOut != INVALID_HANDLE_VALUE ) {
find = TRUE;
CString s(devName);
m_asDevName.Add(s);
CloseHandle( hOut );
}
} else {
if (ERROR_NO_MORE_ITEMS == GetLastError()) {
done = TRUE;
break;
}
}
}
}
NumberDevices = i;
// SetupDiDestroyDeviceInfoList() destroys a device information set
// and frees all associated memory.
SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
return find;
}
HANDLE CDeviceFile::OpenOneDevice(HDEVINFO HardwareDeviceInfo, PSP_INTERFACE_DEVICE_DATA DeviceInfoData, LPTSTR devName)
{
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HANDLE hOut = INVALID_HANDLE_VALUE;
//
// allocate a function class device data structure to receive the
// goods about this particular device.
//
SetupDiGetInterfaceDeviceDetail (
HardwareDeviceInfo,
DeviceInfoData,
NULL, // probing so no output buffer yet
0, // probing so output buffer length of zero
&requiredLength,
NULL); // not interested in the specific dev-node
predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;
functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc (predictedLength);
functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);
//
// Retrieve the information from Plug and Play.
//
if (! SetupDiGetInterfaceDeviceDetail (
HardwareDeviceInfo,
DeviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL)) {
free( functionClassDeviceData );
return INVALID_HANDLE_VALUE;
}
_tcscpy( devName,functionClassDeviceData->DevicePath) ;
TRACE( _T("Attempting to open %s\n"), devName );
hOut = CreateFile (
functionClassDeviceData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file
if (INVALID_HANDLE_VALUE == hOut) {
TRACE( _T("FAILED to open %s\n"), devName );
}
free( functionClassDeviceData );
return hOut;
}
void CDeviceFile::SetCurDev(int n)
{
m_nCurDev = n;
}
int CDeviceFile::GetCurDev()
{
return m_nCurDev;
}
BOOL CDeviceFile::DeviceIoControl(
DWORD dwIoControlCode,
LPVOID lpInBuffer,
DWORD nInBufferSize,
LPVOID lpOutBuffer,
DWORD nOutBufferSize,
LPDWORD lpBytesReturned)
{
BOOL tSuccess = FALSE;
if(m_asDevName.GetSize() == 0)
{
AfxMessageBox(_T("Device not present or busy!"));
return tSuccess;
}
tSuccess = m_file.Open(
m_asDevName[m_nCurDev],
CFile::modeReadWrite|CFile::shareExclusive
);
if( !tSuccess )
{
AfxMessageBox(_T("Open File Failed!"));
return( FALSE );
}
tSuccess = ::DeviceIoControl((HANDLE)m_file.m_hFile,
dwIoControlCode,
lpInBuffer,
nInBufferSize,
lpOutBuffer,
nOutBufferSize,
lpBytesReturned,
(LPOVERLAPPED )NULL);
m_file.Close();
if(!tSuccess)
{
Helper::ShowLastError();
}
return tSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -