Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling device

Tags:

c++

windows

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.

like image 993
user1668674 Avatar asked Sep 13 '12 13:09

user1668674


People also ask

What is disable device?

Disabling Devices means computer instructions or code intended by Seller to erase data or programming or otherwise cause the software or systems to become inoperable or incapable of being used in the full manner for which it was designed or created,.

What happens if you disable device?

Once disabled, Windows will no longer assign system resources to the device and no software on your computer will be able to use it.

What does disabling a device do in Device Manager?

To disable a device means that the device remains physically connected to the computer, but its driver is unloaded from memory and its resources are freed so that the device cannot be used.

How do I Undisable a device on my computer?

Open Start. Search for Device Manager and click the top result to open the app. Expand the branch with the driver you want to enable. Right-click the device and select the Enable device option.


1 Answers

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.

like image 152
Fox Cutter Avatar answered Oct 06 '22 19:10

Fox Cutter