Pregunta

¿Cómo se resuelve una ruta de dispositivo de estilo NT, p. \Device\CdRom0, a su letra de unidad lógica, p.e. G:\ ?

Editar:Un nombre de volumen no es lo mismo que una ruta de dispositivo, por lo que lamentablemente GetVolumePathNamesForVolumeName() no funcionará.

¿Fue útil?

Solución

Con suerte, el siguiente fragmento de código le brindará suficiente para resolver esto; después de haberlo inicializado, solo necesita recorrer la colección para encontrar su coincidencia.Es posible que desee convertir todo a mayúsculas/minúsculas antes de insertarlo en la colección para ayudar con el rendimiento de la búsqueda.

typedef basic_string<TCHAR> tstring;
typedef map<tstring, tstring> HardDiskCollection;

void Initialise( HardDiskCollection &_hardDiskCollection )
{
    TCHAR tszLinkName[MAX_PATH] = { 0 };
    TCHAR tszDevName[MAX_PATH] = { 0 };
    TCHAR tcDrive = 0;

    _tcscpy_s( tszLinkName, MAX_PATH, _T("a:") );
    for ( tcDrive = _T('a'); tcDrive < _T('z'); ++tcDrive )
    {
        tszLinkName[0] = tcDrive;
        if ( QueryDosDevice( tszLinkName, tszDevName, MAX_PATH ) )
        {
            _hardDiskCollection.insert( pair<tstring, tstring>( tszLinkName, tszDevName ) );
        }
    }
}

Otros consejos

¿Quizás podrías usar GetVolumeNameForMountPoint e iterar a través de todos los puntos de montaje A:\ a Z:\, rompiendo cuando encuentres una coincidencia?

http://msdn.microsoft.com/en-us/library/aa364994(VS.85).aspx

(No he probado esto)

La siguiente función hace el trabajo usando C solamente

BOOL GetWin32FileName(const TCHAR* pszNativeFileName, TCHAR *pszWin32FileName)
{
    BOOL bFound = FALSE;

    // Translate path with device name to drive letters.
    TCHAR szTemp[MAX_PATH];
    szTemp[0] = '\0';

    if (GetLogicalDriveStrings(MAX_PATH - 1, szTemp))
    {
        TCHAR szName[MAX_PATH];
        TCHAR szDrive[3] = TEXT(" :");
        TCHAR* p = szTemp;

        do
        {
            // Copy the drive letter to the template string
            *szDrive = *p;

            // Look up each device name
            if (QueryDosDevice(szDrive, szName, MAX_PATH))
            {
                size_t uNameLen = _tcslen(szName);

                if (uNameLen < MAX_PATH)
                {
                    bFound = _tcsnicmp(pszNativeFileName, szName, uNameLen) == 0
                        && *(pszNativeFileName + uNameLen) == _T('\\');

                    if (bFound)
                    {
                        // Replace device path with DOS path
                        StringCchPrintf(pszWin32FileName,
                            MAX_PATH,
                            TEXT("%s%s"),
                            szDrive,
                            pszNativeFileName + uNameLen);
                    }
                }
            }
            // Go to the next NULL character.
            while (*p++);
        } while (!bFound && *p);
    }

    return(bFound);
}

Puede buscar el nombre de todos los volúmenes para que coincida con el nombre de un dispositivo y obtener la letra de unidad. Aquí hay un ejemplo:

int DeviceNameToVolumePathName(WCHAR *filepath) {
    WCHAR fileDevName[MAX_PATH];
    WCHAR devName[MAX_PATH];
    WCHAR fileName[MAX_PATH];
    HANDLE FindHandle = INVALID_HANDLE_VALUE;
    WCHAR  VolumeName[MAX_PATH];
    DWORD  Error = ERROR_SUCCESS;
    size_t Index = 0;
    DWORD  CharCount = MAX_PATH + 1;

    int index = 0;
    // \Device\HarddiskVolume1\windows,locate \windows.
    for (int i = 0; i < lstrlenW(filepath); i++) {
        if (!memcmp(&filepath[i], L"\\", 2)) {
            index++;
            if (index == 3) {
                index = i;
                break;
            }
        }
    }
    filepath[index] = L'\0';

    memcpy(fileDevName, filepath, (index + 1) * sizeof(WCHAR));

    FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));

    if (FindHandle == INVALID_HANDLE_VALUE)
    {
        Error = GetLastError();
        wprintf(L"FindFirstVolumeW failed with error code %d\n", Error);
        return FALSE;
    }
    for (;;)
    {
        //  Skip the \\?\ prefix and remove the trailing backslash.
        Index = wcslen(VolumeName) - 1;

        if (VolumeName[0] != L'\\' ||
            VolumeName[1] != L'\\' ||
            VolumeName[2] != L'?' ||
            VolumeName[3] != L'\\' ||
            VolumeName[Index] != L'\\')
        {
            Error = ERROR_BAD_PATHNAME;
            wprintf(L"FindFirstVolumeW/FindNextVolumeW returned a bad path: %s\n", VolumeName);
            break;
        }
        VolumeName[Index] = L'\0';
        CharCount = QueryDosDeviceW(&VolumeName[4], devName, 100);
        if (CharCount == 0)
        {
            Error = GetLastError();
            wprintf(L"QueryDosDeviceW failed with error code %d\n", Error);
            break;
        }
        if (!lstrcmpW(devName, filepath)) {
            VolumeName[Index] = L'\\';
            Error = GetVolumePathNamesForVolumeNameW(VolumeName, fileName, CharCount, &CharCount);
            if (!Error) {
                Error = GetLastError();
                wprintf(L"GetVolumePathNamesForVolumeNameW failed with error code %d\n", Error);
                break;
            }

            // concat drive letter to path
            lstrcatW(fileName, &filepath[index + 1]);
            lstrcpyW(filepath, fileName);

            Error = ERROR_SUCCESS;
            break;
        }

        Error = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));

        if (!Error)
        {
            Error = GetLastError();

            if (Error != ERROR_NO_MORE_FILES)
            {
                wprintf(L"FindNextVolumeW failed with error code %d\n", Error);
                break;
            }

            //
            //  Finished iterating
            //  through all the volumes.
            Error = ERROR_BAD_PATHNAME;
            break;
        }
    }

    FindVolumeClose(FindHandle);
    if (Error != ERROR_SUCCESS)
        return FALSE;
    return TRUE;

}

Si desea resolverlo en el controlador, puede verificar esto enlace para referencia.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top