Referencing library using Windows.winmd

2

What I'm trying to do

I'm trying to create a library (Arduino.dll) to interact with my Bluetooth device from a laptop (Windows 10). This library is intended to be used by a desktop application. To achieve that, I had to reference both Windows.winmd and System.Runtime.WindowsRuntime.

What's working

I did write the library and tested it on a console application which was on another project in the same solution. To make it work, I had to reference the project and Windows.winmd in the test project. Note that this test project is used to test all of my libraries, so it is heavily manipulated and I tinkered with the settings a lot for years.

What's not working

I finished to write my dll, and wanted to use it in my desktop application. Therefore, I referenced both Arduino.dll and Windows.winmd as in my test project, expecting to work the same way. Sadly, on running the application, I got a "dll not found" when instantiating a class for Arduino.dll :

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in LuxAFX4.exe

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

Strange, but well, I tried to reference the (same) System.Runtime.WindowsRuntime as in Arduino.dll but no luck, I now got an even weirder error :

An unhandled exception of type 'System.BadImageFormatException' occurred in LuxAFX4.exe

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

During my search for a solution, I understood that I shouldn't try to load this "reference library" and if it is not found, it must be because it is not installed on the system. But then, why would it work in my test project?

Another thing I may have understand, is that if my library doesn't expose members of referenced 'sub'-libraries, they won't be needed when referencing my library. Should I try to 'hide' all references? If so, is there a way to catch all exposed members? Source : How to to avoid referencing a dll's referenced dll

Side note : I use C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll and C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd.

May be related : Could not load file or assembly Windows.winmd and How to reference Windows.winmd from a .NET Core library?

c#
dll
visual-studio-2015
asked on Stack Overflow Jan 14, 2017 by LuxOrïk • edited May 23, 2017 by Community

1 Answer

0

After some more research, and with the hint given by Hans Passant, I finally understood that I had chosen the wrong System.Runtime.WindowsRuntime.dll to start with. Changing it to use C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll instead in both the project with the DLL and the project using it, resolved my problem.

I got the new file location here : C# “await” error when using WinRT from Desktop app

answered on Stack Overflow Jan 15, 2017 by LuxOrïk • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0