This is a lengthy post in English. I decided to do it in Englisch because there are no step-by-step tutorials yet out there. Harry Pierson – who demonstrated the brokered components at //build provided some samples in the sample. I took them and re-engineered them to get my stuff done. Here’s my step-by-step tutorial to get a Windows Store App to trigger the command line.

Harry also promised to make available the Project templates for Visual Studio soon. This will make the work much more comfortable. Unfortunately, I couldn’t wait because I really think this is very cool. So – have fun!

Preparation

2. Create another project within this solution. Use the “Windows Runtime Component (Windows) C#“project template. Call this project “WindowsRuntimeComponentBroker”. This project will be able to do some calls to the full .NET Framework as soon as we did some additional work.

3. Create another project within this solution. Use the “Win32 C++” project template. In the wizard select “empty project” and “DLL”. Call this project “WindowsRuntimeComponentBrokerProxy”. This project will provide a proxy for the inter process communication (IPC).

4. Create another project within this solution. Use the “Class Library C#” project template. Call this project “ClassicClassLibrary”. This project will serve as sample for existing projects you already might have from your development in the past. This is a classic .NET 4.5 project. You will be able to call it from MyApp after we’re done.

5. Your Solution should look like this:

The brokered component

6. In the first run, we will just make a call to the broker. Later we will also call the ClassicLib. The logical sequence of calls at runtime will look like this:

MyApp—>Proxy—>Broker—>ClassicLib

7. So let’s start with adding some logic to the Broker. To see the full power of Windows what could be a better sample than calling the commandline? Let’s do this!

8. Rename Class1 to BrokerClass in the Broker project.

9. Add a public method in BrokerClass that calls the commandline.

public int CallCmdFromBroker()

{

System.Diagnostics.Process proc = new System.Diagnostics.Process();

var cmd = "xcopy";

System.Diagnostics.Process.Start("CMD.exe", cmd);

proc.Close();

return 42;

}

10. System.Diagnostics.Proccess will probably not be found. We have to edit the project file a little to make this work.

11. Edit the Broker project file. You can do this by right-click on the Broker project in Solution explorer and by unloading it. Afterwards right-click it again and select edit. You will see pretty much xml.

16. We have to specify an Interface for our BrokerClass. Rightclick “BrokerClass” in code and press Ctrl+R,Ctrl+I. This will automatically generate IBrokerClass.cs for you. Make this Interface public. The content of IBrokerClass.cs should look pretty much like this:

namespace WindowsRuntimeComponentBroker

{

public interface IBrokerClass

{

int CallCmdFromBroker();

}

}

17. Derive BrokerClass from IBrokerClass if you created the interface manually.

18. Open the project properties of the Broker project and edit the post build events. (I also took these from Harry Piersons sample.) (Hint: You can resize the Post-Build-Event Command Line Window.) Add the following steps:

27. Your proxy should be able to build now. You’re done with the proxy.

The app

28. Now let’s reference the Brokered Component from the app.

29. Click Add reference. Then do NOT reference the project itself, but the reference winmd file of the broker. Use the browse button in the references dialog to navigate there. It should be in the … WindowsRuntimeComponentBroker\bin\x86\Debug\reference folder, called WindowsRuntimeComponentBroker.winmd. Do NOT use the implementation winmd file!

30. Open MainPage.xaml.cs and add a method, that calls the broker after the InitializeComponents call. Hint: We don’t use the interface here! But we needed to specify the interface to make sure the IPC will work at runtime! Don’t get confused! J

31. So Intellisense already works – very nice. Now let’s make sure that during runtime everything will work, too. Therefore open the app manifest. Right click it and select “View Code”.

33. Notice two things. First: Specify the fully qualified class name of the class you want to call from the app. Second: specify the folder where you put your dlls! The other entries are standard. Don’t change them.

Run it

34. So far everything should build fine. Try it! If there are build errors, make sure you didn’t miss a thing.

35. Create c:\brokerfolder.

36. Copy the implementation winmd file of the broker component into that folder. You can find it here: [SolutionFolder]\WindowsRuntimeComponentBroker\bin\x86\Debug\imp\WindowsRuntimeComponentBroker.winmd

37. Copy the Proxy dll into that folder. You can find it in here [SolutionFolder]\Debug\WindowsRuntimeComponentBrokerProxy.dll

38. Register the Proxy.dll by running the following command:

regsvr32 "C:\brokerfolder\WindowsRuntimeComponentBrokerProxy.dll" /s

39. Run the following command from within the folder:

icacls $path /T /grant "ALL APPLICATION PACKAGES:RX"

40. Start the app and click the button. If everything went fine, the command line will open up. Awesome.

The classic library

41. So far, so good. But I want to use existing code and want to call an existing (.Net 4.5) library. Let’s do this.

42. Rename class1 in ClassicClassLibrary into ClassicClass.

43. Add a public method in it called RunCmdFromClassic and add the code to open the command line we already used before.

44. Add a reference to the ClassicClassLibrary to the Broker project. You can’t do this via UI. Open the project file and add the following lines.

Hi, we successfully implemented this solution. But when we run it on a Dell Venue pro it generates a TypeLoadException. Do you have any idea what causes this? The tablet is 32 bit, running windows 8.1.