码迷,mamicode.com
首页 > 其他好文 > 详细

遍历所有盘符和对应的设备

时间:2019-01-21 12:07:09      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:ntp   图片   root   master   init   include   seh   ica   image   

mountmgr.h下载
https://github.com/haidragon/wrk-v1.2/tree/master/public/sdk/inc/mountmgr.h

#include "stdafx.h"
#include <windows.h>
#include "mountmgr.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(‘\0‘);
        pszDriveRoot += lstrlen(pszDriveRoot) + 1) {

        szDeviceName[4] = pszDriveRoot[0];
        szDeviceName[5] = _T(‘:‘);
        szDeviceName[6] = _T(‘\0‘);
        //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;

        _tprintf(TEXT("szDeviceName %s\n"), szDeviceName);
        bSuccess = DeviceIoControl(hDevice,
            IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
            NULL, 0,
            (LPVOID)byBuffer, sizeof(byBuffer),
            &cbBytesReturned,
            (LPOVERLAPPED)NULL);
        pMountDevName = (PMOUNTDEV_NAME)byBuffer;
        _tprintf(TEXT("\n%.*ls\n"), 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:\n"), i);
                _tprintf(TEXT("    Device=%.*ls\n"),
                    pMountPoints->MountPoints[i].DeviceNameLength / sizeof(WCHAR),
                    pbyOutBuffer + pMountPoints->MountPoints[i].DeviceNameOffset);
                _tprintf(TEXT("    SymbolicLink=%.*ls\n"),
                    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\n"), szUniqueId);
                else
                    _tprintf(TEXT("    UniqueId=%.*ls\n"),
                        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;
}

技术分享图片技术分享图片

遍历所有盘符和对应的设备

标签:ntp   图片   root   master   init   include   seh   ica   image   

原文地址:http://blog.51cto.com/haidragon/2344874

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!