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:
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()
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With