Jackzz
Jackzz

Reputation: 1467

C++ : Disabling a device driver in Windows

Can anybody help me to tell why this code is not disabling the cdrom driver?It builds correctly.I debugged each line everything works perfectly. I have removed the error handling code and the cleanup code.

int main(int argc, char* argv[])
{

     IWbemServices *pSvc = NULL;
 HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);

    hres = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);    
    IWbemLocator *pLoc = NULL;
    hres = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,LPVOID *)&pLoc);

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

    BSTR MethodName = SysAllocString(L"StopService");
    BSTR ClassName = SysAllocString(L"Win32_SystemDriver");

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

    VARIANT varCommand;

    IWbemClassObject* pOutParams = NULL;
    hres = pSvc->ExecMethod(L"Win32_SystemDriver.Name=\"cdrom\"", MethodName, 0,
    NULL,NULL, &pOutParams, NULL);

    VARIANT varReturnValue;
    hres = pOutParams->Get(L"ReturnValue", 0, &varReturnValue, NULL, 0);
    if (!FAILED(hres))
    wcout << "ReturnValue " << varReturnValue.intVal << endl;
    VariantClear(&varReturnValue);

    // Clean up    
    SysFreeString(ClassName);
    SysFreeString(MethodName);  
    return 0;
}

Please help..

Upvotes: 1

Views: 1961

Answers (1)

ExceptionalHandler
ExceptionalHandler

Reputation: 72

Not all windows driver accept a 'stop' control request even if they say they do. You can not stop cdrom driver even from a command-line running as an administrator like: "sc stop cdrom".

To disable a windows driver, one must set it to SERVICE_DEMAND_START and reboot. Again, you may not be able to disable all drivers. Some drivers have an Error control of 0x3, which means that windows will fallback to last known good control set if those drivers fail to start.

It might be a good idea to try your code with a service/driver that is can be stopped from an administrative command prompt. Moreover, you may want to check "AcceptStop" property before Executing the "StopService" method.

You might also want to CoSetProxyBlanket as mentioned in the example here.

Upvotes: 2

Related Questions