Skip to content

Commit

Permalink
move windows libusb load dir to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
diablodale committed May 16, 2022
1 parent 6a71de5 commit bbf2c7b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 63 deletions.
3 changes: 2 additions & 1 deletion cmake/XLink.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ if(WIN32)
set(XLINK_PLATFORM_INCLUDE ${XLINK_ROOT_DIR}/src/pc/Win/include)

file(GLOB XLINK_PLATFORM_SRC "${XLINK_ROOT_DIR}/src/pc/Win/src/*.c")
list(APPEND XLINK_SOURCES ${XLINK_PLATFORM_SRC})
file(GLOB XLINK_PLATFORM_SRC_CPP "${XLINK_ROOT_DIR}/src/pc/Win/src/*.cpp")
list(APPEND XLINK_SOURCES ${XLINK_PLATFORM_SRC} ${XLINK_PLATFORM_SRC_CPP})
endif()

if(APPLE)
Expand Down
74 changes: 74 additions & 0 deletions src/pc/Win/src/win_usb_host.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Win32 api and MSVC only; not mingw, *nix, etc.
#if defined(_WIN32) && defined(_MSC_VER)

// project
#define MVLOG_UNIT_NAME xLinkUsb

// libraries
#ifdef XLINK_LIBUSB_LOCAL
#include <libusb.h>
#else
#include <libusb-1.0/libusb.h>
#endif

#include "XLink/XLinkLog.h"
#include "usb_host.h"

// std
#include <array>
#include <pathcch.h>

int usbInitialize_customdir(void** hContext) {
// get handle to the module containing a XLink static function/var
// can not use GetModuleFileNameW(nullptr) because when the main depthai app is a DLL (e.g. plugin),
// then it returns the main EXE which is usually wrong
HMODULE hXLink = nullptr;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)&MVLOGLEVEL(global), &hXLink) == 0) {
return LIBUSB_ERROR_OVERFLOW;
}

// get path to that module
std::array<wchar_t, 2048> rawPath = {};
const auto len = GetModuleFileNameW(hXLink, rawPath.data(), static_cast<DWORD>(rawPath.size()));
if ((len == 0) || (len == rawPath.size())) {
return LIBUSB_ERROR_OVERFLOW;
}

// remove filename with string::find_last_of(), _wsplitpath_s(), PathCchRemoveFileSpec()
// using PathCchRemoveFileSpec() as it handles special cases
if (PathCchRemoveFileSpec(rawPath.data(), rawPath.size()) != S_OK) {
return LIBUSB_ERROR_OVERFLOW;
}

// persist existing custom DLL load path
bool oldGetError = false;
std::array<wchar_t, 2048> oldDllDir = {};
if (GetDllDirectoryW(static_cast<DWORD>(oldDllDir.size()), oldDllDir.data())) {
// nothing
}
else {
// SetDllDirectoryW() previously unused, or an error
oldGetError = true;
}

// set custom dll load directory
if (SetDllDirectoryW(rawPath.data()) == 0) {
return LIBUSB_ERROR_OVERFLOW;
}

// initialize libusb
int initResult = LIBUSB_SUCCESS;
__try {
initResult = libusb_init((libusb_context**)hContext);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
initResult = LIBUSB_ERROR_OVERFLOW;
}

// restore custom dll load directory
SetDllDirectoryW(oldGetError ? nullptr : oldDllDir.data());

return initResult;
}

#endif // defined(_WIN32) && defined(_MSC_VER)
64 changes: 2 additions & 62 deletions src/pc/protocols/usb_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
#include <thread>
#include <chrono>
#include <cstring>
#include <array>
#if defined(_WIN32) && defined(_MSC_VER)
#include <pathcch.h>
#endif

constexpr static int MAXIMUM_PORT_NUMBERS = 7;
using VidPid = std::pair<uint16_t, uint16_t>;
Expand Down Expand Up @@ -79,66 +75,10 @@ int usbInitialize(void* options){
// // Debug
// mvLogLevelSet(MVLOG_DEBUG);

// Windows and Win32 api only; not mingw, *nix, etc.
#if defined(_WIN32) && defined(_MSC_VER)
// get handle to the module containing a XLink static function/var
// can not use GetModuleFileNameW(nullptr) because when the main depthai app is a DLL, it
// returns the main EXE, which is usually wrong
HMODULE hXLink = nullptr;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCWSTR)&MVLOGLEVEL(global), &hXLink) == 0) {
return LIBUSB_ERROR_OVERFLOW;
}

// get path to that module
std::array<wchar_t, 2048> rawPath = {};
const auto len = GetModuleFileNameW(hXLink, rawPath.data(), static_cast<DWORD>(rawPath.size()));
if ((len == 0) || (len == rawPath.size())) {
return LIBUSB_ERROR_OVERFLOW;
}
mvLog(MVLOG_INFO, "dale path: %S", rawPath.data());

// remove filename with string::find_last_of(), _wsplitpath_s(), PathCchRemoveFileSpec()
// using PathCchRemoveFileSpec() as it handles special cases
if (PathCchRemoveFileSpec(rawPath.data(), rawPath.size()) != S_OK) {
return LIBUSB_ERROR_OVERFLOW;
}
mvLog(MVLOG_INFO, "dale dir: %S", rawPath.data());

// persist existing custom DLL load path
bool oldGetError = false;
std::array<wchar_t, 2048> oldDllDir = {};
if (GetDllDirectoryW(static_cast<DWORD>(oldDllDir.size()), oldDllDir.data())) {
mvLog(MVLOG_INFO, "dale old: %S", oldDllDir.data());
}
else {
// SetDllDirectoryW() previously unused, or an error
mvLog(MVLOG_INFO, "dale old unused or error");
oldGetError = true;
}

// set custom dll load directory
//const wchar_t hack[] = L"C:\\.hunter\\_Base\\9b4e732\\f33d64c\\8f81e86\\Install\\bin";
//if (SetDllDirectoryW(hack) == 0) {
if (SetDllDirectoryW(rawPath.data()) == 0) {
return LIBUSB_ERROR_OVERFLOW;
}

// initialize libusb
int initResult = LIBUSB_SUCCESS;
__try {
initResult = libusb_init(&context);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
initResult = LIBUSB_ERROR_OVERFLOW;
}

// restore custom dll load directory
SetDllDirectoryW(oldGetError ? nullptr : oldDllDir.data());

return initResult;
#else
return libusb_init(&context);
return usbInitialize_customdir((void**)&context);
#endif
return libusb_init(&context);
}

struct pair_hash {
Expand Down
1 change: 1 addition & 0 deletions src/pc/protocols/usb_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef enum usbBootError {
} usbBootError_t;

int usbInitialize(void* options);
int usbInitialize_customdir(void** hContext);

int usb_boot(const char *addr, const void *mvcmd, unsigned size);
int get_pid_by_name(const char* name);
Expand Down

0 comments on commit bbf2c7b

Please sign in to comment.