2

拜托,你能告诉我我做错了什么吗?

为什么在插入/拔出“禁用设备SERVICE_CONTROL_DEVICEEVENT”时不通知(我的意思是在设备管理器中禁用设备)?

如果设备启用,一切正常。这是我的源代码:

  • 注册通知:

    m_hServiceStatus = RegisterServiceCtrlHandlerEx(m_szServiceName, HandlerEx, NULL);
    if (m_hServiceStatus == NULL)
    {
        return;
    }
    
    DWORD dwState = SERVICE_START_PENDING;
    SetServiceStatus(m_hServiceStatus, &dwState);
    
    // Register device notification
    DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
    ZeroMemory( &notificationFilter, sizeof(notificationFilter) );
    notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    
    if(NULL == (m_hDevNotify = RegisterDeviceNotification(m_hServiceStatus, &notificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE|DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)))
    {
        return;
    }
    
  • HandlerEx功能:

    DWORD HandlerEx(DWORD dwOpcode, DWORD dwEventType, LPVOID lpEventData, LPVOID /*lpContext*/) throw()
    {
        DWORD dwRes = ERROR_CALL_NOT_IMPLEMENTED;
        switch (dwOpcode)
        {
        case SERVICE_CONTROL_STOP:
            if (m_hDevNotify)
            {
                UnregisterDeviceNotification(m_hDevNotify);
                m_hDevNotify = NULL;                
            }
            dwRes = NO_ERROR;
            break;
        case SERVICE_CONTROL_SHUTDOWN:
            if (m_hDevNotify)
            {
                UnregisterDeviceNotification(m_hDevNotify);
                m_hDevNotify = NULL;
            }
            dwRes = NO_ERROR;
            break;
        case SERVICE_CONTROL_DEVICEEVENT:
            {
                // ToDo: process event notification
                dwRes = NO_ERROR;
            }
            break;
        default:
            break;
        }
        return dwRes;
    }
    

提前谢谢你的帮助!

4

0 回答 0