Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c++ - Using IOCTL_MOUNTMGR_QUERY_POINTS

I am not able to know how to use the IOCTL_MOUNTMGR_QUERY_POINTS .

I have searched the internet and found some sample code to try with.

but i am not sure whether its correct or not....

can you please let me know how to use the IOCTL_MOUNTMGR_QUERY_POINTS to get the drive letter

Thank you for your time

below is my source coode

HANDLE hUsbDevice = CreateFile( pDetData->DevicePath,  
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);

UCHAR Bytes[10000];
PMOUNTMGR_MOUNT_POINTS pMntPoints = (PMOUNTMGR_MOUNT_POINTS) Bytes;
MOUNTMGR_MOUNT_POINT mntPoint, *pmnt;
DWORD bytesReturned;
if (hUsbDevice == INVALID_HANDLE_VALUE) {
    qDebug()<<"CreateFile failed with error: %d
"<<GetLastError();
}
else {
   qDebug ()<<"VALID DEVICE";
   BOOL status = DeviceIoControl( hUsbDevice, 
      IOCTL_MOUNTMGR_QUERY_POINTS,
      &mntPoint,
      sizeof(MOUNTMGR_MOUNT_POINT),
      pMntPoints,
      10000,
      &bytesReturned,
      NULL);

    wprintf(L"BOOL VALUE : %d
", status);
    qDebug ()<<pMntPoints->MountPoints;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK! Here is a code example, but written without any error control to make it shorter:

#include <windows.h>
#include <C:WinDDK7600.16385.1incddkmountmgr.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "Crypt32.lib")

int main()
{
    TCHAR chDrive = 'N', szUniqueId[128];
    TCHAR szDeviceName[7] = _T("\\.");
    HANDLE hDevice, hMountMgr; 
    BYTE byBuffer[1024];
    PMOUNTDEV_NAME pMountDevName;
    DWORD cbBytesReturned, dwInBuffer, dwOutBuffer, ccb;
    PMOUNTMGR_MOUNT_POINT pMountPoint;
    BOOL bSuccess;
    PBYTE pbyInBuffer, pbyOutBuffer;
    LPTSTR pszLogicalDrives, pszDriveRoot;

    // MOUNTMGR_DOS_DEVICE_NAME is defined as L"\\.\MountPointManager"
    hMountMgr = CreateFile (MOUNTMGR_DOS_DEVICE_NAME,
                            0, FILE_SHARE_READ | FILE_SHARE_WRITE,
                            NULL, OPEN_EXISTING, 0, NULL);
    if (hMountMgr == INVALID_HANDLE_VALUE)
        return 1;

    cbBytesReturned = GetLogicalDriveStrings (0, NULL);
    pszLogicalDrives = (LPTSTR) LocalAlloc (LMEM_ZEROINIT,
                                            cbBytesReturned*sizeof(TCHAR));
    cbBytesReturned = GetLogicalDriveStrings (cbBytesReturned,
                                                pszLogicalDrives);
    for (pszDriveRoot = pszLogicalDrives; *pszDriveRoot != TEXT('');
            pszDriveRoot += lstrlen(pszDriveRoot) + 1) {

        szDeviceName[4] = pszDriveRoot[0];
        szDeviceName[5] = _T(':');
        szDeviceName[6] = _T('');
        //lstrcpy (&szDeviceName[4], TEXT("\??\USBSTOR\DISK&VEN_SANDISK&PROD_CRUZER&REV_8.01\1740030578903736&0"));

        hDevice = CreateFile (szDeviceName, 0,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL, OPEN_EXISTING, 0, NULL);
        if (hDevice == INVALID_HANDLE_VALUE)
            return 1;

        bSuccess = DeviceIoControl (hDevice,
                                    IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, 
                                    NULL, 0,
                                    (LPVOID)byBuffer, sizeof(byBuffer),
                                    &cbBytesReturned,
                                    (LPOVERLAPPED) NULL);
        pMountDevName = (PMOUNTDEV_NAME) byBuffer;
        _tprintf (TEXT("
%.*ls
"), pMountDevName->NameLength/sizeof(WCHAR),
                                        pMountDevName->Name);
        bSuccess = CloseHandle (hDevice);

        dwInBuffer = pMountDevName->NameLength + sizeof(MOUNTMGR_MOUNT_POINT);
        pbyInBuffer = (PBYTE) LocalAlloc (LMEM_ZEROINIT, dwInBuffer);
        pMountPoint = (PMOUNTMGR_MOUNT_POINT) pbyInBuffer;
        pMountPoint->DeviceNameLength = pMountDevName->NameLength;
        pMountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
        CopyMemory (pbyInBuffer + sizeof(MOUNTMGR_MOUNT_POINT),
                    pMountDevName->Name, pMountDevName->NameLength);

        dwOutBuffer = 1024 + sizeof(MOUNTMGR_MOUNT_POINTS);
        pbyOutBuffer = (PBYTE) LocalAlloc (LMEM_ZEROINIT, dwOutBuffer);
        bSuccess = DeviceIoControl (hMountMgr,
                                    IOCTL_MOUNTMGR_QUERY_POINTS, 
                                    pbyInBuffer, dwInBuffer,
                                    (LPVOID)pbyOutBuffer, dwOutBuffer,
                                    &cbBytesReturned,
                                    (LPOVERLAPPED) NULL);
        if (bSuccess) {
            ULONG i;
            PMOUNTMGR_MOUNT_POINTS pMountPoints = (PMOUNTMGR_MOUNT_POINTS) pbyOutBuffer;
            for (i=0; i<pMountPoints->NumberOfMountPoints; i++) {
                _tprintf (TEXT("#%i:
"), i);
                _tprintf (TEXT("    Device=%.*ls
"),
                        pMountPoints->MountPoints[i].DeviceNameLength/sizeof(WCHAR),
                        pbyOutBuffer + pMountPoints->MountPoints[i].DeviceNameOffset);
                _tprintf (TEXT("    SymbolicLink=%.*ls
"),
                        pMountPoints->MountPoints[i].SymbolicLinkNameLength/sizeof(WCHAR),
                        pbyOutBuffer + pMountPoints->MountPoints[i].SymbolicLinkNameOffset);
                ccb = sizeof(szUniqueId)/sizeof(szUniqueId[0]);

                if (CryptBinaryToString (pbyOutBuffer + pMountPoints->MountPoints[i].UniqueIdOffset,
                                        pMountPoints->MountPoints[i].UniqueIdLength,
                                        CRYPT_STRING_BASE64, 
                                        szUniqueId, &ccb))
                    _tprintf (TEXT("    UniqueId=%s
"), szUniqueId);
                else
                    _tprintf (TEXT("    UniqueId=%.*ls
"),
                            pMountPoints->MountPoints[i].UniqueIdLength/sizeof(WCHAR),
                            pbyOutBuffer + pMountPoints->MountPoints[i].UniqueIdOffset);
            }
        }
        pbyInBuffer = (PBYTE) LocalFree (pbyInBuffer);
        pbyOutBuffer = (PBYTE) LocalFree (pbyOutBuffer);
    }
    pszLogicalDrives = (LPTSTR) LocalFree (pszLogicalDrives);
    bSuccess = CloseHandle (hMountMgr);

    return 0;
}

if produce on my computer the output like

DeviceHarddiskVolume3
#0:
    Device=DeviceHarddiskVolume3
    SymbolicLink=DosDevicesC:
    UniqueId=O5TWlQAAwBRIAAAA
#1:
    Device=DeviceHarddiskVolume3
    SymbolicLink=??Volume{12703dc4-bf56-11db-8c6c-806e6f6e6963}
    UniqueId=O5TWlQAAwBRIAAAA

...

DeviceCdRom2
#0:
    Device=DeviceCdRom2
    SymbolicLink=DosDevicesL:
    UniqueId=??USBSTOR#CdRom&Ven_HL-DT-ST&Prod_DVDRAM_GE20LU11&Rev_CL01#0010101640008B615&0#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}
#1:
    Device=DeviceCdRom2
    SymbolicLink=??Volume{2c5f6a93-2b50-11df-aa6a-005056c00008}
    UniqueId=??USBSTOR#CdRom&Ven_HL-DT-ST&Prod_DVDRAM_GE20LU11&Rev_CL01#0010101640008B615&0#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}

DeviceHarddiskVolume8
#0:
    Device=DeviceHarddiskVolume8
    SymbolicLink=DosDevicesN:
    UniqueId=_??_USBSTOR#Disk&Ven_SanDisk&Prod_Cruzer&Rev_8.01#1740030578903736&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
#1:
    Device=DeviceHarddiskVolume8
    SymbolicLink=??Volume{ae08a3c8-71cf-11de-bc1d-005056c00008}
    UniqueId=_??_USBSTOR#Disk&Ven_SanDisk&Prod_Cruzer&Rev_8.01#1740030578903736&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...