◆急切求助◆通过DeviceIOControl启用和禁用优盘的USB口问题(在线等)。。。
使用读写注册表的方法,可以禁用和启用优盘的USB口(鼠标键盘除外);
------想通过Win32API函数实现,一直没成功,主要用到的函数有CreateFile和DeviceIOControl;
控制优盘设备主要用到的Disk Management Control Codes 是IOCTL_DISK_PERFORMANCE和IOCTL_DISK_PERFORMANCE_OFF,不知道选择是否正确,这两个参数使用还有问题。。。
请对这块熟悉的朋友提下建议,谢谢。。。
////////////////////////////////////////
bool CUSBTestDlg::OpenUSB(DISK_PERFORMANCE *pdg) //用API函数启用优盘
{
HANDLE hDevice; // handle to the drive to be examined
bool bResult; // results flag
DWORD junk; // discard results
hDevice = CreateFile(
"\\\\.\\H:", //要打开的驱动名称
GENERIC_READ|GENERIC_WRITE, //访问方式
FILE_SHARE_READ|FILE_SHARE_WRITE, //共享方式
NULL, //安全描述符指针
OPEN_EXISTING, //创建方式
0, //文件属性及标志
NULL //模板文件的句柄
);
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_PERFORMANCE, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED)NULL); // synchronous I/O
CloseHandle(hDevice);
return (bResult);
}
////////////////////////////////////////
bool CUSBTestDlg::CloseUSB() //用API函数禁用优盘
{
HANDLE hDevice; // handle to the drive to be examined
bool bResult; // results flag
DWORD junk; // discard results
hDevice = CreateFile(
"\\\\.\\H:", //要打开的驱动名称
GENERIC_READ|GENERIC_WRITE, //访问方式
FILE_SHARE_READ|FILE_SHARE_WRITE, //共享方式
NULL, //安全描述符指针
OPEN_EXISTING, //创建方式
0, //文件属性及标志
NULL //模板文件的句柄
);
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
//IOCTL_DISK_PERFORMANCE_OFF为未定义的变量。。。
bResult = DeviceIoControl(hDevice, IOCTL_DISK_PERFORMANCE_OFF,NULL,0, NULL,0, &junk,(LPOVERLAPPED)NULL);
CloseHandle(hDevice);
return (bResult);
}
////////////////////////////////////////
void CUSBTestDlg::OnButton4() //启用优盘
{
DISK_PERFORMANCE pdg; // disk drive geometry structure
bool bResult = false; // generic results flag
bResult = CUSBTestDlg::OpenUSB (&pdg);
}
////////////////////////////////////////
void CUSBTestDlg::OnButton5() //禁用优盘
{
bool bResult = false; // generic results flag
bResult = CUSBTestDlg::CloseUSB();
}
////////////////////////////////////////
Disk Management Control Codes
IOCTL_DISK_PERFORMANCE
The IOCTL_DISK_PERFORMANCE control code enables performance counters that provide disk performance information.
To perform this operation, call the DeviceIoControl function with the following parameters.
BOOL DeviceIoControl(
(HANDLE) hDevice, // handle to device
IOCTL_DISK_PERFORMANCE, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
(LPVOID) lpOutBuffer, // output buffer
(DWORD) nOutBufferSize, // size of output buffer
(LPDWORD) lpBytesReturned, // number of bytes returned
(LPOVERLAPPED) lpOverlapped // OVERLAPPED structure
);
IOCTL_DISK_PERFORMANCE_OFF
The IOCTL_PERFORMANCE_OFF control code disables the performance counters that provide disk performance information.
To perform this operation, call the DeviceIoControl function with the following parameters.
BOOL DeviceIoControl(
(HANDLE) hDevice, // handle to device
IOCTL_DISK_PERFORMANCE_OFF, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
NULL, // lpOutBuffer
0, // nOutBufferSize
(LPDWORD) lpBytesReturned, // number of bytes returned
(LPOVERLAPPED) lpOverlapped // OVERLAPPED structure
);
////////////////////////////////////////