DLL Dependencies Error
I wrote a custom DLL with Visual Studio in C# that I now plan to use in Unity. It contains Windows functions related to Bluetooth and Enumeration. These functions were imported in the class library that compiled the Dll via "Add References" option in VS.
I wrote a file in Unity (test.cs) to access static functions in the file. I repeatedly get the error:
It's peculiar because the entire system appears fine until you click the red button: no unity editor errors, no red lines under any test. When run, it always reports this error.
I know it's not a code issue - I wrote a Unit Test in Visual Studio and it worked perfectly.
I know it's not a file issue - I compiled another function without Windows APIs in another static file within the same Dll/Class Library, and it works perfectly when Unity accesses its functions.
In Unity, Assets/Plugins contains the custom DLL I'm using, System.Runtime.dll, Windows.winmd, Windows.Devices.Bluetooth.dll, and WIndows.Devices.Enumeration.dll. The file I'm using to test it (test.cs) is in the Assets folder.
I am using .NET Framework 4.7.2 Class Library to compile the Dll in Visual Studio, Unity 2022.319f1 with VS as its editor (which reports zero errors when viewed), and a Windows 11 OS Lenovo computer.
Unity compatibility settings are Mono, and .NET Framework.
How do I resolve this? Any help would be appreciated!
---
I'd love to use commands from them directly, but the issue is that I need to use classes from the Windows.Devices.Enumeration namespace so I can't import just static functions like DllImport lets me.
If there's a solution to this that lets me import the class, I'd gladly use it! Looking for anything that resolves or sidesteps this problem RN.
(I am trying to use Windows.Devices.Enumeration.DeviceWatcher class)
100 Replies
you need to follow the instructions here https://docs.unity3d.com/6000.0/Documentation/Manual/windowsstore-scripts.html
Thanks for your help! unfortunately I am not building to UWP (I want this code to work in Editor) and that's why I'm using a DLL.
Do you have another solution?
hm, well
what you would need to do is use https://github.com/microsoft/CsWinRT/blob/master/docs/embedded.md to reference the Windows APIs instead of referencing the Windows.winmd directly
This looks like it might work!
How do I use it though?
The file breaks whether I use NuGet for Unity or just manually unzip and add it
no, you should use that with the class library that you already had in visual studio
ohh i see
the package is setting up a bunch of stuff to automatically generate the glue code for the Windows API, it is not going to work if you just unpack it
it broke my code and said that windows could not be found
I imported with VS. built in Nuget manager
you need to do that and then you need to set all of the properties it says on the page in the project file
the
CsWinRTEmbedded
, CsWinRTWindowsMetadata
, CsWinRTIncludes
, etc.
i would start by taking your whole csproj file and opening it in notepad and replacing it with this:
yes, replace the entire thingdid this, it says that netstandard 2.0 is not available and same error (windows could not be found)
do I remove the netstandard 2.0 file?
what do you mean by netstandard2.0 is not available
can you show what your project file looks like now
@reflectronic
o oops i copied the wrong one
can you use the one i pasted instead
yeah sorry
Seems like the only working namespace is Windows.Foundation.Collections
you will need to change the CsWinRTIncludes to add these namespaces
(you can do it directly from visual studio by clicking on the project in solution explorer, you don't have to use notepad anymore)
yep
the second erroring line is windows.devices.enumeration though
and it doensnt work
when you build the project, what kind of errors does it give you
56 errors
ok, you will have to delete your AssemblyInfo.cs file
just did
now I have 2 warnings and 50 errors
still some of this
ok, so, can you try
remove
add next to the other PackageReference items
and try building again
same errors, but now with 59 warnings too
project file
can you paste all of the errors and warnings, you should be able to copy them from the error list window
@reflectronic rereading the link you sent first, it seems that langversion must be set to 9.
let me try that
no, that is not the cause of the issue
yeah didn't fix
it also says "CsWinRTWindowsMetadata - must be set to the Windows SDK release to use for Windows APIs"
I seem to have found it
just re-checked the windows version I had
it wasn't 10.0.19401.0 it was 10.0.22621.0
changing it makes there be no errors before building, but now it spits out about 5000 errors after building
if that works can you delete the
I did, still 5000 errors
what are the errors
thing is though I don't have system.memory
i don't think
wait nvm i do
it seems like the problem is just adding the rest of the includes
from the thing you just sent, you need to add
Windows.UI;
Windows.ApplicationModel;
Windows.Storage.Streams;
Windows.Foundation;
trying to rebuild rn
it is taking way longer
also curiously, the code highlights for API is now gone
failed with 2k errors
it does appear that I need windows.ui and windows.foundation, not sure about the other 2. ill do a bit of testing with them
OK the least errors I have is when I delete the middle 2 and change Windows.UI to Windows.UI.Core
delete Windows.System, add Windows.UI.Color
less errors
text highlights are back too
ok this is going to be a very time consuming process
im ready
here is perhaps a better alternative:
sure, i'd love to hear it!
replace <CsWinRTIncludes> with
it is probably going to take a long time to build
also not sure, but ive just updated it with unloading + reloading the project
is that what I'm supposed to be doing?
you should not need to reload it after you edit it
huh
ok
one error
Severity Code Description Project File Line Suppression State Details
Error (active) CS1029 #error: 'Unsupported Universal API Contract version' XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\obj\Debug\net472\Generated Files\CsWinRT\ComInteropHelpers.cs 53
@reflectronic
I assume I add
-include Windows.Foundation.UniversalApiContract
add
just hit run, the app froze
visual studio
oh its back
errors just complain about UI again
-exclude Windows.Foundation.PropertyType
Severity Code Description Project File Line Suppression State Details
Warning (active) CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\Test.cs 27
Error (active) CS1029 #error: 'Unsupported Universal API Contract version' XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\obj\Debug\net472\Generated Files\CsWinRT\ComInteropHelpers.cs 53
Warning (active) CS4014 The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the await operator to the result. The call to the async method starts an asynchronous task. However, because no await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't what you expect. Usually other aspects of the calling method depend on the results of the call or, minimally, the called method is expected to complete before you return from the method that contains the call. An equally important issue is what happens to exceptions that are raised in the called async method. An exception that's raised in a method that returns a Task or Task<TResult> is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown. As a best practice, you should always await the call. You should consider suppressing the warning only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions. In that case, you can suppress the warning by assigning the task result of the call to a variable. XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\Test.cs 29 same error as before
Error (active) CS1029 #error: 'Unsupported Universal API Contract version' XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\obj\Debug\net472\Generated Files\CsWinRT\ComInteropHelpers.cs 53
Warning (active) CS4014 The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the await operator to the result. The call to the async method starts an asynchronous task. However, because no await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't what you expect. Usually other aspects of the calling method depend on the results of the call or, minimally, the called method is expected to complete before you return from the method that contains the call. An equally important issue is what happens to exceptions that are raised in the called async method. An exception that's raised in a method that returns a Task or Task<TResult> is stored in the returned task. If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown. As a best practice, you should always await the call. You should consider suppressing the warning only if you're sure that you don't want to wait for the asynchronous call to complete and that the called method won't raise any exceptions. In that case, you can suppress the warning by assigning the task result of the call to a variable. XDTKDllNet C:\Users\rli\source\repos\XDTKDllNet\XDTKDllNet\Test.cs 29 same error as before
did you delete the DefineConstants? it should still be there
oops, let me put that back
19 errors
what happens if you
-include Windows.UI.Text.FontWeights
its mostly still complaining about the UI assignments
The error messages say IFontWeights, are you sure it's FontWeights?
yes
ok
similar errors with ifontweights
can you show your project file
it says uh
let me friend you
still doesn't work
uh can you just send the CsWinRTFilters part
ok i sent it in 2 parts
this seems to work
sorry about that
can you remove the # from
-exclude Windows.UI.Text
or, uh
delete the whole line
and replace it with -exclude Windows.UI.Text
OK IT HAS NO ERRORS!
14 warnings though
let me try that with my Unit test
yeah that's fine
it is probably very slow to build
even if you make a small change to your own code it will probably be very slow
nah like 1 minute
i would suggest extracting your own code into a separate project
leave the generated code in its own project, and add a reference to it from the new project, so that it does not get rebuilt every time you tweak something
OK I see
but this should work for unity now
let me test that rq
it does work for my VS unit test
Sorry, tested the wrong version. Not sure it works yet
IT WORK IN VS
No, unfortunately it says this:
Assembly 'Assets/Plugins/XDTKDllNet.dll' will not be loaded due to errors:
Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
you will have to load System.Runtime.CompilerServices.Unsafe as well
System.Runtime.CompilerServices.Unsafe 6.0.0
Provides the System.Runtime.CompilerServices.Unsafe class, which provides generic, low-level functionality for manipulating pointers.
Commonly Used Types:
System.Runtime.CompilerServices.Unsafe
press "open in NuGet Package Explorer", open lib/net461, double click on the DLL to download it
into VS DLL or into Unity?
into unity
I tried that, the editor application fully crashed after hitting run
hm
Crash Logs from Editor.Log
I am currently running it from file being in the project though
maybe I can fix it by just making that work
not sure though
error is a bit convoluted language
i am not really sure how to fix this
well, i don't even know what's causing it
yeah same
let me see if Unity discord knows how
it is pretty late for me though so maybe I'll try again in 1 or 2 days
regardless, thank you so so much for helping me with this project
have a good day!