scottpidzarko / USBSession

Wrapper to use LibUSBDotNet in PowerShell 7
MIT License
0 stars 0 forks source link

Cannot find libusb-1.0 dll when running Mac Unit tests #5

Closed scottpidzarko closed 4 years ago

scottpidzarko commented 4 years ago

Mac OSX El Capitan.

Build compiles great but this has been driving me insane - when running any of the project's unit tests in VS 2019 on OSX, I cannot for the life of me get them to not throw the following exception:

image

Unhelpfully, the exception prompts me to update my ldconfig, which is great for Linux, but not so much on Unix.

I've installed libusb with homebrew and the .dylib is showing up in /usr/local/lib like it's supposed to. I tried multiple things to "point" mono at that location, including reinstalling libusb with Homebrew, installing mono with homebrew and running "export MONO_GAC_PREFIX="/usr/local", editing the mono dllmap at "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/config" to point to the libusb-1.0 library, adding a custom dllmap in a .config file to the output folder where USBSession.dll was compiled to, no dice.

the library doesn't have the dll extension in the LibUSBDotNet library: image

scottpidzarko commented 4 years ago

See https://github.com/LibUsbDotNet/LibUsbDotNet/pull/41/commits/d7f178046330c6ddaa5bc0ca872d8512393726ee for how libusbdotnet does their unit tests - they overwrite the LIBUSB_DLL location for unit tests (and brew install libusb lsub)

scottpidzarko commented 4 years ago

https://github.com/libusb/libusb/wiki/FAQ#How_can_I_run_libusb_applications_under_Mac_OS_X_if_there_is_already_a_kernel_extension_installed_for_the_device

Something to look at as well

scottpidzarko commented 4 years ago

https://stackoverflow.com/questions/9048626/dllnotfoundexception-with-dllimport-in-mono-on-mac-wrong-architecture

I wonder if it will be found if I place it in the /opt/local/lib directory

Another thing to check: 64-bit mono vs 32 bit mono and if the lib is the right architecture as well

scottpidzarko commented 4 years ago

Just created a new .dotnetframework console project in VS2019 on Mac, added Libusbdotnet, and then copy and pasted the constructor code from this project, and it ran just fine. It can find the library where it's supposed to be too.

Scotts-MacBook-Pro:Debug scott$ MONO_LOG_LEVEL=debug mono TestLibUSB.exe | grep usb
Mono: DllImport attempting to load: '/usr/local/lib/libusb-1.0.dylib'.
Mono: DllImport loaded library '/usr/local/lib/libusb-1.0.dylib'.
Mono: DllImport searching in: '/usr/local/lib/libusb-1.0.dylib' ('/usr/local/lib/libusb-1.0.dylib').
Mono: Searching for 'libusb_init'.
Mono: Probing 'libusb_init'.
Mono: Found as 'libusb_init'.
Mono: DllImport searching in: '/usr/local/lib/libusb-1.0.dylib' ('/usr/local/lib/libusb-1.0.dylib').
Mono: Searching for 'libusb_get_device_list'.
Mono: Probing 'libusb_get_device_list'.
Mono: Found as 'libusb_get_device_list'.
Mono: DllImport searching in: '/usr/local/lib/libusb-1.0.dylib' ('/usr/local/lib/libusb-1.0.dylib').
Mono: Searching for 'libusb_handle_events_timeout'.
Mono: Probing 'libusb_handle_events_timeout'.
Mono: Found as 'libusb_handle_events_timeout'.
Mono: DllImport searching in: '/usr/local/lib/libusb-1.0.dylib' ('/usr/local/lib/libusb-1.0.dylib').
Mono: Searching for 'libusb_free_device_list'.
Mono: Probing 'libusb_free_device_list'.
Mono: Found as 'libusb_free_device_list'.
scottpidzarko commented 4 years ago

Fixed. Looks like dot net core looks for the shared library in the bin/netcoreappX.Y folder and no where else.

See https://www.reddit.com/r/dotnet/comments/8tp9qk/dllimport_in_dot_net_core/