【问题标题】:windows Device disablewindows 设备禁用
【发布时间】:2013-12-18 14:27:33
【问题描述】:

我正在尝试从我的应用程序中禁用网络摄像头。它将设备管理器中的设备状态从禁用更改为启用,但设备仍处于活动状态,当我尝试关闭设备属性窗口时。它要求重新启动系统以进行更改。他们是否可以通过代码来完成此操作而无需重新启动系统。

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;

}

任何帮助表示赞赏

谢谢

【问题讨论】:

    标签: c++ windows-7 device-manager


    【解决方案1】:

    我得到了解决方案 进程已打开硬件句柄并加载了 dll 等,这就是它需要重新启动的原因。必须退出具有设备链接的进程以避免在禁用设备之前重新启动

    【讨论】:

      【解决方案2】:

      我没有代表发表评论,否则我会发表评论,因为这肯定不是一个完整的答案。您是否查看过 WMI 界面?例如,也许 Win32_SystemDriver 类会做你想做的事——它有一个“禁用”方法。

      【讨论】:

      • 谢谢你的回复,你能给我这个实现或链接的任何例子吗
      • 我无法通过实现这个直接检测相机你有什么想法来检测
      • 这个 WMI 类的参考文档在这里:msdn.microsoft.com/en-us/library/aa394472(v=vs.85).aspx,但是您可能需要做更多的阅读才能了解如何使用 WMI(它有点像野兽)。无论如何,听起来你解决了你的问题,但我不想让你的问题悬而未决。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多