Windows 8 Applications (formerly Metro) run inside a sandbox, making communicating with other application difficult at best. There is no easy way to do it, and for good reason, too. When one app is running, all other may be suspended, so what’s the point of communicating anyway? Furthermore, there is no guarantee that the other app even exists on the machine.
Still, suppose we wanted to share some information (I’m not talking about the standard share contract) via (say) shared memory. Can we achieve that?
Flipping to the documentation of the CreateFileMapping Win32 API shows that unfortunately this is only available for desktop apps. A new function exists, however, called CreateFileMappingFromApps that works only in Windows 8 apps. Great! Can we use that? Let’s try.
I’ve created two simple C++ Windows 8 Applications (Metro or Store apps) that have a simple UI consisting of a TextBox with two buttons, one for writing to the shared memory, the other for reading. The XAML looks something like this:
In the MainPage constructor, we can create the file mapping (section) object and save it in a member handler:
This creates a 1MB shared memory section named “MySharedMem” backed up by the system paging file.
To write some text into that memory, we can use the following code:
Running a single application has the desired effect – the memory is written and read from correctly, which may not be surprising.
If two similar application are running, that use the same code, i.e. – the same file mapping object name – sharing is possible.
However, trying this simply fails. The handles are created successfully, the writing and reading succeed as well. So, what’s wrong?
Opening Process Explorer reveals the unpleasant truth. Here’s the handle view for the first app, concentrating on the section object in question:
It seems that the name of the section is not really MySharedMem, but something that consists of some SID-looking string. Running another application shows the following in Process Explorer:
That’s a completely different name! That’s why the sharing wasn’t working.
Communicating between a desktop app and a Windows 8 app is problematic for exactly the same reason; desktop apps names are under the BasedNamedObjects logical directory (and not AppContainerNamedObjects).
One option that we can try is prefixing the object name with “Global\”, such as “Global\\MySharedMem”. This should create the object in session 0, allowing easy sharing. This, unfortunately, fails, because of a missing privilege that a Windows 8 app does not (and cannot) have.
The above is not specific to section objects, but to any kernel object created from a Windows 8 app process, such as a mutex, semaphore, file, etc. Another option we may consider is using the DuplicateHandle API, and although this is permitted, we first need a handle to the other process, which requires a call to OpenProcess, which, unfortunately cannot be used in Windows 8 apps.
So, there you have it. Windows 8 apps are really in a tight box.