U盘检测的程序

#include "stdafx.h"
#include "test.h"
#include "usbhead.h"

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;

 // initialize MFC and print and error on failure
 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  cerr << _T("Fatal Error: MFC initialization failed") << endl;
  nRetCode = 1;
 }
 else
 {
  // TODO: code your application's behavior here.

  int i;

  CString   str;
  HKEY hkey;
  CStringArray strArrayInfo;   //存放移动硬盘的信息
  CStringArray strArrayDrive;
  DWORD dwtype,sl = 256;
  char sz[256];


  //首先通过注册表检测,最多检测到8个移动硬盘
  for(i=1;i<8;i++)
  {
   if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM//CurrentControlSet//Services//Disk//Enum",/
    NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS)
   {
    CString id;
    id.Format("%d",i);
    if(::RegQueryValueEx(hkey,id,NULL,&dwtype,(LPBYTE)sz,&sl)==ERROR_SUCCESS)
    {
     strArrayInfo.Add(sz);
     cout<<sz<<endl;


     strArrayDrive.Add("////.//PHYSICALDRIVE"+id);
    }
   }
  }

 

 
  //类据移动硬盘的属性
  for (i=0;i < strArrayDrive.GetSize();i++)
  {
   CString strDrive = strArrayDrive[i];
   HANDLE hDevice=CreateFile(strDrive,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
   if(hDevice==INVALID_HANDLE_VALUE)
   {  
    AfxMessageBox("Error!");
    continue;
   }


   
   PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];

   pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;

   if(GetDisksProperty(hDevice, pDevDesc))
   {
    char *p =NULL;

    cout<<"/r/n找到移动盘/n/n"<<endl;


    p= (char*)pDevDesc;
    str = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)"); //名称
    cout<<"名称:/t"<<(const char *)str<<endl;


    str=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)"); //产品ID
    cout<<"产品ID:/t"<<(const char *)str<<endl;

    str = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)"); //
    cout<<"n产品版本:/t"<<(const char *)str<<endl;

 

    str = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");
    cout<<"n序列号:/t"<<(const char *)str<<"/n/n";

   }

 

  }

 

  //列举移动硬盘的盘符usbhead.h
  for( TCHAR cLetter = _T('C'); cLetter <= _T('Z'); cLetter++ )
  {
   CString sDrive (cLetter);
   sDrive += _T("://");
   if(GetDriveType(sDrive)==DRIVE_REMOVABLE)
   {
    cout<<(const char *)sDrive<<endl;
    
   }
  }

  cout<<endl<<endl;
 }

 return nRetCode;
}

 

BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
 STORAGE_PROPERTY_QUERY Query; 
 DWORD dwOutBytes;    
 BOOL bResult;     

 Query.PropertyId = StorageDeviceProperty;
 Query.QueryType = PropertyStandardQuery;

 bResult = ::DeviceIoControl(hDevice,   
   IOCTL_STORAGE_QUERY_PROPERTY,   
   &Query, sizeof(STORAGE_PROPERTY_QUERY), 
   pDevDesc, pDevDesc->Size,    
   &dwOutBytes,       
   (LPOVERLAPPED)NULL);     

 return bResult;
}
 

 

用到的头文件
#include "devioctl.h"
#include "usbdi.h"
#include <dbt.h>      
#include <winioctl.h>     

extern "C" {
#include "hidsdi.h"
#include <setupapi.h>
}
#define DRVUNKNOWN  0
#define DRVFIXED  1
#define DRVREMOTE  2
#define DRVRAM   3
#define DRVCD   4
#define DRVREMOVE  5
typedef enum _STORAGE_BUS_TYPE {
    BusTypeUnknown = 0x00,
    BusTypeScsi,
    BusTypeAtapi,
    BusTypeAta,
    BusType1394,
    BusTypeSsa,
    BusTypeFibre,
    BusTypeUsb,
    BusTypeRAID,
    BusTypeMaxReserved = 0x7F
} STORAGE_BUS_TYPE, *PSTORAGE_BUS_TYPE;
typedef struct _STORAGE_DEVICE_DESCRIPTOR {
  ULONG  Version;
  ULONG  Size;
  UCHAR  DeviceType;
  UCHAR  DeviceTypeModifier;
  BOOLEAN  RemovableMedia;
  BOOLEAN  CommandQueueing;
  ULONG  VendorIdOffset;
  ULONG  ProductIdOffset;
  ULONG  ProductRevisionOffset;
  ULONG  SerialNumberOffset;
  STORAGE_BUS_TYPE  BusType;
  ULONG  RawPropertiesLength;
  UCHAR  RawDeviceProperties[1];

} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR;
typedef enum _STORAGE_QUERY_TYPE {
  PropertyStandardQuery = 0,
  PropertyExistsQuery,
  PropertyMaskQuery,
  PropertyQueryMaxDefined

} STORAGE_QUERY_TYPE, *PSTORAGE_QUERY_TYPE;

typedef enum _STORAGE_PROPERTY_ID {
  StorageDeviceProperty = 0,
  StorageAdapterProperty,
  StorageDeviceIdProperty

} STORAGE_PROPERTY_ID, *PSTORAGE_PROPERTY_ID;

typedef struct _STORAGE_PROPERTY_QUERY {
  STORAGE_PROPERTY_ID  PropertyId;
  STORAGE_QUERY_TYPE  QueryType;
  UCHAR  AdditionalParameters[1];

} STORAGE_PROPERTY_QUERY, *PSTORAGE_PROPERTY_QUERY;

typedef struct _STRING_DESCRIPTOR_NODE
{
    struct _STRING_DESCRIPTOR_NODE *Next;
    UCHAR                           DescriptorIndex;
    USHORT                          LanguageID;
    USB_STRING_DESCRIPTOR           StringDescriptor[0];
} STRING_DESCRIPTOR_NODE, *PSTRING_DESCRIPTOR_NODE;
extern void WINAPI HIDIOCompletionRoutine(DWORD, DWORD, LPOVERLAPPED);
#define IOCTL_STORAGE_QUERY_PROPERTY   CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)

你可能感兴趣的:(struct,String,null,query,Descriptor,initialization)