📄 storageenumview.cpp
字号:
// StorageEnumView.cpp : implementation of the CStorageEnumView class
//
#include "stdafx.h"
#include "StorageEnum.h"
#include "StorageEnumDoc.h"
#include "StorageEnumView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView
IMPLEMENT_DYNCREATE(CStorageEnumView, CEditView)
BEGIN_MESSAGE_MAP(CStorageEnumView, CEditView)
//{{AFX_MSG_MAP(CStorageEnumView)
ON_COMMAND(ID_STORAGE_ENUM, OnStorageEnum)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView construction/destruction
CStorageEnumView::CStorageEnumView()
{
// TODO: add construction code here
}
CStorageEnumView::~CStorageEnumView()
{
}
BOOL CStorageEnumView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView drawing
void CStorageEnumView::OnDraw(CDC* pDC)
{
CStorageEnumDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView printing
BOOL CStorageEnumView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default CEditView preparation
return CEditView::OnPreparePrinting(pInfo);
}
void CStorageEnumView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView begin printing.
CEditView::OnBeginPrinting(pDC, pInfo);
}
void CStorageEnumView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView end printing
CEditView::OnEndPrinting(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView diagnostics
#ifdef _DEBUG
void CStorageEnumView::AssertValid() const
{
CEditView::AssertValid();
}
void CStorageEnumView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CStorageEnumDoc* CStorageEnumView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStorageEnumDoc)));
return (CStorageEnumDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStorageEnumView message handlers
#define MAX_DEVICE 16
DEFINE_GUID(DiskClassGuid, 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
DEFINE_GUID(CdRomClassGuid, 0x53f56308L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(PartitionClassGuid, 0x53f5630aL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
DEFINE_GUID(TapeClassGuid, 0x53f5630bL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(WriteOnceDiskClassGuid, 0x53f5630cL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(VolumeClassGuid, 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(MediumChangerClassGuid, 0x53f56310L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(FloppyClassGuid, 0x53f56311L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(CdChangerClassGuid, 0x53f56312L, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
//DEFINE_GUID(StoragePortClassGuid, 0x2accfe60L, 0xc130, 0x11d2, 0xb0, 0x82, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
CString CStorageEnumView::MakeActualString(LPCTSTR pszMsg)
{
int i;
int len;
BOOL fNeedTranslation;
CString strMsg;
len = ::strlen(pszMsg);
fNeedTranslation = TRUE;
i=0;
while(i<len && fNeedTranslation)
{
if(!(pszMsg[i]>='0' && pszMsg[i]<='9' ||
pszMsg[i]>='A' && pszMsg[i]<='F' ||
pszMsg[i]>='a' && pszMsg[i]<='f'))
{
fNeedTranslation = FALSE;
}
i++;
}
if(fNeedTranslation)
{
WORD word;
for(i=0; i<len; i+=4)
{
::sscanf(&pszMsg[i], "%04x", &word);
strMsg += (char)(word%256);
strMsg += (char)(word/256);
}
}
else
{
strMsg = pszMsg;
}
strMsg.TrimLeft();
strMsg.TrimRight();
return strMsg;
}
CString CStorageEnumView::MakeDriveInfo(LPCTSTR pszDevicePath, PDISK_GEOMETRY pGeometry, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
CString strInfo;
CString strTmp;
char* p;
p= (char*)pDevDesc;
strInfo.Format("\r\nDevice path: %s\r\n", pszDevicePath);
switch(pDevDesc->BusType)
{
case BusTypeScsi: strTmp = "SCSI"; break;
case BusTypeAtapi: strTmp = "ATAPI"; break;
case BusTypeAta: strTmp = "ATA"; break;
case BusType1394: strTmp = "IEEE 1394"; break;
case BusTypeSsa: strTmp = "SSA"; break;
case BusTypeFibre: strTmp = "FIBRE"; break;
case BusTypeUsb: strTmp = "USB"; break;
case BusTypeRAID: strTmp = "RAID"; break;
default: strTmp = "Unkown";
}
strInfo += "Bus type: " + strTmp + "\r\n";
switch(pGeometry->MediaType)
{
case FixedMedia: strTmp = "Disk"; break;
case RemovableMedia: strTmp = "Disk"; break;
case CD_ROM: strTmp = "CD-ROM"; break;
case CD_R: strTmp = "CD-R (Write Once)"; break;
case CD_RW: strTmp = "CD-R/W (Rewriteable)"; break;
case DVD_ROM: strTmp = "DVD-ROM"; break;
case DVD_R: strTmp = "DVD-R (Write Once)"; break;
case DVD_RW: strTmp = "DVD-R/W (Rewriteable)"; break;
default: strTmp = "Unkown";
}
strInfo += "Device type: " + strTmp + "\r\n";
strTmp = (pDevDesc->RemovableMedia ? "Removable" : "Fixed");
strInfo += "Media type: " + strTmp + "\r\n";
strTmp = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset] : "");
strInfo += "Vendor specific: " + strTmp + "\r\n";
strTmp = (pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset] : "");
strInfo += "Model number: " + strTmp + "\r\n";
strTmp = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "");
strInfo += "Firmware revision: " + strTmp + "\r\n";
strTmp = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "");
strInfo += "Serial number: " + MakeActualString(strTmp) + "\r\n";
strTmp.Format("Cylinders = %I64d\r\n", pGeometry->Cylinders);
strInfo+=strTmp;
strTmp.Format("Tracks per cylinder = %ld\r\n", (ULONG) pGeometry->TracksPerCylinder);
strInfo+=strTmp;
strTmp.Format("Sectors per track = %ld\r\n", (ULONG) pGeometry->SectorsPerTrack);
strInfo+=strTmp;
strTmp.Format("Bytes per sector = %ld\r\n", (ULONG) pGeometry->BytesPerSector);
strInfo+=strTmp;
ULONGLONG DiskSize = pGeometry->Cylinders.QuadPart * (ULONGLONG)pGeometry->TracksPerCylinder *
(ULONGLONG)pGeometry->SectorsPerTrack * (ULONGLONG)pGeometry->BytesPerSector;
strTmp.Format("Disk size = %I64d Bytes (%I64d MB)\r\n", DiskSize, DiskSize / (1024 * 1024));
strInfo+=strTmp;
return strInfo;
}
void CStorageEnumView::OnStorageEnum()
{
// TODO: Add your command handler code here
HANDLE hDevice;
PDISK_GEOMETRY pGeometry;
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
CString strInfo;
int nDevice;
int i,j;
char* szDevicePath[MAX_DEVICE]; // 设备路径
LPGUID lpGuid[] = {
(LPGUID)&DiskClassGuid,
(LPGUID)&CdRomClassGuid,
// (LPGUID)&TapeClassGuid,
}; // 一些存储设备的GUID
// 分配需要的空间
for(i=0;i<MAX_DEVICE;i++) szDevicePath[i]=new char[256];
pGeometry = new DISK_GEOMETRY;
pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
// 对感兴趣的类型的存储设备,逐一获取设备信息
for(i=0; i<sizeof(lpGuid)/sizeof(LPGUID); i++)
{
// 取设备路径
nDevice=::GetDevicePath(lpGuid[i], szDevicePath);
// 对同一类的存储设备,逐一获取设备信息
for(j=0; j<nDevice; j++)
{
// 打开设备
hDevice=OpenDevice(szDevicePath[j]);
if(hDevice != INVALID_HANDLE_VALUE)
{
// 取设备信息
GetDriveGeometry(hDevice, pGeometry);
GetDriveProperty(hDevice, pDevDesc);
// 制作显示信息串
strInfo += MakeDriveInfo(szDevicePath[j], pGeometry, pDevDesc);
// 关闭设备
CloseHandle(hDevice);
}
}
}
// 释放空间
delete pDevDesc;
delete pGeometry;
for(i=0;i<MAX_DEVICE;i++) delete []szDevicePath[i];
CEdit& Edit = GetEditCtrl();
Edit.SetWindowText(strInfo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -