Month: September 2017

How to expose C# async methods in WinRT components

When authoring Windows Runtime (WinRT) components with C# you cannot use the standard C# async Task pattern. Say for example you have code like:

Visual Studio will give you a lengthy error similar to this:

'Demo.MyComponent.SaveToFilel(Windows.Storage.StorageFolder, System.String)' has a parameter of type 'System.Threading.Tasks.Task' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types.
Consider changing the type 'Task' in the method signature to one of the following types instead:
Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces.

The problem is Windows Runtime does not support Task for asynchronous operations. Developers must instead use IAsyncAction or IAsyncOperation. That creates a new problem though; if the function uses await it must be declared with the async keyword, and that requires the function to be return Task!

Thankfully there is a way out of this standoff. Keep the original function but mark it as private. Create a new public method that calls the private implementation and returns the result as an IAsyncAction or IAsyncOperation type with the AsAsyncOperation() method. It’s a bit messy but it works perfectly.

Here’s how it would look for the example above:

Embedding user defined resources in Visual C++ binaries

For my current project I wanted the ability to package my entire C++ project (dependent DLLs and data files) as a single Windows executable. To do this I had to find a way to embed the data files into the executable at build time. There’s multiple ways to do this. I settled on using the Windows resource system, since the project is (currently) Windows only.

Add external files

  1. Add text files named “Sample.txt” and “Sample2.txt” to the project with some random text.

Add a new resource file and header

  1. Add a new resource file (.rc) to your project. This example will use ExternalResources.rc.

  2. Add a new header file (.h) to your project with the same name but a .h extension.

  3. Open up the header file and create one entry for each external file you want. Each ID must be unique – it’s a good idea to start around 1000 and go up from there. I use C_ for the definition, but it’s totally optionally. Name your resource however you want!

  1. Open up the resource file (Right click -> View code), and delete everything in the file. Copy the following code, and modify it to fit your needs. The #include <windows.h> statement allows you to declare a language for a resource, which I can cover in a later tutorial.

Accessing resource data

Loading resources

You’re all done, congrats!

References

http://stackoverflow.com/questions/7288279/how-to-embed-a-file-into-an-executable
https://msdn.microsoft.com/en-us/library/6e7446zd.aspx
https://msdn.microsoft.com/en-us/library/aa381054%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/ms648046%28v=VS.85%29.aspx