zoukankan      html  css  js  c++  java
  • DriverStudio 和 WDF驱动 通过GUID获取设备句柄的差别 分类: windows驱动程序WDM 2013-10-30 08:56 812人阅读 评论(0) 收藏

    DriverStudio

    /*****************************************************************************
    * 功能: 通过GUID打开设备,获得设备句柄
    * 参数: 
    *****************************************************************************/
    HANDLE lOpenByInterface(
      GUID* pClassGuid, // points to the GUID that identifies the interface class
      DWORD instance,  // specifies which instance of the enumerated devices to open
      PDWORD pError  // address of variable to receive error status
      )
    {
     HANDLE hDev=0;
     
     CDeviceInterfaceClass DevClass(pClassGuid, pError);

     if (*pError != ERROR_SUCCESS)
      return INVALID_HANDLE_VALUE;

     CDeviceInterface DevInterface(&DevClass, instance, pError);

     if (*pError != ERROR_SUCCESS)
      return INVALID_HANDLE_VALUE;

     hDev = CreateFile(
      DevInterface.DevicePath(),
      GENERIC_READ | GENERIC_WRITE,
      FILE_SHARE_READ | FILE_SHARE_WRITE,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      NULL
      );

     if (hDev == INVALID_HANDLE_VALUE)
      *pError = GetLastError();
     
     return hDev;
    }

    //WDF驱动

    HANDLE CxDriverInterface::SetupDevInterfaces(DWORD dev_interface_index, int board_type)
    {
     HANDLE hd_invalid = INVALID_HANDLE_VALUE;
     DWORD required_buf_size;
     GUID *pGuid = (LPGUID)&GUID_DEVINTERFACE_ATHENA;

     if ( board_type == Athena_EVK )
     {
      pGuid = (LPGUID)&GUID_DEVINTERFACE_ATHENA;
     }
     else if( (board_type == Atlas_Plus_EVK) || (board_type == Atlas_EVK) )
     {
      pGuid = (LPGUID)&GUID_DEVINTERFACE_ATLAS;
     }

     //Get handle to our device information set that contains requested device information elements
     HDEVINFO devInfoSetHandle = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
     if ( devInfoSetHandle == INVALID_HANDLE_VALUE )
     {
      TRACE("SetupDiGetClassDevs failed to find device GUID in system! ");
      return hd_invalid;
     }

     SP_INTERFACE_DEVICE_DATA devInterfaceInfo;
     devInterfaceInfo.cbSize = sizeof(devInterfaceInfo);

     // Try to enumerate the device interfaces that are contained in device information set just retrieved
     if( !SetupDiEnumDeviceInterfaces( devInfoSetHandle, NULL, pGuid, dev_interface_index, &devInterfaceInfo ) )
     {
      TRACE("SetupDiEnumDeviceInterfaces failed to enumerate device GUID! ");
      SetupDiDestroyDeviceInfoList(devInfoSetHandle);
      return hd_invalid;
     }

     // Get the required buffer size and allocate proper sized buffer
     SetupDiGetDeviceInterfaceDetail( devInfoSetHandle, &devInterfaceInfo, NULL, 0, &required_buf_size, NULL );
     PSP_INTERFACE_DEVICE_DETAIL_DATA devInterfaceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (required_buf_size);

     if( devInterfaceDetail == NULL )
     {
      TRACE("SetupDiGetDeviceInterfaceDetail failed! ");
      SetupDiDestroyDeviceInfoList( devInfoSetHandle );
      return hd_invalid;
     }

     // Get details for the device interface
     devInterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
     if( !SetupDiGetDeviceInterfaceDetail( devInfoSetHandle, &devInterfaceInfo, devInterfaceDetail, required_buf_size, NULL, NULL) )
     {
      TRACE("SetupDiGetDeviceInterfaceDetail failed to set SP_INTERFACE_DEVICE_DETAIL_DATA! ");
      SetupDiDestroyDeviceInfoList( devInfoSetHandle );
      delete devInterfaceDetail;
      return hd_invalid;
     }
     //前面这部分在Driverworks中用两个类来完成
     // Get device handle
     LPCWSTR dev_path = devInterfaceDetail->DevicePath;
     HANDLE dev_hd = CreateFile( devInterfaceDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

     if( dev_hd == INVALID_HANDLE_VALUE )
     {
      TRACE("Failed to create device handleB!");
      exit(1);
     }

     delete devInterfaceDetail;

     if ( devInfoSetHandle )
      SetupDiDestroyDeviceInfoList( devInfoSetHandle );

     return dev_hd;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    P4091 [HEOI2016/TJOI2016]求和(第二类斯特林数+NTT)
    CF960G Bandit Blues(第一类斯特林数)
    loj#2542. 「PKUWC2018」随机游走(树形dp+Min-Max容斥)
    mysql修改原始密码
    MySQL基础命令小结
    pip安装python包出错:Could not find a version that satisfies the requirement skimage (from versions: )
    python 安装whl文件
    python中使用anaconda对不平衡数据的处理包imblearn的安装
    数据分析-合辑
    No module named ‘sklearn.model_selection解决办法
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706744.html
Copyright © 2011-2022 走看看