#include "StdAfx.h"
#include "GetDeviceMessage.h"
//
// 获取光驱信息函数
// 参数:一: CreateFile打开的句柄
// 二: 输入结构体
// 三: 输入结构体的大小
// 四: 返回输出结构体
// 五: 输出结构体的大小
// 六: 函数得到信息后将信息做何操作
//
bool carry_cdb(HANDLE device, void *cdb, UCHAR cdb_length, void *buffer, DWORD buffer_length, int data_in)
{
OutputDebugString("carry_cdb开始执行");
DWORD returned;
// size of SCSI_PASS_THROUGH + 96 bytes for sense data
unsigned char cmd[sizeof(SCSI_PASS_THROUGH_DIRECT) + 96] = {0};
// shortcut to the buffer
SCSI_PASS_THROUGH_DIRECT *pcmd = (SCSI_PASS_THROUGH_DIRECT *) cmd;
// Copy the CDB to the SCSI_PASS_THROUGH structure
::memcpy(pcmd->Cdb, cdb, cdb_length);
pcmd->DataBuffer = buffer;
pcmd->DataTransferLength = buffer_length;
pcmd->DataIn = data_in;
pcmd->CdbLength = cdb_length;
pcmd->Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
pcmd->SenseInfoLength = sizeof(cmd) - sizeof(SCSI_PASS_THROUGH_DIRECT);
pcmd->SenseInfoOffset = sizeof(SCSI_PASS_THROUGH_DIRECT);
pcmd->TimeOutValue = 6000;
OutputDebugString("传入参数获取成功");
BOOL bRet = DeviceIoControl(
device,
IOCTL_SCSI_PASS_THROUGH_DIRECT,
(LPVOID)&cmd,
sizeof(cmd),
(LPVOID)&cmd,
sizeof(cmd),
&returned,
NULL);
if (bRet)
{
OutputDebugString("信息获取成功");
return TRUE;
}
else
{
return FALSE;
}
}
//
// 根据所传盘符获得光驱信息保存进参数二的结构体(所传盘符格式以GetDriveType返回为基准,例如(G:\\))
// 返回:-1 打开句柄失败
// -2 获取信息失败
// 0 函数执行成功
//
int GetDeviceMessage(PCSTR str, CD_DRIVER_MESSAGE *strDeviceMessage)
{
OutputDebugString("GetDeviceMessage");
// 获得CreateFile可打开文件名
CString strDevname;
strDevname.Format("\\\\.\\%s", str);
strDevname= strDevname.Left(strDevname.GetLength()-1);
OutputDebugString(strDevname);
HANDLE device = ::CreateFile(strDevname,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
0);
// 打开句柄失败
if (device == (HANDLE)-1)
{
return -1;
}
OutputDebugString("获取句柄成功");
// 判定需要查询什么的结构体
CDB_INQUIRY6 inquiry = {0};
// 保存光驱信息的结构
SCSI_INQUIRY_STD_DATA data = {0};
inquiry.AllocationLength = sizeof(data);
inquiry.OperationCode6 = SCSIOP_INQUIRY;
if (!carry_cdb(device, &inquiry, sizeof(inquiry), &data, sizeof(data)))
{
OutputDebugString("获取信息失败");
::CloseHandle(device);
return -2;
}
CString s;
char snip[100] = {0};
CD_DRIVER_MESSAGE strDevieMsg;
// 将当前盘符保存进结构体
strncpy(strDeviceMessage->letter ,str, 31);
OutputDebugString(strDeviceMessage->letter);
int i;
strncpy(snip, data.vendor_id, i = sizeof(data.vendor_id));
snip[i] =0;
s.Format("%s", snip);
// strDeviceMessage->vendor_id += s;
strncpy(strDeviceMessage->vendor_id ,s, 31);
OutputDebugString(strDeviceMessage->vendor_id);
strncpy(snip, data.product_id , i = sizeof(data.product_id));
snip[i] = 0;
s.Format("%s", snip);
strncpy(strDeviceMessage->product_id ,s, 31);
// strDeviceMessage->product_id += s;
OutputDebugString(strDeviceMessage->product_id);
strncpy(snip, data.product_revision_level , i = sizeof(data.product_revision_level));
snip[i] = 0;
s.Format("%s", snip);
strncpy(strDeviceMessage->product_revision_level ,s, 63);
// strDeviceMessage->product_revision_level += s;
OutputDebugString(strDeviceMessage->product_revision_level);
CloseHandle(device);
OutputDebugString("GetDeviceMessage执行成功");
return 0;
}
//
// 获得有盘的光驱的信息(函数内部自动获取有盘光驱是哪些)
// 返回有盘光驱个数(>=0都算成功)
// 返回-1 代表打开该光驱句柄失败
// 返回-2 代表读取该光驱信息失败
//
int UpdateDeviceInfos(CD_DRIVER_MESSAGE* strDeviceMessage)
{
int iNumber = 0; // 保存正在读盘的光驱个数
// 获得当前光驱的盘符保存进CStringArray
TCHAR buf[100];
DWORD len = GetLogicalDriveStrings(sizeof(buf)/sizeof(TCHAR),buf);
LPCTSTR sDrivePath = NULL;
CStringArray strArray;
for (TCHAR* s=buf; *s; s+=_tcslen(s)+1)
{
LPCTSTR sDrivePath = s;
UINT uDriveType = GetDriveType(sDrivePath);
if (uDriveType == DRIVE_CDROM)
{
strArray.Add(sDrivePath);
}
}
// 根据盘符获取光驱信息
for(int j= 0; j<strArray.GetSize(); j++)
{
// 判断光驱是否正在使用
if(GetVolumeInformation(strArray.GetAt(j), NULL,0,NULL,NULL,0,NULL,0))
{
// 正在使用 获取光驱信息保存进结构体数组
int iFlag = GetDeviceMessage(strArray.GetAt(j), &strDeviceMessage[iNumber]);
// 判断执行状况
if (iFlag != 0)
{
return iFlag;
}
iNumber++;
}
}
// 返回有盘光驱数
return iNumber;
}
没有合适的资源?快使用搜索试试~ 我知道了~
获得“光驱和刻录机中光盘”的信息
共5个文件
h:3个
cpp:2个
5星 · 超过95%的资源 需积分: 18 14 下载量 153 浏览量
2012-08-18
11:40:16
上传
评论 1
收藏 8KB RAR 举报
温馨提示
主要通过DeviceIocontrol和一些系统API获取刻录机和刻录机中光盘的信息,其中包含头文件和源文件,加入工程后即可使用,获取光盘信息的函数在刻录光盘时因为系统资源被占用有一个函数失败,因仓促写成,这个函数中流程有点问题,其他的函数还是成功的,各位稍稍修理下即可使用。
资源推荐
资源详情
资源评论
收起资源包目录
获得“光驱和刻录机中光盘”的信息.rar (5个子文件)
获得“光驱和刻录机中光盘”的信息
获得光盘信息
GetDiscMessage.cpp 4KB
GetDiscMessage.h 618B
获得光驱信息
GetDeviceMessage.h 912B
GetDeviceMessage.cpp 5KB
scsi_struct.h 5KB
共 5 条
- 1
资源评论
- zhh680_yatou2013-06-28代码很全,结构很清晰,非常感谢,我找这部分资源很久。
- shiyangning2013-08-17下了还没来的及编译.不过看代码还是比较整齐的。应该不是骗分的...
码海小舟
- 粉丝: 2
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功