From bbf2c7beaabd1d0ee88b80587cb8ac7b56fa599e Mon Sep 17 00:00:00 2001 From: Dale Phurrough Date: Tue, 17 May 2022 00:19:11 +0200 Subject: [PATCH] move windows libusb load dir to separate file --- cmake/XLink.cmake | 3 +- src/pc/Win/src/win_usb_host.cpp | 74 +++++++++++++++++++++++++++++++++ src/pc/protocols/usb_host.cpp | 64 +--------------------------- src/pc/protocols/usb_host.h | 1 + 4 files changed, 79 insertions(+), 63 deletions(-) create mode 100755 src/pc/Win/src/win_usb_host.cpp diff --git a/cmake/XLink.cmake b/cmake/XLink.cmake index 19573c1..fc0ccee 100644 --- a/cmake/XLink.cmake +++ b/cmake/XLink.cmake @@ -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) diff --git a/src/pc/Win/src/win_usb_host.cpp b/src/pc/Win/src/win_usb_host.cpp new file mode 100755 index 0000000..2f0215e --- /dev/null +++ b/src/pc/Win/src/win_usb_host.cpp @@ -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 +#else +#include +#endif + +#include "XLink/XLinkLog.h" +#include "usb_host.h" + +// std +#include +#include + +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 rawPath = {}; + const auto len = GetModuleFileNameW(hXLink, rawPath.data(), static_cast(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 oldDllDir = {}; + if (GetDllDirectoryW(static_cast(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) diff --git a/src/pc/protocols/usb_host.cpp b/src/pc/protocols/usb_host.cpp index c92b5ca..b9793f0 100644 --- a/src/pc/protocols/usb_host.cpp +++ b/src/pc/protocols/usb_host.cpp @@ -23,10 +23,6 @@ #include #include #include -#include -#if defined(_WIN32) && defined(_MSC_VER) - #include -#endif constexpr static int MAXIMUM_PORT_NUMBERS = 7; using VidPid = std::pair; @@ -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 rawPath = {}; - const auto len = GetModuleFileNameW(hXLink, rawPath.data(), static_cast(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 oldDllDir = {}; - if (GetDllDirectoryW(static_cast(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 { diff --git a/src/pc/protocols/usb_host.h b/src/pc/protocols/usb_host.h index a567b66..0ec906f 100644 --- a/src/pc/protocols/usb_host.h +++ b/src/pc/protocols/usb_host.h @@ -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);