Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif

#include <GLFW/glfw3.h>
#include "win/win-helpers.h"
#include "utilities/os/h_result.h"

namespace rs2
{
Expand Down Expand Up @@ -249,8 +249,8 @@ Some auxillary functionalities might be affected. Please report this message if
{
std::string res;
#ifdef _WIN32
using namespace librealsense::platform;

using namespace utilities::h_result;
if (f == temp_folder)
{
TCHAR buf[MAX_PATH];
Expand Down
56 changes: 56 additions & 0 deletions common/utilities/os/h_result.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once
#include "../../src/types.h"
#include <comdef.h>



namespace utilities {
namespace h_result {

using namespace librealsense;
inline std::string win_to_utf(const WCHAR * s)
{
auto len = WideCharToMultiByte(CP_UTF8, 0, s, -1, nullptr, 0, nullptr, nullptr);
if (len == 0)
throw std::runtime_error(to_string() << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError());

std::string buffer(len - 1, ' ');
len = WideCharToMultiByte(CP_UTF8, 0, s, -1, &buffer[0], static_cast<int>(buffer.size()) + 1, nullptr, nullptr);
if (len == 0)
throw std::runtime_error(to_string() << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError());

return buffer;
}

inline std::string hr_to_string(HRESULT hr)
{
_com_error err(hr);
std::wstring errorMessage = (err.ErrorMessage()) ? err.ErrorMessage() : L"";
std::stringstream ss;
ss << "HResult 0x" << std::hex << hr << ": \"" << win_to_utf(errorMessage.data()) << "\"";
return ss.str();
}

inline bool check(const char * call, HRESULT hr, bool to_throw = true)
{
if (FAILED(hr))
{
std::string descr = to_string() << call << " returned: " << hr_to_string(hr);
if (to_throw)
throw librealsense::windows_backend_exception(descr);
else
LOG_DEBUG(descr);

return false;
}
return true;
}

#define CHECK_HR(x) check(#x, x);
#define LOG_HR(x) check(#x, x, false);

}
}
3 changes: 3 additions & 0 deletions src/mf/mf-hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <propkeydef.h>
#include <comutil.h>
#include <string>
#include "../../common/utilities/os/h_result.h"

#pragma comment(lib, "Sensorsapi.lib")
#pragma comment(lib, "PortableDeviceGuids.lib")
Expand All @@ -37,6 +38,8 @@ namespace librealsense
{
namespace platform
{
using namespace utilities::h_result;

class sensor_events : public ISensorEvents
{
public:
Expand Down
2 changes: 2 additions & 0 deletions src/mf/mf-uvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The library will be compiled without the metadata support!\n")
#include <vidcap.h>
#include <ksmedia.h> // Metadata Extension
#include <Mferror.h>
#include "../../common/utilities/os/h_result.h"

#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "mf.lib")
Expand All @@ -54,6 +55,7 @@ namespace librealsense
{
namespace platform
{
using namespace utilities::h_result;

#ifdef METADATA_SUPPORT

Expand Down
41 changes: 3 additions & 38 deletions src/win/win-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <regex>
#include <Sddl.h>
#include "../../common/utilities/os/h_result.h"

#pragma comment(lib, "cfgmgr32.lib")
#pragma comment(lib, "setupapi.lib")
Expand All @@ -44,22 +45,15 @@ namespace librealsense
{
namespace platform
{
using namespace utilities::h_result;

template<typename T>
size_t vector_bytes_size(const typename std::vector<T>& vec)
{
static_assert((std::is_arithmetic<T>::value), "vector_bytes_size requires numeric type for input data");
return sizeof(T) * vec.size();
}

std::string hr_to_string(HRESULT hr)
{
_com_error err(hr);
std::wstring errorMessage = (err.ErrorMessage()) ? err.ErrorMessage() : L"";
std::stringstream ss;
ss << "HResult 0x" << std::hex << hr << ": \"" << win_to_utf(errorMessage.data()) << "\"";
return ss.str();
}

typedef ULONG(__stdcall* fnRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);


Expand All @@ -76,35 +70,6 @@ namespace librealsense
return false;
}

bool check(const char * call, HRESULT hr, bool to_throw)
{
if (FAILED(hr))
{
std::string descr = to_string() << call << " returned: " << hr_to_string(hr);
if (to_throw)
throw windows_backend_exception(descr);
else
LOG_DEBUG(descr);

return false;
}
return true;
}

std::string win_to_utf(const WCHAR * s)
{
auto len = WideCharToMultiByte(CP_UTF8, 0, s, -1, nullptr, 0, nullptr, nullptr);
if(len == 0)
throw std::runtime_error(to_string() << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError());

std::string buffer(len-1, ' ');
len = WideCharToMultiByte(CP_UTF8, 0, s, -1, &buffer[0], static_cast<int>(buffer.size())+1, nullptr, nullptr);
if(len == 0)
throw std::runtime_error(to_string() << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError());

return buffer;
}

std::vector<std::string> tokenize(std::string string, char separator)
{
std::vector<std::string> tokens;
Expand Down
6 changes: 0 additions & 6 deletions src/win/win-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ namespace librealsense
}
}

bool check(const char * call, HRESULT hr, bool to_throw = true);
#define CHECK_HR(x) check(#x, x);
#define LOG_HR(x) check(#x, x, false);

std::string win_to_utf(const WCHAR * s);

bool is_win10_redstone2();

std::vector<std::string> tokenize(std::string string, char separator);
Expand Down
3 changes: 3 additions & 0 deletions src/winusb/enumerator-winusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

#include <Cfgmgr32.h>
#include <SetupAPI.h>
#include "../../common/utilities/os/h_result.h"

#pragma comment(lib, "winusb.lib")

namespace librealsense
{
namespace platform
{
using namespace utilities::h_result;

//https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/supported-usb-classes#microsoft-provided-usb-device-class-drivers
const std::map<std::string, usb_class> guids = {
{"{175695cd-30d9-4f87-8be3-5a8270f49a31}", RS2_USB_CLASS_VENDOR_SPECIFIC}, //Ivcam HWM
Expand Down