Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for get_folder_path #9562

Merged
merged 13 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions common/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <wchar.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include "utilities/os/hresult.h"
#endif

#if (defined(_WIN32) || defined(_WIN64))
Expand All @@ -42,7 +43,6 @@

namespace rs2
{

// Use shortcuts for long names to avoid trimming of essential data
std::string truncate_string(const std::string& str, size_t width)
{
Expand Down Expand Up @@ -248,6 +248,7 @@ Some auxillary functionalities might be affected. Please report this message if
{
std::string res;
#ifdef _WIN32

if (f == temp_folder)
{
TCHAR buf[MAX_PATH];
Expand All @@ -261,12 +262,21 @@ Some auxillary functionalities might be affected. Please report this message if
else
{
GUID folder;
HRESULT hr;
switch (f)
{
case user_desktop: folder = FOLDERID_Desktop;
break;
case user_documents: folder = FOLDERID_Documents;
break;
// The user's Documents folder location may get overridden, as we know OneDrive does in certain circumstances.
// In such cases, the new function, SHGetKnownFolderPath, does not always return the new path, while the deprecated
// function does.
CHAR path[MAX_PATH];
CHECK_HR(SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, 0, path));

res = path;
res += "\\";
return res;
case user_pictures: folder = FOLDERID_Pictures;
break;
case user_videos: folder = FOLDERID_Videos;
Expand All @@ -277,8 +287,9 @@ Some auxillary functionalities might be affected. Please report this message if
throw std::invalid_argument(
std::string("Value of f (") + std::to_string(f) + std::string(") is not supported"));
}

PWSTR folder_path = NULL;
HRESULT hr = SHGetKnownFolderPath(folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path);
hr = SHGetKnownFolderPath(folder, KF_FLAG_DEFAULT_PATH, NULL, &folder_path);
if (SUCCEEDED(hr))
{
char str[1024];
Expand Down
2 changes: 2 additions & 0 deletions common/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/time/l500/get-mfr-ww.h"
"${CMAKE_CURRENT_LIST_DIR}/time/l500/get-mfr-ww.cpp"
)

include(${CMAKE_CURRENT_LIST_DIR}/os/CMakeLists.txt)
9 changes: 9 additions & 0 deletions common/utilities/os/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2021 Intel Corporation. All Rights Reserved.

if(WIN32)
target_sources(${LRS_TARGET}
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/hresult.h")

endif()
48 changes: 48 additions & 0 deletions common/utilities/os/hresult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once
#include <string>
#include <Windows.h>
#include <functional>
#include <comdef.h>
#include <sstream>
#include "../string/windows.h"

namespace utilities {
namespace hresult {

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


#define CHECK_HR_STR( call, hr ) \
if( FAILED( hr ) ) \
{ \
std::ostringstream ss; \
ss << call << " returned: " << utilities::hresult::hr_to_string( hr ); \
std::string descr = ss.str(); \
throw std::runtime_error( descr ); \
}


#define LOG_HR_STR( call, hr ) \
if( FAILED( hr ) ) \
{ \
std::ostringstream ss; \
ss << call << " returned: " << utilities::hresult::hr_to_string( hr ); \
std::string descr = ss.str(); \
LOG_DEBUG(descr); \
}

#define CHECK_HR( x ) CHECK_HR_STR( #x, x )
#define LOG_HR( x ) LOG_HR_STR( #x, x )

}
}
40 changes: 40 additions & 0 deletions common/utilities/string/windows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#include <sstream>

#ifdef WIN32
#include <Windows.h>

namespace utilities {
namespace string {
namespace windows {

inline std::string win_to_utf(const WCHAR * s)
{
auto len = WideCharToMultiByte(CP_UTF8, 0, s, -1, nullptr, 0, nullptr, nullptr);
std::ostringstream ss;

if (len == 0)
{
ss << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError();
throw std::runtime_error(ss.str());
}

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)
{
ss.clear();
ss << "WideCharToMultiByte(...) returned 0 and GetLastError() is " << GetLastError();
throw std::runtime_error(ss.str());
}

return buffer;
}
} // namespace windows
} // namespace string
} // namespace utilities
#endif
7 changes: 4 additions & 3 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/string/windows.h"

#pragma comment(lib, "Sensorsapi.lib")
#pragma comment(lib, "PortableDeviceGuids.lib")
Expand Down Expand Up @@ -425,8 +426,8 @@ namespace librealsense
{
if (IsEqualPropertyKey(propertyKey, SENSOR_PROPERTY_DEVICE_PATH))
{
info.device_path = win_to_utf( propertyValue.pwszVal );
info.id = win_to_utf( fName );
info.device_path = utilities::string::windows::win_to_utf( propertyValue.pwszVal );
info.id = utilities::string::windows::win_to_utf( fName );

uint16_t vid, pid, mi;
std::string uid, guid;
Expand Down Expand Up @@ -461,7 +462,7 @@ namespace librealsense
}
if (IsEqualPropertyKey(propertyKey, SENSOR_PROPERTY_SERIAL_NUMBER))
{
auto str = win_to_utf( propertyValue.pwszVal );
auto str = utilities::string::windows::win_to_utf( propertyValue.pwszVal );
std::transform(begin(str), end(str), begin(str), ::tolower);
info.serial_number = str;
}
Expand Down
10 changes: 5 additions & 5 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/hresult.h"

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

#ifdef METADATA_SUPPORT

#pragma pack(push, 1)
Expand Down Expand Up @@ -267,7 +267,7 @@ namespace librealsense
reinterpret_cast<void **>(&ks_topology_info)));

DWORD nNodes=0;
check("get_NumNodes", ks_topology_info->get_NumNodes(&nNodes));
LOG_HR_STR("get_NumNodes", ks_topology_info->get_NumNodes(&nNodes));

CComPtr<IUnknown> unknown = nullptr;
CHECK_HR(ks_topology_info->CreateNodeInstance(xu.node, IID_IUnknown,
Expand Down Expand Up @@ -729,7 +729,7 @@ namespace librealsense

WCHAR * wchar_name = nullptr; UINT32 length;
CHECK_HR(pDevice->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &wchar_name, &length));
auto name = win_to_utf(wchar_name);
auto name = utilities::string::windows::win_to_utf(wchar_name);
CoTaskMemFree(wchar_name);

uint16_t vid, pid, mi; std::string unique_id, guid;
Expand Down Expand Up @@ -891,7 +891,7 @@ namespace librealsense
{
safe_release(pMediaType);
if (hr != MF_E_NO_MORE_TYPES) // An object ran out of media types to suggest therefore the requested chain of streaming objects cannot be completed
check("_reader->GetNativeMediaType(sIndex, k, &pMediaType.p)", hr, false);
LOG_HR_STR("_reader->GetNativeMediaType(sIndex, k, &pMediaType.p)",hr);

break;
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ namespace librealsense
const auto timeout_ms = RS2_DEFAULT_TIMEOUT;
if (_has_started.wait(timeout_ms))
{
check("_reader->ReadSample(...)", _readsample_result);
LOG_HR_STR("_reader->ReadSample(...)", _readsample_result);
}
else
{
Expand Down
49 changes: 6 additions & 43 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/string/windows.h"

#pragma comment(lib, "cfgmgr32.lib")
#pragma comment(lib, "setupapi.lib")
Expand Down Expand Up @@ -51,15 +52,6 @@ namespace librealsense
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 +68,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 Expand Up @@ -360,7 +323,7 @@ namespace librealsense
{
if (handle_node(targetKey, h, i)) // exit condition
{
return std::make_tuple(win_to_utf(fullPath.c_str()) + " " + std::to_string(i),
return std::make_tuple(utilities::string::windows::win_to_utf(fullPath.c_str()) + " " + std::to_string(i),
static_cast<usb_spec>(pConInfo->DeviceDescriptor.bcdUSB));
}
}
Expand All @@ -384,7 +347,7 @@ namespace librealsense
std::vector<WCHAR> buf( cch_required + 1 );
if( CM_Get_Device_ID( devinst, buf.data(), cch_required, 0 ) != CR_SUCCESS )
return false;
*p_out_str = win_to_utf( buf.data() );
*p_out_str = utilities::string::windows::win_to_utf( buf.data() );
}

return true;
Expand Down Expand Up @@ -503,7 +466,7 @@ namespace librealsense
str.reserve( cb );
if( CM_Get_DevNode_Property( get(), &property, &type, (PBYTE) str.data(), &cb, 0 ) != CR_SUCCESS )
return std::string();
return win_to_utf( str.data() );
return utilities::string::windows::win_to_utf( str.data() );
}


Expand Down Expand Up @@ -593,7 +556,7 @@ namespace librealsense
LOG_ERROR("CM_Get_Device_ID failed");
return false;
}
std::string parent_id = win_to_utf( pInstID2.data() );
std::string parent_id = utilities::string::windows::win_to_utf( pInstID2.data() );
//LOG_DEBUG( "... parent device id " << parent_id );
uint16_t parent_vid, parent_pid, parent_mi;
parse_usb_path_from_device_id( parent_vid, parent_pid, parent_mi, parent_uid, parent_id ); // may fail -- but we try to get the parent_uid
Expand Down Expand Up @@ -643,7 +606,7 @@ namespace librealsense
uint16_t mi = 0;
std::string guid;
std::wstring ws(detail_data->DevicePath);
std::string path( win_to_utf( detail_data->DevicePath ));
std::string path(utilities::string::windows::win_to_utf( detail_data->DevicePath ));

/* Parse the following USB path format = \?usb#vid_vvvv&pid_pppp&mi_ii#aaaaaaaaaaaaaaaa#{gggggggg-gggg-gggg-gggg-gggggggggggg} */
parse_usb_path_multiple_interface(vid, pid, mi, parent_uid, path, guid);
Expand Down
7 changes: 1 addition & 6 deletions src/win/win-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <windows.h>
#include <cfgmgr32.h>
#include "../../common/utilities/os/hresult.h"

#define WAIT_FOR_MUTEX_TIME_OUT (5000)

Expand All @@ -29,12 +30,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: 2 additions & 1 deletion src/winusb/enumerator-winusb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "usb/usb-enumerator.h"
#include "device-winusb.h"
#include "interface-winusb.h"
#include "win/win-helpers.h"
#include "../common/utilities/os/hresult.h"
#include "types.h"

#include <atlstr.h>
Expand All @@ -28,6 +28,7 @@ namespace librealsense
{
namespace platform
{

//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
Loading