3
votes

What works > Library setup

I have a Windows Phone 8 solution with 2 projects:

  • "Hello", a simple library project -> generates Hello.dll.
  • "HelloNativeRT", a WP Runtime Component with C++ files -> generates HelloNativeRT.dll and HelloNativeRT.winmd

In this solution, the "Hello" library references the WP Runtime Component, so calls like...

HelloNativeRT.SampleNamespace test = new HelloNativeRT.SampleNamespace();

...work fine in this library project.

What doesn't work > WP8 app setup

However, I want to use these two libraries in a Windows Phone 8 app, but without adding references to the projects, since I need to ship the compiled libraries to clients.

  • I referenced the Hello.dll file in the project, as well as the HelloNativeRT.winmd file.
  • When I launch the application in debug mode, and goes to the line HelloNativeRT.SampleNamespace test = new HelloNative... it crashes and says "TypeLoadException", like it cannot load the native module.

I suppose I need to include the HelloNativeRT.dll file in a way or another, since I guess it contains the native (compiled) code, as the winmd file may only embed the C++/CX code.

How should I setup my project to include this DLL?

I tried to put it at the root of the WP8 project, to reference it, to embed it... with no luck.

2

2 Answers

4
votes

The reason was quite simple in my case: the .winmd file and the .dll file generated from the WinRT Component must have THE SAME NAME (e.g: testRT.dll and testRT.winmd).

Then:

  • Add the .winmd medata file as a reference in your project.
  • Check that the .winmd / dll files are in the same folder to be correctly loaded.
3
votes

You need to:

  • Add a reference to your managed DLL file (the wrapper),
  • Add a reference to your winmd metadata file (the WinPRT component),
  • Add your native DLL library file as a member of your project, with build action to "Content" and "Copy always" selected.
  • Add a section to your manifest file:

WPAppManifest:

<ActivatableClasses>
    <InProcessServer>
        <Path>external_component.dll</Path>
        <ActivatableClass ActivatableClassId="external_component.MyComponent" ThreadingModel="both" />
    </InProcessServer>
</ActivatableClasses>

This last point is the one that is auto-magically done by Visual Studio when you reference a WinPRT project from a WP8 project ;-) I suppose not a lot of people are referencing native libs manually, since the documentation on that point is very sparse. The only link where I saw the solution mentionned was here...