I am writing a Windows 8 Store app using Visual Studio 2012. The main app is a XAML/C# app but it also uses a WinRT component written using C++/CX. For the WinRT component, I am using the VS project template called “DLL (Windows Store apps)” from the New Project dialog. Once I have this project, I change it to contain a single namespace with a single ref class. My C# project contains a reference to this C++ project.
However, when I try to run my app, the C# code throws an exception at the point it tries to instantiate/activate the C++ ref class. I have seen several working examples of this even on MSDN samples. I have verified that the WinRT component DLL and the winmd files are correctly copied into the final app package, and also that the app’s AppxManifest.xml is properly updated with the correct registration information for the WinRT class. However it still fails.
I don’t know what’s going wrong. How can I fix this error?
Windows Runtime component binaries are required to have the following two exports:
DllCanUnloadNow
DllGetActivationFactory
When you use the VS project template “DLL (Windows Store apps)”, your project doesn’t define the requisite macro that causes the compiler to put the above two exports in the binary. Under project settings you will notice that the macro set is _WINDLL. You will need to change this to _WINRT_DLL.
Alternatively, the correct way to create a WinRT component using C++/CX is to use the VS project template called “Windows Runtime Component” under Visual C++ -> Windows Store. Using this will create a project which is already correctly setup with the right project settings etc. for creating proper WinRT binaries.