다음을 통해 공유


WRL Class Library Project Template

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at 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

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

Download the WRL Project Template

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 8.x 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.

Examples that use the project template

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

What the project template provides

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.

See Also

Windows Runtime C++ Template Library (WRL)