why I invoke SetupDiCallClassInstaller,it returns false,and GetlastError return 13.How Can I find the reseaon ? what causes the invalid data?

Codelily 1 Reputation point
2021-07-08T08:44:22.287+00:00

SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
params.Scope = DICS_FLAG_GLOBAL;
params.HwProfile = 0;
if (enable)
{
params.StateChange = DICS_ENABLE;
}
else
{
params.StateChange = DICS_DISABLE;
}

            if (!SetupDiSetClassInstallParams(hDevInfo, &devInfoData, (SP_CLASSINSTALL_HEADER*)&params, sizeof(SP_PROPCHANGE_PARAMS)))
            {
                printf(" Camera: %s SetupDiSetClassInstallParams GetLastError: %d \n", currentCameraName, GetLastError());
                return false;
            }

            if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &devInfoData))
            {
                printf(" Camera: %s SetupDiCallClassInstaller GetLastError: %d \n ", currentCameraName, GetLastError());
                return false;
            }

            SP_DEVINSTALL_PARAMS devParams;
            devParams.cbSize = sizeof(devParams);
            if (!SetupDiGetDeviceInstallParams(hDevInfo, &devInfoData, &devParams))
            {
                //log
                printf(" Camera: %s SetupDiGetDeviceInstallParams GetLastError: %d \n ", currentCameraName, GetLastError());
                return false;
            }

            if (devParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
            {
                //log
                printf("about change Camera: %s  system need to Restart ", currentCameraName);
                return false;
            }
            break;
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,980 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,752 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 68,671 Reputation points
    2021-07-08T10:57:45.153+00:00

    This test with a Network adapter works for me as Admin and in x64 =>

    WCHAR* wsAdapter = TEXT("TunnelBear Adapter V9");
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA did;
    SP_PROPCHANGE_PARAMS pcp;
    TCHAR wsBuffer[1024] = TEXT("");
    DEVPROPTYPE dpt = 0;
    BOOL bRet;
    //hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_IMAGE, 0, 0, DIGCF_PRESENT);
    //hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT);    
    hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, 0, 0, DIGCF_PRESENT);
    for (int i = 0; ; i++)
    {
        did.cbSize = sizeof(did);
        if (!SetupDiEnumDeviceInfo(hDevInfo, i, &did))
            break;
        bRet = SetupDiGetDeviceProperty(hDevInfo, &did, &DEVPKEY_Device_DeviceDesc, &dpt, (PBYTE)wsBuffer, 1000, NULL, 0);
        if (bRet == FALSE)
            continue;
        if (wcscmp(wsBuffer, wsAdapter) == 0)
        {
            pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
            if (SetupDiSetClassInstallParams(hDevInfo, &did, &pcp.ClassInstallHeader, sizeof(SP_PROPCHANGE_PARAMS)))
            {
                pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
                pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
                pcp.HwProfile = 0;
                pcp.Scope = DICS_FLAG_GLOBAL; // DICS_FLAG_CONFIGSPECIFIC;
                pcp.StateChange = DICS_DISABLE;
                SetupDiSetClassInstallParams(hDevInfo, &did, &pcp.ClassInstallHeader, sizeof(SP_PROPCHANGE_PARAMS));
                TCHAR wsError[512] = TEXT("");
                if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &did) == 0)
                {       
                    DWORD dwError = GetLastError();
                    if (GetLastError() == ERROR_IN_WOW64)
                        wsprintf(wsError, TEXT("SetupDiCallClassInstaller does not work from WOW64\r\n"));
                    else
                        wsprintf(wsError, TEXT("SetupDiCallClassInstaller Error : %d\r\n"), dwError);
                }
                else
                    wsprintf(wsError, TEXT("Device state successfully changed\r\n"));
                OutputDebugString(wsError);
            }
            else
            {
                // Error...
            }
            break;
        }
    }
    SetupDiDestroyDeviceInfoList(hDevInfo);