23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Building your own Windows Runtime components to deliver great apps post on the Windows 8<br />

developer blog gives some examples of this. It shows how to use the System.IO.Compression API in .NET<br />

to work with ZIP files and the XAudio APIs (part of DirectX) to bypass the <strong>HTML</strong> audio element and<br />

perform native audio playback. In the latter case, you might remember from Chapter 10, in the section<br />

“Playing Sequential Audio,” that no matter how hard we tried to smooth the transition between tracks<br />

with the audio element, there is always some discernible gap. This is due to the time it takes for the<br />

element to translate all of its operations into the native XAudio APIs. By going directly to those same<br />

APIs, you can circumvent the problem entirely. (That said, Microsoft knows about the behavior of the<br />

audio element and will likely improve its performance in the future.)<br />

This approach can also be used to communicate with external hardware that’s not represented in the<br />

WinRT APIs but is represented in Win32/COM. We saw this with the XInput and JavaScript controller<br />

sketch sample in Chapter 15, “Devices and Printing” (also discussed in the blog post above).<br />

Another very simple example would be creating a component to answer an oft-heard question:<br />

“How do I create a GUID in JavaScript?” Although you can implement a routine to construct a GUID<br />

string from random numbers, it’s not a proper GUID in that there is no guarantee of uniqueness (GUID<br />

stands for Globally Unique Identifier, after all). To do the job right, you’d want to use the Win32 API<br />

CoGreatGuid, for which you can create a very simple C++ wrapper.<br />

Overkill? Some developers have commented that going to all the trouble to create a WinRT<br />

component just to call one method like CoCreateGuid sounds like a heavyweight solution. However,<br />

considering the simplicity of a basic WinRT component as we’ve seen in this chapter, all you’re really<br />

doing with a component is setting up a multilanguage structure through which you can use the full<br />

capabilities of each language. The overhead is really quite small: a Release build of the C++ component<br />

in “Quickstart #2” produces a 39K DLL and a 3K .winmd file, for example.<br />

Using a WinRT component in this way applies equally to COM DLLs that contain third-party APIs like<br />

code libraries. You can use these in a Windows Store app provided they meet three requirements:<br />

• The DLL is packaged with the app.<br />

• The DLL uses only those Win32/COM APIs that are allowed for Windows Store apps. Otherwise<br />

the app will not pass Store certification.<br />

• The DLL must implement what is called Regfree COM, meaning that it doesn’t require any<br />

registry entries for its operation. (Windows Store apps do not have access to the registry and<br />

thus cannot register a COM library.) The best reference I’ve found for this is the article Simplify<br />

App Deployment with ClickOnce and Registration-Free COM in MSDN Magazine.<br />

If all of these requirements are met, the app can then use the CoCreateInstanceFromApp function<br />

from a component to instantiate objects from that DLL.<br />

751

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!