Adding a "winmd" reference vs Adding a project reference
Asked Answered
N

3

8

I have a Windows Phone 8 project and another project written in C++ ; both are in the same solution. The C++ project is a dynamic library used in the WP8 project, and it is configured to produce a Windows Metada file (.winmd) on top of the .dll file.

When adding the C++ project as a Project Reference in the WP8 project, everything works perfectly well.

However, I'd like to reference directly the binaries instead of the project so I tried referencing the .dll itself but VS2012 would not let me (which I totally understand since the library is unmanaged from what I understand). Adding the .winmd file instead works, I mean it compiles without warning/errors ; but it crashes at runtime (I get a TargetInvocationException which is raised because the "actual" code of the C++ library cannot be found).

When adding the .winmd file, I made sure the .dll file was next to it. Putting both the files in the bin directory of the WP8 project does not work either.

I can't find any clues on the internet and I'd be grateful if you could give me some, any hints are welcome!

Here is a schema of the trivial architecture I'm trying to set up:

architecture

And here is the stacktrace of the exception raised:

at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
   at Sqlite.Sqlite3.sqlite3_open_v2(String filename, Database& db, Int32 flags, String zVfs)
   at SQLite.SQLite3.Open(String filename, Database& db, Int32 flags, IntPtr zVfs)
   at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
   at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
   at WP8ClassLibrary.SomeManager..ctor(String databasePath)
   at WP8App.SomeViewModel..ctor()
   at WP8App.MainPage..ctor()
Narcoanalysis answered 19/7, 2013 at 12:59 Comment(0)
N
0

Well I managed to make it work. As the stacktrace in the original post would suggest, I am using SQLite in my application. If I add the winmd file as a reference instead of the C++ project, it looks like some dependencies are not satisfied.

Therefore I had to add a reference to SQLite for Windows Phone into the WP8ClassLibrary project. That was somehow a dumb mistake of mine because this C++ project probably carried this dependency with itself (there are several winmd references in it but I could not guess they would be part of the SQLite for Windows Phone based on their obscure names).

Narcoanalysis answered 19/7, 2013 at 15:15 Comment(0)
C
1

Tools + Options, Projects and Solutions, Build and Run. Change the "MSBuild project build output verbosity" setting to Normal.

Pay attention to the build output, the messages you see after "XapPackager". Which show which files are getting added to the Xap package. Your DLL needs to be in that list. If it is not then your program will fail as described. In which case you'll need to find out why it is getting skipped. Start that by checking that the Copy Local property of the .winmd reference is True.

Chiliasm answered 19/7, 2013 at 13:53 Comment(3)
Thank you for your answer. The Copy Local property is set to true; and both the winmd and all files are in the list displayed after Begin Xap packaging. I simplified a bit the situation in my original post ; I have a C++ file producing a dll and a winmd, this winmd is used in a WP8 class library project which produces a dll used in a "concrete" WP8 project. The two dll and the winmd files are correctly copied during the build.Narcoanalysis
You are not helping me help you. "all" files? Two dlls? You now have a way to diagnose what's different between the two approaches but didn't mention any. I don't have a detailed exception message to look at either. I'd have to recommend a project reference if you can't document it better.Chiliasm
I'm sorry I meant "winmd and dll files", my finger somehow slipped to the a key...Narcoanalysis
B
1

I just wanted to add my experience to the answer..

If you are trying to add a C++ library directly to C# code (for a Windows Phone 8.1 app in my case), then including the .winmd file enables the compilation but the app crashes on launch. The stack trace only says failed to load C++ dll.

I had to also add reference to Visual Studio C++ runtime library in the C# application. I found out about the missing reference by diffing the working .xap (created from a solution that includes both C# and C++ projects) and non working .xap (created from a solution that only includes C# project along with reference to C++ .winmd file)

Beaulieu answered 12/2, 2016 at 6:13 Comment(0)
N
0

Well I managed to make it work. As the stacktrace in the original post would suggest, I am using SQLite in my application. If I add the winmd file as a reference instead of the C++ project, it looks like some dependencies are not satisfied.

Therefore I had to add a reference to SQLite for Windows Phone into the WP8ClassLibrary project. That was somehow a dumb mistake of mine because this C++ project probably carried this dependency with itself (there are several winmd references in it but I could not guess they would be part of the SQLite for Windows Phone based on their obscure names).

Narcoanalysis answered 19/7, 2013 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.