user1668674
user1668674

Reputation: 93

Disabling device

I've got a problem with a simple function in my program, that function (listed below) should find device with HardwareId id and then turn it off/on. It finds it but then I get error, and GetLastError() returns value out of described in msdn range. I marked error in code with comment. If anyone seeing this is familiar with SetupDiCallClassInstaller() please help. I don't know where to search for that error and if it is code fault or system env. I'm using windows 7 64-bit and run this program as admin.

bool DisableInterface(bool bStatus) {   
IN LPTSTR HardwareId;      
HardwareId = L"DAUDIO\\FUNC_01&VEN_10DE&DEV_0018&SUBSYS_10DE0101";   

DWORD NewState ;   

if(bStatus) {   
    NewState = DICS_DISABLE;   
}   
else {   
    NewState = DICS_ENABLE;   
}   


DWORD i, err;   
bool found = false;   

HDEVINFO hDevInfo;   
SP_DEVINFO_DATA spDevInfoData ;   

hDevInfo=SetupDiGetClassDevs(NULL, 0, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);   
if(hDevInfo == INVALID_HANDLE_VALUE)   
{   
    printf("blad1");   
    return false;   
}   

spDevInfoData.cbSize=sizeof(SP_DEVINFO_DATA);   
for(i=0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)   
{   
    DWORD DataT;   
    LPTSTR p, buffer = NULL;   
    DWORD buffersize = 0;   

    // get all devices info
    while(!SetupDiGetDeviceRegistryProperty(hDevInfo,   
                                            &spDevInfoData,   
                                            SPDRP_HARDWAREID,   
                                            &DataT,   
                                            (PBYTE)buffer,   
                                            buffersize,   
                                            &buffersize) )   
    {   
        if(GetLastError() == ERROR_INVALID_DATA) {    
            break ;   
        }   
        else if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {    
            if(buffer) 
                LocalFree(buffer);   
            buffer = (wchar_t*)LocalAlloc(LPTR,buffersize);   
        }   
        else {   
            goto cleanup_DeviceInfo;   
        }   
    }   

    if(GetLastError() == ERROR_INVALID_DATA) 
        continue;   

    //find device with HardwerId
    for(p = buffer; *p && (p<&buffer[buffersize])  ; p += lstrlen(p) + sizeof(TCHAR)) {   
        if( !_tcscmp(HardwareId, p) ) {   
            found = true;   
            break;   
        }   
    }   

    if(buffer) 
        LocalFree(buffer);   

    // if device found change it's state
    if(found)   
    {   
        SP_PROPCHANGE_PARAMS params;   

        params.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER);   
        params.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE ;   
        params.Scope=DICS_FLAG_GLOBAL ;   
        params.StateChange = NewState ;   

        // setup proper parameters            
        if(!SetupDiSetClassInstallParams(hDevInfo, &spDevInfoData, &params.ClassInstallHeader, sizeof(params))) {   
            DWORD errorcode = GetLastError();   
        }   

        // use parameters
        if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData)) {   
            DWORD errorcode = GetLastError(); // error here  
        }   

        switch(NewState) {   
            case DICS_DISABLE :   
                printf("off");   
                break;   
            case DICS_ENABLE :   
                printf("on");   
                break;   
        }   

        break;   
    }   

}   

cleanup_DeviceInfo :   
err = GetLastError();   
SetupDiDestroyDeviceInfoList(hDevInfo);   
SetLastError(err);   

return true;   
}  

Thanks for help.

Upvotes: 9

Views: 8027

Answers (1)

Fox Cutter
Fox Cutter

Reputation: 655

The HEX version of your error is 0xE0000235. Looking in SetupAPI.h we can see that this maps to ERROR_IN_WOW64.

If you look on this MSDN thread you can see other people with this issue. About 1/3 of the way down the page Maarten van de Bospoort MSFT responds with this:

The error is because you’re calling SetupDiCallClassInstaller from a x86 process on a x64 machines.

Seems like this is the cause of your problem, you're on a 64-bit version of windows, but calling it from a 32-bit process. Try compiling for 64-bit.

Upvotes: 14

Related Questions