Win32_SystemDriver 禁用设备驱动程序

Win32_SystemDriver to disable device drivers

试图找到我的问题的答案:,我使用 SetupDiXxx Classes 找到了答案。但问题是,一旦它是 disabled/enabled,它就能够从设备管理器 enable/disable 设备。所以任何用户都可以轻松克服禁令。

进一步研究,我看到 WMIWin32_SystemDriver class 有一个 StopService 方法可以用来禁用设备的驱动程序。但我不确定如何编写相同的代码。谁能帮我在 C++ 中编写代码。我在 MSVS 2010.

您可以通过以下方式在 C++ 中访问 WMI 类: https://msdn.microsoft.com/en-us/library/aa392109(v=vs.85).aspx 但这似乎很老套,也不是很容易。如果您仍然这样做,这是我能找到的最短示例: https://msdn.microsoft.com/en-us/library/aa390421(v=vs.85).aspx 。我已经更新它以匹配您正在做的事情:

#define _WIN32_DCOM
#include <windows.h>
#include <Wbemidl.h>
#include <comdef.h>

# pragma comment(lib, "wbemuuid.lib")

void main()
{
    BSTR MethodName = SysAllocString(L"StopService");
    BSTR ClassName = SysAllocString(L"WINMGMTS:\\.\ROOT\CIMV2\ms_409:Win32_SystemDriver");

    IWbemServices *pSvc = NULL;
    HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hres))
    {
        return;
    }

    hres = CoInitializeSecurity(
        NULL,
        -1,                          // COM negotiates service
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

    if (FAILED(hres))
    {
        CoUninitialize();
        return;
    }

    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(
        CLSID_WbemLocator,
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *)&pLoc);

    if (FAILED(hres))
    {
        CoUninitialize();
        return;
    }

    hres = pLoc->ConnectServer(
        _bstr_t(L"ROOT\CIMV2"),
        NULL,
        NULL,
        0,
        NULL,
        0,
        0,
        &pSvc
        );

    IWbemClassObject* pClass = NULL;
    hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

    if (FAILED(hres))
    {
        CoUninitialize();
        return;
    }

    IWbemClassObject* pInParamsDefinition = NULL;
    hres = pClass->GetMethod(MethodName, 0,
        &pInParamsDefinition, NULL);

    // Execute Method
    IWbemClassObject* pOutParams = NULL;
    hres = pSvc->ExecMethod(ClassName, MethodName, 0, NULL, NULL, &pOutParams, NULL);

    CoUninitialize();
}

您将替换类名以指向您的驱动程序。如果不这样做,它将失败并显示 WBEM_E_INVALID_OBJECT_PATH。要找到它,您需要枚举您的 wmi 对象,这样您就可以 see/pick。这在powershell中绝对是最简单的,只需打开powershell和运行 Get-WmiObject -class Win32_SystemDriver。尽管您可能应该从 powershell 中完成所有这些操作,但请仔细想想。

听起来您可能想考虑学习如何利用 windows 安全策略来限制可以使用的设备: https://msdn.microsoft.com/en-us/library/bb530324.aspx。您将首先启动 gpedit 并按照说明进行操作,直到您阻止设备安装。