C
C#7mo ago
dyxnie

Properly import dll in csproj

I am currently porting a Wii Mote Controls API from Unity to Godot and I have a couple dlls I have to import because i get the error:
E 0:00:01:0602 nint HIDapi.hid_enumerate(ushort, ushort): System.DllNotFoundException: Unable to load DLL 'hidapi' or one of its dependencies: The specified module could not be found. (0x8007007E)
<C++ Error> System.DllNotFoundException
<C++ Source> :0 @ nint HIDapi.hid_enumerate(ushort, ushort)
<Stack Trace> :0 @ nint HIDapi.hid_enumerate(ushort, ushort)
WiimoteManager.cs:63 @ bool WiimoteApi.WiimoteManager._FindWiimotes(WiimoteApi.WiimoteType)
WiimoteManager.cs:39 @ bool WiimoteApi.WiimoteManager.FindWiimotes()
WiimoteConnection.cs:42 @ void WiimoteConnection.FindWiimotes()
WiimoteTest.cs:15 @ void WiimoteTest._Ready()
Node.cs:2137 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
WiimoteTest_ScriptMethods.generated.cs:48 @ bool WiimoteTest.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)
E 0:00:01:0602 nint HIDapi.hid_enumerate(ushort, ushort): System.DllNotFoundException: Unable to load DLL 'hidapi' or one of its dependencies: The specified module could not be found. (0x8007007E)
<C++ Error> System.DllNotFoundException
<C++ Source> :0 @ nint HIDapi.hid_enumerate(ushort, ushort)
<Stack Trace> :0 @ nint HIDapi.hid_enumerate(ushort, ushort)
WiimoteManager.cs:63 @ bool WiimoteApi.WiimoteManager._FindWiimotes(WiimoteApi.WiimoteType)
WiimoteManager.cs:39 @ bool WiimoteApi.WiimoteManager.FindWiimotes()
WiimoteConnection.cs:42 @ void WiimoteConnection.FindWiimotes()
WiimoteTest.cs:15 @ void WiimoteTest._Ready()
Node.cs:2137 @ bool Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
WiimoteTest_ScriptMethods.generated.cs:48 @ bool WiimoteTest.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant_call_error*, Godot.NativeInterop.godot_variant*)
`
10 Replies
dyxnie
dyxnieOP7mo ago
And this is my .csproj:
<Project Sdk="Godot.NET.Sdk/4.2.2">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>surfsSportResort</RootNamespace>
</PropertyGroup>

<ItemGroup>
<!-- Reference to hidapi.dll for win64 platform -->
<PrivateReference Include="hidapi">
<HintPath>$(MSBuildThisFileDirectory)Wiimote Godot API\Plugins\win32\hidapi.dll</HintPath>
</PrivateReference>
</ItemGroup>

<ItemGroup Condition="'$(Platform)'=='x64'">
<!-- Reference to hidapi.dll for win32 platform -->
<PrivateReference Include="hidapi">
<HintPath>$(MSBuildThisFileDirectory)Wiimote Godot API\Plugins\win64\hidapi.dll</HintPath>
</PrivateReference>
</ItemGroup>
</Project>
<Project Sdk="Godot.NET.Sdk/4.2.2">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net7.0</TargetFramework>
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'ios' ">net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>surfsSportResort</RootNamespace>
</PropertyGroup>

<ItemGroup>
<!-- Reference to hidapi.dll for win64 platform -->
<PrivateReference Include="hidapi">
<HintPath>$(MSBuildThisFileDirectory)Wiimote Godot API\Plugins\win32\hidapi.dll</HintPath>
</PrivateReference>
</ItemGroup>

<ItemGroup Condition="'$(Platform)'=='x64'">
<!-- Reference to hidapi.dll for win32 platform -->
<PrivateReference Include="hidapi">
<HintPath>$(MSBuildThisFileDirectory)Wiimote Godot API\Plugins\win64\hidapi.dll</HintPath>
</PrivateReference>
</ItemGroup>
</Project>
I tried importing it with the help of AI but it didnt really work.. And this is my wrapped HIDapi.cs:
using System.Runtime.InteropServices;
using System.Collections;
using System;
using System.Text;

public class HIDapi {

[DllImport("hidapi")]
public static extern int hid_init();

[DllImport("hidapi")]
public static extern int hid_exit();

[DllImport("hidapi")]
public static extern IntPtr hid_error(IntPtr device);

[DllImport("hidapi")]
public static extern IntPtr hid_enumerate(ushort vendor_id, ushort product_id);

[DllImport("hidapi")]
public static extern void hid_free_enumeration(IntPtr devs);

[DllImport("hidapi")]
public static extern int hid_get_feature_report(IntPtr device, byte[] data, UIntPtr length);

[DllImport("hidapi")]
public static extern int hid_get_indexed_string(IntPtr device, int string_index, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_manufacturer_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_product_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_serial_number_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern IntPtr hid_open(ushort vendor_id, ushort product_id, string serial_number);

[DllImport("hidapi")]
public static extern void hid_close(IntPtr device);

[DllImport("hidapi")]
public static extern IntPtr hid_open_path(string path);

[DllImport("hidapi")]
public static extern int hid_read(IntPtr device, byte[] data, UIntPtr length);

...
using System.Runtime.InteropServices;
using System.Collections;
using System;
using System.Text;

public class HIDapi {

[DllImport("hidapi")]
public static extern int hid_init();

[DllImport("hidapi")]
public static extern int hid_exit();

[DllImport("hidapi")]
public static extern IntPtr hid_error(IntPtr device);

[DllImport("hidapi")]
public static extern IntPtr hid_enumerate(ushort vendor_id, ushort product_id);

[DllImport("hidapi")]
public static extern void hid_free_enumeration(IntPtr devs);

[DllImport("hidapi")]
public static extern int hid_get_feature_report(IntPtr device, byte[] data, UIntPtr length);

[DllImport("hidapi")]
public static extern int hid_get_indexed_string(IntPtr device, int string_index, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_manufacturer_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_product_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern int hid_get_serial_number_string(IntPtr device, StringBuilder str, UIntPtr maxlen);

[DllImport("hidapi")]
public static extern IntPtr hid_open(ushort vendor_id, ushort product_id, string serial_number);

[DllImport("hidapi")]
public static extern void hid_close(IntPtr device);

[DllImport("hidapi")]
public static extern IntPtr hid_open_path(string path);

[DllImport("hidapi")]
public static extern int hid_read(IntPtr device, byte[] data, UIntPtr length);

...
(text too long)
...
[DllImport("hidapi")]
public static extern int hid_read_timeout(IntPtr dev, byte[] data, UIntPtr length, int milliseconds);

[DllImport("hidapi")]
public static extern int hid_send_feature_report(IntPtr device, byte[] data, UIntPtr length);

[DllImport("hidapi")]
public static extern int hid_set_nonblocking(IntPtr device, int nonblock);

[DllImport("hidapi")]
public static extern int hid_write(IntPtr device, byte[] data, UIntPtr length);
}

struct hid_device_info {
public string path;
public ushort vendor_id;
public ushort product_id;
public string serial_number;
public ushort release_number;
public string manufacturer_string;
public string product_string;
public ushort usage_page;
public ushort usage;
public int interface_number;
public IntPtr next;
}
...
[DllImport("hidapi")]
public static extern int hid_read_timeout(IntPtr dev, byte[] data, UIntPtr length, int milliseconds);

[DllImport("hidapi")]
public static extern int hid_send_feature_report(IntPtr device, byte[] data, UIntPtr length);

[DllImport("hidapi")]
public static extern int hid_set_nonblocking(IntPtr device, int nonblock);

[DllImport("hidapi")]
public static extern int hid_write(IntPtr device, byte[] data, UIntPtr length);
}

struct hid_device_info {
public string path;
public ushort vendor_id;
public ushort product_id;
public string serial_number;
public ushort release_number;
public string manufacturer_string;
public string product_string;
public ushort usage_page;
public ushort usage;
public int interface_number;
public IntPtr next;
}
dyxnie
dyxnieOP7mo ago
and this is my directory the dlls are under "/Wiimote Godot API/Plugins/"
No description
Omnissiah
Omnissiah7mo ago
you shouldn't need to do this there should be a .net wrapper
dyxnie
dyxnieOP7mo ago
i have never really programmed in c# outside of some unity related stuff so ill look into it the hidapi wrapper (i believe it is called like that) was copied over from the unity extension
Omnissiah
Omnissiah7mo ago
i see there is a hidapi.net in nuget in the past i used the other, hidlibrary, tho anyway is the dll in the compiled folder?
dyxnie
dyxnieOP7mo ago
i guess so if all the other things are being compiled
Omnissiah
Omnissiah7mo ago
then i guess it should work
dyxnie
dyxnieOP7mo ago
does the import look right to you?
Omnissiah
Omnissiah7mo ago
i dont know that library; i would use imports only if really necessary
dyxnie
dyxnieOP7mo ago
tried recompiling the dll with the c and h file im pretty sure were used: hidapi.c: https://pastebin.com/FnPPsZM8 hidapi.h: https://pastebin.com/JK74uLyc Command i used for compiling: gcc -shared -o hidapi.dll hidapi.c -lsetupapi How i reference it in my csproj:
<ItemGroup>
<Reference Include="hidapi.dll"/>
</ItemGroup>
<ItemGroup>
<Reference Include="hidapi.dll"/>
</ItemGroup>
My HIDapi.cs (Wrapper): https://pastebin.com/V3pRPDhk
Want results from more Discord servers?
Add your server