1

我的电脑是操作系统:windows10 x64。我想使用代码启用/禁用虚拟 com 端口。

实际上,错误发生在下面,errorcode = #define ERROR_ACCESS_DENIED 5L

在window10中,我不能控制设备禁用或启用???还是源代码错了??

如果你知道,请帮助我

使用的代码如下。这是从 stackoverflow.com 获得的

bool COhCoachView::ControlDeviceDriver(bool bStatus) {
    IN LPTSTR HardwareId;
    //HardwareId = L"DAUDIO\\FUNC_01&VEN_10DE&DEV_0018&SUBSYS_10DE0101";
    // dock device name : \\?\USB#VID_0462&PID_5740#00000000001A#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
    CString debug;
    char* ptr;


    ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)"ControlDeviceDriver");


    DWORD NewState;

    if (bStatus) {
        NewState = DICS_ENABLE;
    }
    else {
        NewState = DICS_DISABLE;
    }


    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 = (TCHAR*)LocalAlloc(LPTR, buffersize);
            }
            else {
                goto cleanup_DeviceInfo;
            }
        }

        if (GetLastError() == ERROR_INVALID_DATA)
            continue;


        ptr = strstr(buffer, "VID_0462&PID_5740");
        if (ptr != NULL)
        {
            debug.Format("device name: %s", buffer);
            ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)debug);

            found = true;
        }




        //find device with HardwerId
        /*
        for (p = buffer; *p && (p < &buffer[buffersize]); p += lstrlen(p) + sizeof(TCHAR)) {

            //debug.Format("device name: %s", p);
            //::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)debug);

            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();
                debug.Format("==== error %x ======", errorcode);
                ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)debug);

            }

            // use parameters
            if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData)) {
                DWORD errorcode = GetLastError(); // error here  
                if (errorcode == 0x05)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData))
                        {

                            ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)"retry succeeded for disabling device");
                            break;
                        }

                        ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)"retry failed for disabling device");
                        Sleep(20);
                    }

                }

                debug.Format("==== error2  %x ======", errorcode);
                ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)debug);
            }

            switch (NewState) {
            case DICS_DISABLE:
                ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)"Device OFF");
                printf("off");
                break;
            case DICS_ENABLE:
                ::SendMessage(hwndLogBar, MSG_WRITE_LOG, 0, (LPARAM)(LPCTSTR)"Device ON");
                printf("on");
                break;
            }

            break;
        }

    }

cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(hDevInfo);
    SetLastError(err);

    return true;



    return true;
}

多谢

4

0 回答 0