获取驱动器的设备号失败

获取驱动器的设备号失败

本文介绍了获取驱动器的设备号失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试获取驱动器号的设备号。我有以下代码来获取设备号。在我的机器上调试它工作正常。但是我使用这个exe来制作可启动的USB设备来擦除数据。但是那个时候设备的信件来了
不同。请参阅以下屏幕截图。这是我在调试代码时得到的输出。它工作正常。设备编号为0. 





我在使用相同的exe来擦除数据的同时使用可启动USB重启系统时得到的以下输出。但这里的设备号是1.使用相同的系统和相同的代码,但唯一使用此exe创建可启动设备。 





无法追踪问题。要获得具有以下代码的驱动器号的设备编号。

   vector    <   PDRIVE_LETTER_TO_DEVICE_INFO  >     *   driveLetterToDeviceInfo   =       矢量 <   PDRIVE_LETTER_TO_DEVICE_INFO  > ;;   

无效 DriveUtilities :: GetDriveLetterToDeviceInfo vector < PDRIVE_LETTER_TO_DEVICE_INFO > * driveLetterToDeviceInfo

{

DWORD结果
= GetLogicalDrives ();

CString driveLetterArray = _T " ABCDEFGHIJKLMNOPQRSTUVWXYZ" );

DWORD bitMask
= 0x1 ;

// vector< HANDLE> driveHandles;

int i = 0 ; i < 26 ; ++)
{

如果 (( bitMask & result != 0
{

//发现它们所在的驱动器
CString pathToOpenDrive = _T " \\\ \\\\。\\" );
pathToOpenDrive
追加 driveLetterArray Mid i 1 ));
pathToOpenDrive
追加 _ T ":" ));

LPDWORD bytesreturned
= DWORD ;

HANDLE targetDrive
= CreateFile pathToOpenDrive 0 0 空白 OPEN_EXISTING 0 空白 );
如果 targetDrive != INVALID_HANDLE_VALUE
{

PSTORAGE_DEVICE_NUMBER myDeviceNumber
= STORAGE_DEVICE_NUMBER ;

PDRIVE_LETTER_TO_DEVICE_INFO myLetterToDeviceInfo
= DRIVE_LETTER_TO_DEVICE_INFO ;

如果 DeviceIoControl targetDrive IOCTL_STORAGE_GET_DEVICE_NUMBER NULL 0 myDeviceNumber sizeof STORAGE_DEVICE_NUMBER ), bytesreturned 空白 ))
{


myLetterToDeviceInfo
- > DeviceInfo = STORAGE_DEVICE_NUMBER ;
myLetterToDeviceInfo
- > DeviceInfo - > DeviceNumber = myDeviceNumber - > DeviceNumber ;
myLetterToDeviceInfo
- > DeviceInfo - > DeviceType = myDeviceNumber - > DeviceType ;
myLetterToDeviceInfo
- > DeviceInfo - > PartitionNumber = myDeviceNumber - > PartitionNumber ;

myLetterToDeviceInfo
- > DriveLetter = driveLetterArray i 1 );
fWriteToLog
_ T "驱动器号:%s \ n" ), TRUE myLetterToDeviceInfo - > DriveLetter );

driveLetterToDeviceInfo
- > push_back myLetterToDeviceInfo );


}
其他
{
DWORD lastError
= GetLastError ();
fWriteToLog
_ T "尝试获取设备编号时出错。上次错误:%08x \ n" ), bDebug lastError );
_tprintf
_ T "尝试获取设备编号时出错。上次错误:%08x \ n" ), lastError );

}

//抓取音量信息
LPTSTR bufVolumeName
= TCHAR [ MAX_PATH + 1 ];
LPTSTR bufFSName
= TCHAR [ MAX_PATH + 1 ];

如果 GetVolumeInformation driveLetterArray 中间 i 1 + _T ":\ \" ),
bufVolumeName

MAX_PATH
+ 1
NULL
空白 空白
bufFSName

MAX_PATH
+ 1 ))
{
myLetterToDeviceInfo
- > VolumeName = bufVolumeName ;
myLetterToDeviceInfo
- > FSName = bufFSName ;
_tprintf
_ T "卷名:%s文件系统名称:%s \ n" ), bufVolumeName bufFSName );
fWriteToLog
_ T "卷名:%s文件系统名称:%s \ n" ), TRUE bufVolumeName bufFSName );
删除 bufVolumeName ;
删除 bufFSName ;
}
其他
{
DWORD lastError
= GetLastError ();
fWriteToLog
_ T "获取音量信息时出错。上次错误:%08x \ n" ), bDebug lastError );
_tprintf
_ T "获取音量信息时出错。上次错误:%08x \ n" ), lastError );

}

CloseHandle targetDrive );

删除 myDeviceNumber ;
删除 bytesreturned ;
}

}
bitMask
= bitMask << 1 ;
}

}





它工作正常。但是对于某些COE,它会返回错误的设备号。请帮我解决此问题。 




Rani manickam

解决方案

I am trying to get the device number for the drive letter. i have below code for getting the device number. while debugging the in my machine it works fine. but i making bootable USB device using this exe to wipe the data. but that time the device letter comes different. Please refer the below screenshot. This is output i am getting while debugging the code. it works fine. the device number is 0. 

The below out put i am getting while rebooting the system with bootable USB using the same exe to wipe the data. but here the device number is 1. using same system and same code but the only creating bootable device using this exe. 

Not able to trace the problem. To get the device number for the drive letter i m having the below code.

vector <PDRIVE_LETTER_TO_DEVICE_INFO> *driveLetterToDeviceInfo = new vector<PDRIVE_LETTER_TO_DEVICE_INFO>;

     void DriveUtilities::GetDriveLetterToDeviceInfo(vector <PDRIVE_LETTER_TO_DEVICE_INFO> *driveLetterToDeviceInfo)

    {

      DWORD result = GetLogicalDrives();

      CString driveLetterArray = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

      DWORD bitMask = 0x1;

      //vector <HANDLE> driveHandles;

      for (int i = 0; i < 26; i++)
      {

    if ((bitMask & result) != 0)
    {

      //discover what drive they lie on
      CString pathToOpenDrive = _T("\\\\.\\");
      pathToOpenDrive.Append(driveLetterArray.Mid(i, 1));
      pathToOpenDrive.Append(_T(":"));

      LPDWORD bytesreturned = new DWORD;

      HANDLE targetDrive = CreateFile(pathToOpenDrive, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
      if (targetDrive != INVALID_HANDLE_VALUE)
      {

        PSTORAGE_DEVICE_NUMBER myDeviceNumber = new STORAGE_DEVICE_NUMBER;

        PDRIVE_LETTER_TO_DEVICE_INFO myLetterToDeviceInfo = new DRIVE_LETTER_TO_DEVICE_INFO;

        if (DeviceIoControl(targetDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, myDeviceNumber, sizeof(STORAGE_DEVICE_NUMBER), bytesreturned, NULL))
        {


          myLetterToDeviceInfo->DeviceInfo = new STORAGE_DEVICE_NUMBER;
          myLetterToDeviceInfo->DeviceInfo->DeviceNumber = myDeviceNumber->DeviceNumber;
          myLetterToDeviceInfo->DeviceInfo->DeviceType = myDeviceNumber->DeviceType;
          myLetterToDeviceInfo->DeviceInfo->PartitionNumber = myDeviceNumber->PartitionNumber;

          myLetterToDeviceInfo->DriveLetter = driveLetterArray.Mid(i, 1);
          fWriteToLog(_T("Drive Letter:%s\n"), TRUE, myLetterToDeviceInfo->DriveLetter);

          driveLetterToDeviceInfo->push_back(myLetterToDeviceInfo);


        }
        else
        {
          DWORD lastError = GetLastError();
          fWriteToLog(_T("Error when trying to get device number. Last error:%08x\n"), bDebug, lastError);
          _tprintf(_T("Error when trying to get device number. Last error:%08x\n"),lastError);

        }

        //grab volume info 
        LPTSTR bufVolumeName = new TCHAR[MAX_PATH + 1];
        LPTSTR bufFSName = new TCHAR[MAX_PATH + 1];

        if (GetVolumeInformation(driveLetterArray.Mid(i, 1) + _T(":\\"),
          bufVolumeName,
          MAX_PATH + 1,
          NULL, NULL, NULL,
          bufFSName,
          MAX_PATH + 1))
        {
          myLetterToDeviceInfo->VolumeName = bufVolumeName;
          myLetterToDeviceInfo->FSName = bufFSName;
          _tprintf(_T("Volume Name:%s File System Name: %s\n"), bufVolumeName, bufFSName);
          fWriteToLog(_T("Volume Name:%s File System Name: %s\n"), TRUE, bufVolumeName, bufFSName);
          delete bufVolumeName;
          delete bufFSName;
        }
        else
        {
          DWORD lastError = GetLastError();
          fWriteToLog(_T("Error when getting volume info. Last error:%08x\n"), bDebug, lastError);
          _tprintf(_T("Error when getting volume info. Last error:%08x\n"), lastError);

        }

        CloseHandle(targetDrive);

        delete myDeviceNumber;
        delete bytesreturned;
      }

    }
        bitMask = bitMask << 1;
  }

}

It works fine. but with some COE it returns wrong device number. Please help me to resolve this issue. 


Rani manickam

解决方案


这篇关于获取驱动器的设备号失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:54