PoshCode Logo PowerShell Code Repository

MetroApps Module by Tome Tanasovski 23 months ago
View followups from AAAAAAAAAA | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3740"></script>download | new post

This module provides two functions:

Get-MetroApp – This cmdlet reads the registry for the keys that have the launcher id and the entry point (interesting for xaml apps, but not so much for html5 apps)

Start-MetroApp – This cmdlet uses one of the ids returned by Get-metroapp to launch an app in the win8 metro interface

  1. <#
  2. Created by: Tome Tanasovski
  3. Version: 1.0
  4. Date: 11/2/2012
  5.  
  6. This module provides two functions:
  7.  
  8. Get-MetroApp - This cmdlet reads the registry for the keys that have the launcher id and the entry point (interesting for xaml apps, but not so much for html5 apps)
  9.  
  10. Start-MetroApp - This cmdlet uses one of the ids returned by Get-metroapp to launch an app in the win8 metro interface
  11.  
  12. Apologies for the lack of get-help documentation, but I'll be doing a blog post about this shortly:
  13. http://powertoe.wordpress.com
  14.  
  15. Original C# code modified slightly from here - also the registry info is on this page too:
  16. http://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c
  17. #>
  18.  
  19. $code = @"
  20. using System;
  21. using System.Runtime.CompilerServices;
  22. using System.Runtime.InteropServices;
  23. namespace Win8 {
  24.    public enum ActivateOptions
  25.    {
  26.        None = 0x00000000,  // No flags set
  27.        DesignMode = 0x00000001,  // The application is being activated for design mode, and thus will not be able to
  28.        // to create an immersive window. Window creation must be done by design tools which
  29.        // load the necessary components by communicating with a designer-specified service on
  30.        // the site chain established on the activation manager.  The splash screen normally
  31.        // shown when an application is activated will also not appear.  Most activations
  32.        // will not use this flag.
  33.        NoErrorUI = 0x00000002,  // Do not show an error dialog if the app fails to activate.                                
  34.        NoSplashScreen = 0x00000004,  // Do not show the splash screen when activating the app.
  35.    }
  36.  
  37.    [ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  38.    interface IApplicationActivationManager
  39.    {
  40.        // Activates the specified immersive application for the "Launch" contract, passing the provided arguments
  41.        // string into the application.  Callers can obtain the process Id of the application instance fulfilling this contract.
  42.        IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
  43.        IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
  44.        IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
  45.    }
  46.  
  47.    [ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]//Application Activation Manager
  48.    public class ApplicationActivationManager : IApplicationActivationManager
  49.    {
  50.        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/]
  51.        public extern IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
  52.        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  53.        public extern IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId);
  54.        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
  55.        public extern IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
  56.    }
  57. }
  58. "@
  59.  
  60. add-type -TypeDefinition $code
  61. $appman = new-object Win8.ApplicationActivationManager
  62.  
  63. function Get-MetroApp {
  64.     $entry = 'HKCU:\Software\Classes\ActivatableClasses\Package'
  65.     foreach ($appkey in (dir $entry |select -ExpandProperty pspath)) {        
  66.         #$id = ((dir (join-path $appkey 'Server')) |Get-ItemProperty).appusermodelid
  67.         $id = (dir (Join-Path $appkey server) |?{$_.pspath -notmatch 'BackgroundTransferHost.1'} |Get-ItemProperty).appusermodelid
  68.         if ($id) {
  69.             $possibleclassidkeys = dir (join-path $appkey 'ActivatableClassID') |select -ExpandProperty pspath            
  70.             # we look for the app key first, then app.wwa, and then any other key if neither returns an entrypoint
  71.             $key = $possibleclassidkeys |?{$_ -match 'app$'}
  72.             $entrypoint=$null
  73.             if ($key) {
  74.                 if (Test-Path (join-path $key CustomAttributes)) {
  75.                     $entrypoint = (Get-ItemProperty (join-path $key CustomAttributes)).('appobject.entrypoint')
  76.                 }
  77.             }
  78.             if (!$entrypoint) {
  79.                 $key = $possibleclassidkeys |?{$_ -match 'app.wwa$'}
  80.                 if ($key) {
  81.                     if (Test-Path (join-path $key CustomAttributes)) {
  82.                         $entrypoint = (Get-ItemProperty (join-path $key CustomAttributes)).('appobject.entrypoint')
  83.                     }
  84.                 }
  85.             }
  86.             if (!$entrypoint) {
  87.                 foreach ($key in $possibleclassidkeys) {
  88.                     if (Test-Path (join-path $key CustomAttributes)) {
  89.                         $entrypoint = (Get-ItemProperty (join-path $key CustomAttributes)).('appobject.entrypoint')
  90.                         break
  91.                     }
  92.                 }
  93.             }
  94.             new-object psobject -Property ([ordered] @{
  95.                 EntryPoint = $entrypoint                
  96.                 ID = $id
  97.             })
  98.          }
  99.     }
  100. }
  101.  
  102. function Start-MetroApp {
  103.     param(
  104.         [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
  105.         [string] $ID
  106.     )
  107.     $appman.ActivateApplication($ID,$null,[Win8.ActivateOptions]::None,[ref]0)
  108. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me