Export (0) Print
Expand All

WRL Class Library Project Template

If you use Visual Studio to write a Windows Runtime C++ Template Library (WRL) project, you can greatly simplify your task by downloading the WRL Class Library project template.

Note Note

If you have to manually update the project settings for an existing project, see DLLs (C++/CX).

Visual Studio doesn't provide a template for WRL projects. Here’s how to download a project template that creates a basic class library for Windows Store apps with WRL.

To download the WRL Project Template

  1. On the menu bar, choose File, New Project.

  2. In the left pane of the New Project dialog box, select Online, and then select Templates.

  3. In the Search online templates box in the upper right corner, type WRL Class Library. When the template appears in the search results, choose the OK button.

  4. In the Download and Install dialog box, if you agree to the licensing terms, choose the Install button.

  5. After the template installs, create a project by choosing File, New Project, and then selecting the WRLClassLibrary template. The project creates a DLL.

Read Walkthrough: Creating a Basic Windows Runtime Component Using WRL for an example that uses this template to create a Windows Runtime component.

The project template provides:

  • an .idl file that declares the MIDL attributes for a basic interface its class implementation. Here’s an example.

    import "inspectable.idl";
    import "Windows.Foundation.idl";
    
    #define COMPONENT_VERSION 1.0
    
    namespace WRLClassLibrary
    {
        interface IWinRTClass;
        runtimeclass WinRTClass;
    
        [uuid(89656677-8679-477c-aff1-f724c64b70f3), version(COMPONENT_VERSION), exclusiveto(WinRTClass)] 
        interface IWinRTClass : IInspectable
        {
        }
    
        [version(COMPONENT_VERSION), activatable(COMPONENT_VERSION)]
        runtimeclass WinRTClass
        {
            [default] interface IWinRTClass;
        }
    }
    
  • a .cpp file that defines the class implementation. Here’s an example.

    #include "pch.h"
    
    #include "WRLClassLibrary_h.h"
    #include <wrl.h>
    
    using namespace Microsoft::WRL;
    using namespace Windows::Foundation;
    
    namespace ABI
    {
        namespace WRLClassLibrary
        {
            class WinRTClass: public RuntimeClass<IWinRTClass>
            {
                InspectableClass(L"WRLClassLibrary.WinRTClass", BaseTrust)
    
            public:
                WinRTClass()
                {
                }
            };
    
            ActivatableClass(WinRTClass);
        }
    }
    

    The RuntimeClass base class helps manage the global reference of all objects in the module and declares the methods of the IUnknown and IInspectable interfaces. The InspectableClass macro implements IUnknown and IInspectable. The ActivatableClass macro creates a class factory that creates instances of the class.

  • a file named module.cpp that defines the library exports DllMain, DllCanUnloadNow, DllGetActivationFactory, and DllGetClassObject.

Show:
© 2014 Microsoft