Question

I am trying to disable webcam from my app. it changes status of device in the device manager from disable to enable but device is still active and when i try to close device properties window. it ask for restart system to effect changes.Is their any way through code i can done this without system restart.

int main(int argc, void * argv[])
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
SP_PROPCHANGE_PARAMS params; // params to set in order to enable/disable the device

// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,0, 0,DIGCF_PRESENT | DIGCF_ALLCLASSES );

if (hDevInfo == INVALID_HANDLE_VALUE)
{
    // Insert error handling here.
    return 1;
}

// Enumerate through all devices in Set.

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
{
      wchar_t  szPhysical[MAX_PATH] = {0};
      const char *sstt ="\\Device\\00000079";

      while (!SetupDiGetDeviceRegistryProperty(hDevInfo,&DeviceInfoData,SPDRP_PHYSICAL_DEVICE_OBJECT_NAME,0L,(PBYTE)szPhysical,2048,0)){}

      if(szPhysical[0]==sstt[0])
        if(szPhysical[1]==sstt[1])
        if(szPhysical[2]==sstt[2])
        if(szPhysical[3]==sstt[3])
        if(szPhysical[4]==sstt[4])
        if(szPhysical[5]==sstt[5])
        if(szPhysical[6]==sstt[6])
        if(szPhysical[7]==sstt[7])
        if(szPhysical[8]==sstt[8])
        if(szPhysical[9]==sstt[9])
        if(szPhysical[10]==sstt[10])
        if(szPhysical[11]==sstt[11])
        if(szPhysical[12]==sstt[12])
        if(szPhysical[13]==sstt[13])
        if(szPhysical[14]==sstt[14])
        if(szPhysical[15]==sstt[15]){

        printf("disabling...\n");
        // init the structure
        params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
        params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
        params.HwProfile = 0;
        params.Scope = DICS_FLAG_CONFIGSPECIFIC;
        params.StateChange = DICS_DISABLE;
        // prepare operation
        if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData,&params.ClassInstallHeader, sizeof(params)))
        {
            printf("Error while preparing params !\n");
            break;
        }
        // launch op
        if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData))
        {
            printf("Error while calling OP ! Return code is %x\n", GetLastError());
            continue;
        }
        printf("done.\n\n");

    }

}

if ( GetLastError()!=NO_ERROR &&GetLastError()!=ERROR_NO_MORE_ITEMS )
{
    // Insert error handling here.
    return 1;
}

//  Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);

return 0;

}

any help appreciated

thanks

Was it helpful?

Solution

I got its solution process has open handle to hardware and has dll,s etc. loaded that's why it needs restart. process which has link to device must be exited to avoid restart before disabling device

OTHER TIPS

I don't have the rep to comment, otherwise I would have as this is certainly not a complete answer. Have you looked into the WMI interface? For example, maybe the Win32_SystemDriver class would do what you want- it has a "disable" method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top