Question

Je veux des clés USB de liste dans ma machine. Comment faire en VC ++. U peut donner un exemple de code.

Était-ce utile?

La solution

Je ne pense pas que vous allez trouver quelqu'un pour écrire le code pour vous. Vous êtes un programmeur, c'est (probablement) votre travail

Cependant, vous pouvez commencer par GetLogicalDriveStrings et GetDriveType .

Autres conseils

Selon la documentation de GetDriveType il dit que nous devrions utiliser SetupDiGetDeviceRegistryProperty pour, et je cite:

  

Pour déterminer si un lecteur est un lecteur USB de type, SetupDiGetDeviceRegistryProperty d'appel et spécifiez la propriété SPDRP_REMOVAL_POLICY.

J'ai couru quelques tests et ne pouvait pas trouver aucune indication d'un certain dispositif étant une clé USB. retourne SPDRP_REMOVAL_POLICY 2 pour de nombreux appareils (ainsi que mes lecteurs USB) donc je ne peux pas utiliser vraiment. Appel SetupDiGetDeviceRegistryProperty avec SPDRP_CAPABILITIES et le filtre ne CM_DEVCAP_REMOVABLE donne également de nombreux appareils (même en combinaison avec la politique de suppression ne donne aucune bonne indication comment trouver mes lecteurs USB. En outre, appeler SetupDiGetDeviceRegistryProperty avec SPDRP_DEVTYPE renvoie toujours une erreur 13 ( « Les données sont invalides. ») Et je ne sais pas pourquoi.

Voici un code:

void SetupDiInformation()
{
    HDEVINFO hDevInfo = SetupDiGetClassDevsW(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
    if (INVALID_HANDLE_VALUE == hDevInfo)
    {
        fwprintf(stderr, L"Error SetupDiCreateDeviceInfoList: %d\n", GetLastError());
        return;
    }

    SP_DEVINFO_DATA devInfoData;
    devInfoData.cbSize = sizeof(devInfoData);
    BOOL success;
    success = SetupDiEnumDeviceInfo(hDevInfo, 0, &devInfoData);
    for (int i=1; success; i++)
    {
        DWORD regDataType = REG_NONE, reqSize = 0;
        WCHAR deviceDesc[MAX_PATH+1] = {0};
        DWORD deviceType = -1, capabilities = -1;
        DWORD removalPolicy = CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL;
        BOOL regPropSuccess = false;

/*
        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_DEVTYPE, &regDataType, 
            (PBYTE)&deviceType, sizeof(deviceType), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_DEVTYPE)[%d]: %d\n", i, GetLastError());
        }
*/

        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_DEVICEDESC, &regDataType, 
            (PBYTE)deviceDesc, sizeof(deviceDesc), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_DEVICEDESC)[%d]: %d\n", i, GetLastError());
        }


        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_CAPABILITIES, &regDataType, 
            (PBYTE)&capabilities, sizeof(capabilities), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_CAPABILITIES)[%d]: %d\n", i, GetLastError());
        }  

        regDataType = REG_NONE; reqSize = 0;
        regPropSuccess = SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, 
            SPDRP_REMOVAL_POLICY, &regDataType, 
            (PBYTE)&removalPolicy, sizeof(removalPolicy), &reqSize);
        if (!regPropSuccess)
        {
            fwprintf(stderr, L"Error SetupDiGetDeviceRegistryProperty(SPDRP_REMOVAL_POLICY)[%d]: %d\n", i, GetLastError());
        }  

        if ((CM_DEVCAP_REMOVABLE & capabilities) != 0)
        {
            wprintf(L"% 4d. ", i);
            wprintf(L"%X-%04X-%X-", 
                devInfoData.ClassGuid.Data1, 
                devInfoData.ClassGuid.Data2, 
                devInfoData.ClassGuid.Data3);
            int data4len = sizeof(devInfoData.ClassGuid.Data4)/sizeof(devInfoData.ClassGuid.Data4[0]);
            for (int j=0; j<data4len; j++)
                wprintf(L"%02X", devInfoData.ClassGuid.Data4[j]);

            if (wcslen(deviceDesc) > 30)
                deviceDesc[30]=L'\0';
            //wprintf(L" %-8d%-30s 0x%08X %d [%d] ", devInfoData.DevInst, deviceDesc, deviceType, removalPolicy, capabilities);
            wprintf(L" %-8d%-30s %d [%d] ", devInfoData.DevInst, deviceDesc, removalPolicy, capabilities);
            //DisplayCapabilities(capabilities);
            wprintf(L"\n");
        }

        success = SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData);
    }

    DWORD lastError = GetLastError();
    if (lastError != ERROR_NO_MORE_ITEMS)
    {
        // error occurred
        fwprintf(stderr, L"Error SetupDiEnumDeviceInfo: %d\n", lastError);
    }

    if (!SetupDiDestroyDeviceInfoList(hDevInfo))
    {
        fwprintf(stderr, L"Error SetupDiDestroyDeviceInfoList: %d\n", GetLastError());
        return;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top