Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
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 Walkthrough: Creating a Basic Windows Runtime Component Using WRL.
This document shows how to use the Windows Runtime C++ Template Library (WRL) to create a basic Windows Runtime component. The component adds two numbers and raises an event when the result is prime. This document also demonstrates how to use the component from a Windows 8.x Store app that uses JavaScript.
Prerequisites
Experience with the Windows Runtime.
Experience with COM.
To create a basic Windows Runtime component that adds two numbers
In Visual Studio, create a Visual C++
WRLClassLibraryproject. The document Class Library Project Template describes how to download this template. Name the projectContoso.In Contoso.cpp and Contoso.idl, replace all instances of "WinRTClass" with "Calculator".
In Contoso.idl, add the
Addmethod to theICalculatorinterface.HRESULT Add([in] int a, [in] int b, [out, retval] int* value);In Contoso.cpp, add the
Addmethod to thepublicsection of theCalculatorclass.HRESULT __stdcall Add(_In_ int a, _In_ int b, _Out_ int* value) { if (value == nullptr) { return E_POINTER; } *value = a + b; return S_OK; }Important
Because you’re creating a COM component, remember to use the
__stdcallcalling convention.We recommend that you use
_Out_and other source annotation language (SAL) annotations to describe how a function uses its parameters. SAL annotations also describe return values. SAL annotations work with the C/C++ Code Analysis tool to discover possible defects in C and C++ source code. Common coding errors that are reported by the tool include buffer overruns, uninitialized memory, null pointer dereferences, and memory and resource leaks.
To use the component from a Windows 8.x Store app that uses JavaScript
In Visual Studio, add a new JavaScript
Blank Appproject to theContososolution. Name the projectCalculatorJS.In the
CalculatorJSproject, add a reference to theContosoproject.In default.html, replace the
bodysection with these UI elements:<div> <input id="a" /> <input id="b" /> <p id="result">Result:</p> <button onclick="Add()">Add</button> </div>In default.js, implement the
OnClickfunction.function Add() { "use strict"; var calculator = new Contoso.Calculator(); var a = document.getElementById("a"); var b = document.getElementById("b"); document.getElementById("result").innerHTML = "Result: " + calculator.add(a.value, b.value); }Note
In JavaScript, the first letter of a method name is changed to lowercase to match the standard naming conventions.
To add an event that fires when a prime number is calculated
In Contoso.idl, before the declaration of
ICalculator, define the delegate type,PrimeNumberEvent, which provides anintargument.[uuid(3FBED04F-EFA7-4D92-B04D-59BD8B1B055E), version(COMPONENT_VERSION)] delegate HRESULT PrimeNumberEvent(int primeNumber);When you use the
delegatekeyword, the MIDL compiler creates an interface that contains anInvokemethod that matches that delegate's signature. In this example, the generated file Contoso_h.h defines theIPrimeNumberEventinterface, which is used later in this procedure.MIDL_INTERFACE("3FBED04F-EFA7-4D92-B04D-59BD8B1B055E") IPrimeNumberEvent : public IUnknown { public: virtual HRESULT STDMETHODCALLTYPE Invoke( int primeNumber) = 0; };In the
ICalculatorinterface, define thePrimeNumberFoundevent. Theeventaddandeventremoveattributes specify that the consumer of theICalculatorinterface can both subscribe to and unsubscribe from this event.[eventadd] HRESULT PrimeNumberFound( [in] PrimeNumberEvent* eventHandler, [out, retval] EventRegistrationToken* eventCookie); [eventremove] HRESULT PrimeNumberFound( [in] EventRegistrationToken eventCookie);In Contoso.cpp, add a
privateMicrosoft::WRL::EventSource member variable to manage the event subscribers and invoke the event handler.EventSource<IPrimeNumberEvent> m_events;In Contoso.cpp, implement the
add_PrimeNumberFoundandremove_PrimeNumberFoundmethods.HRESULT __stdcall add_PrimeNumberFound(_In_ IPrimeNumberEvent* event, _Out_ EventRegistrationToken* eventCookie) { return m_events.Add(event, eventCookie); } HRESULT __stdcall remove_PrimeNumberFound(_In_ EventRegistrationToken eventCookie) { return m_events.Remove(eventCookie); }
To raise the event when a prime number is calculated
In Contoso.cpp, add the
IsPrimemethod to theprivatesection of theCalculatorclass.// Determines whether the input value is prime. bool IsPrime(int n) { if (n < 2) { return false; } for (int i = 2; i < n; ++i) { if ((n % i) == 0) { return false; } } return true; }Modify the
Calculator’sAddmethod to call the Microsoft::WRL::EventSource::InvokeAll method when a prime number is calculated.HRESULT __stdcall Add(_In_ int a, _In_ int b, _Out_ int* value) { if (value == nullptr) { return E_POINTER; } int c = a + b; if (IsPrime(c)) { m_events.InvokeAll(c); } *value = c; return S_OK; }
To handle the event from JavaScript
In default.html, modify the
bodysection to include a text area that contains prime numbers.<div> <input id="a" /> <input id="b" /> <p id="result">Result:</p> <p id="primes" style="color:#808080">Primes found:</p> <button onclick="Add()">Add</button> </div>In default.js, modify the
Addfunction to handle thePrimeNumberFoundevent. The event handler appends the prime number to the text area that was defined by the previous step.function Add() { "use strict"; var calculator = new Contoso.Calculator(); calculator.onprimenumberfound = function (ev) { document.getElementById("primes").innerHTML += " " + ev.target; }; var a = document.getElementById("a"); var b = document.getElementById("b"); document.getElementById("result").innerHTML = "Result: " + calculator.add(a.value, b.value); }Note
In JavaScript, the event names are changed to lower-case and are prepended with "on" to match the standard naming conventions.
The following illustration shows the basic Calculator app.
.jpeg)
Next Steps
See Also
Windows Runtime C++ Template Library (WRL)
Class Library Project Template
C/C++ Code Analysis tool