Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a "winmd" reference vs Adding a project reference

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()
like image 442
Max Avatar asked Jul 19 '13 12:07

Max


People also ask

What is the difference between project reference and DLL reference?

Well, project references are helpful when you are building and testing in both debug and release mode. If you directly add a DLL then you are locked into whatever that particular DLL was built as. The project reference allows this to be a build time decision. This is correct what you are saying.

What is a project reference?

A project reference is a link from the current Studio project to another project. The reference makes certain resources in the referenced project become available for use in the current project.

How do you add references to a project?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference. For more information, see How to: Add or remove references.


2 Answers

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.

like image 102
Hans Passant Avatar answered Oct 23 '22 17:10

Hans Passant


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)

like image 24
vishalb Avatar answered Oct 23 '22 19:10

vishalb