diff options
| author | Dave Wilson <[email protected]> | 2015-04-29 09:48:49 -0700 |
|---|---|---|
| committer | Dave Wilson <[email protected]> | 2015-04-29 10:47:29 -0700 |
| commit | d40232638faaab19de557e70a3ee938da7a35295 (patch) | |
| tree | 14e61c973745949af186b7940e3a53d25322f5de /pofx/WDF/App/PowerFxApp.cpp | |
| parent | 02e5b090a6131a7cc1b90f4a9373117c9e3c7088 (diff) | |
samples update for //build
Diffstat (limited to 'pofx/WDF/App/PowerFxApp.cpp')
| -rw-r--r-- | pofx/WDF/App/PowerFxApp.cpp | 147 |
1 files changed, 51 insertions, 96 deletions
diff --git a/pofx/WDF/App/PowerFxApp.cpp b/pofx/WDF/App/PowerFxApp.cpp index c628d366..2ea29a33 100644 --- a/pofx/WDF/App/PowerFxApp.cpp +++ b/pofx/WDF/App/PowerFxApp.cpp @@ -478,115 +478,70 @@ GetDevicePath( _In_ size_t BufLen ) { - HDEVINFO HardwareDeviceInfo; - SP_DEVICE_INTERFACE_DATA DeviceInterfaceData; - PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData = NULL; - ULONG Length, RequiredLength = 0; - BOOL bResult; - HRESULT hr; - - HardwareDeviceInfo = SetupDiGetClassDevs( - InterfaceGuid, - NULL, - NULL, - (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); + CONFIGRET cr = CR_SUCCESS; + PWSTR deviceInterfaceList = NULL; + ULONG deviceInterfaceListLength = 0; + PWSTR nextInterface; + HRESULT hr = E_FAIL; + BOOL bRet = TRUE; - if (HardwareDeviceInfo == INVALID_HANDLE_VALUE) { - printf("SetupDiGetClassDevs failed!\n"); - return FALSE; + cr = CM_Get_Device_Interface_List_Size( + &deviceInterfaceListLength, + InterfaceGuid, + NULL, + CM_GET_DEVICE_INTERFACE_LIST_PRESENT); + if (cr != CR_SUCCESS) { + printf("Error 0x%x retrieving device interface list size.\n", cr); + goto clean0; } - DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); - - bResult = SetupDiEnumDeviceInterfaces(HardwareDeviceInfo, - 0, - InterfaceGuid, - 0, - &DeviceInterfaceData); - - if (bResult == FALSE) { - - LPVOID lpMsgBuf; - - if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR) &lpMsgBuf, - 0, - NULL - )) { - - printf("SetupDiEnumDeviceInterfaces failed: %ws", (LPTSTR)lpMsgBuf); - LocalFree(lpMsgBuf); - } + if (deviceInterfaceListLength <= 1) { + bRet = FALSE; + printf("Error: No active device interfaces found.\n" + " Is the sample driver loaded?"); + goto clean0; + } - printf("SetupDiEnumDeviceInterfaces failed.\n"); - SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); - return FALSE; + deviceInterfaceList = (PWSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR)); + if (deviceInterfaceList == NULL) { + printf("Error allocating memory for device interface list.\n"); + goto clean0; } + ZeroMemory(deviceInterfaceList, deviceInterfaceListLength * sizeof(WCHAR)); - SetupDiGetDeviceInterfaceDetail( - HardwareDeviceInfo, - &DeviceInterfaceData, + cr = CM_Get_Device_Interface_List( + InterfaceGuid, NULL, - 0, - &RequiredLength, - NULL - ); - - DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, RequiredLength); - - if (DeviceInterfaceDetailData == NULL) { - SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); - printf("Failed to allocate memory.\n"); - return FALSE; + deviceInterfaceList, + deviceInterfaceListLength, + CM_GET_DEVICE_INTERFACE_LIST_PRESENT); + if (cr != CR_SUCCESS) { + printf("Error 0x%x retrieving device interface list.\n", cr); + goto clean0; } - DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); - - Length = RequiredLength; - - bResult = SetupDiGetDeviceInterfaceDetail( - HardwareDeviceInfo, - &DeviceInterfaceData, - DeviceInterfaceDetailData, - Length, - &RequiredLength, - NULL); - - if (bResult == FALSE) { - - LPVOID lpMsgBuf; - - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR) &lpMsgBuf, - 0, - NULL - ); - - SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); - printf("Error in SetupDiGetDeviceInterfaceDetail: %ws\n", (LPWSTR)lpMsgBuf); - LocalFree(DeviceInterfaceDetailData); - LocalFree(lpMsgBuf); - return FALSE; + nextInterface = deviceInterfaceList + wcslen(deviceInterfaceList) + 1; + if (*nextInterface != UNICODE_NULL) { + printf("Warning: More than one device interface instance found. \n" + "Selecting first matching device.\n\n"); } - hr = StringCchCopy(DevicePath, - BufLen, - DeviceInterfaceDetailData->DevicePath) ; + hr = StringCchCopy(DevicePath, BufLen, deviceInterfaceList); + if (FAILED(hr)) { + bRet = FALSE; + printf("Error: StringCchCopy failed with HRESULT 0x%x", hr); + goto clean0; + } - SetupDiDestroyDeviceInfoList(HardwareDeviceInfo); // It must be executed in both success and failure traces - LocalFree(DeviceInterfaceDetailData); +clean0: + if (deviceInterfaceList != NULL) { + free(deviceInterfaceList); + } + if (CR_SUCCESS != cr) { + bRet = FALSE; + } - return ( !FAILED(hr) ); // Result depends on StringCchCopy() + return bRet; } |
