PoshCode Archive  Check-in [571f1e4a83]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:This script can be run with PowerShell to launch a metro application against a file. It is really intended to be used from a batch file, which is embedded as a comment within the script.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:571f1e4a834eabc81b08e9adae2cc805f87ae8e0aad5fa7d394c8dea4c3f92e5
User & Date: Bob P 2018-06-10 14:16:40
Context
2018-06-10
14:16
My prompt function for PS 6.0.0.9 — There’s a bug causing an echo if you use write-host in your prompt on unix with PS 6 currently, so I rewrote my prompt to just output a string with escape sequences… check-in: 7eff776e86 user: Joel Bennett tags: trunk
14:16
This script can be run with PowerShell to launch a metro application against a file. It is really intended to be used from a batch file, which is embedded as a comment within the script. check-in: 571f1e4a83 user: Bob P tags: trunk
14:16
Depends from PSCpu.ps1 library You can find it at https://github.com/gregzakh/PSCpu/blob/master/Lib/PSCpu.ps1 check-in: a38afd0fb2 user: Marta Leigh tags: trunk
Changes

Added Launch-Metro-App-w-file.ps1.





























































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# encoding: ascii
# api: csharp
# title: Launch Metro App w/ file
# description: This script can be run with PowerShell to launch a metro application against a file. It is really intended to be used from a batch file, which is embedded as a comment within the script.
# version: 1.0
# type: script
# author: Bob P
# license: CC0
# x-poshcode-id: 6479
# x-archived: 2016-08-21T07:19:19
# x-published: 2016-08-19T02:07:00
#
#
<#
Created by: Bob P
Version: 1.0
Date: 08/18/16
 
This script file is used to run a metro app from a command line (e.g. CMD)

Usage: 
PowerShell.exe -ExecutionPolicy Bypass -Command "& '_path_to_this_file'" _execution_string _filename_

For example, if you:
* Save this file as c:\utils\Run4.ps1
* Want to launch Movies and TV
* Want that program to display a file at c:\users\user1\MyMovie.mp4, you would have this command line:
PowerShell.exe -ExecutionPolicy Bypass -Command "& 'c:\utils\Run4.ps1'" Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo c:\users\user1\MyMovie.mp4

Here is a batch file that will launch Movies & TV:
---- Start LaunchMovies.cmd
@echo off
if *%1*==** goto badArg
for %%F in (%1) do set Q='%%~fF'
PowerShell.exe -ExecutionPolicy Bypass -Command "& 'c:\utils\run4.ps1'" Microsoft.ZuneVideo_8wekyb3d8bbwe!Microsoft.ZuneVideo %Q%
goto done

:badArg
echo Usage: %0 File-to-Play
goto done

:done
---- End LaunchMovies.cmd

Thanks to http://poshcode.org/3740 and http://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c/
#>

$code = @'
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace StartMetroApp {
    public enum ActivateOptions
    {
        None = 0x00000000,  // No flags set
        DesignMode = 0x00000001,  // The application is being activated for design mode, and thus will not be able to
        // to create an immersive window. Window creation must be done by design tools which
        // load the necessary components by communicating with a designer-specified service on
        // the site chain established on the activation manager.  The splash screen normally
        // shown when an application is activated will also not appear.  Most activations
        // will not use this flag.
        NoErrorUI = 0x00000002,  // Do not show an error dialog if the app fails to activate.                                
        NoSplashScreen = 0x00000004,  // Do not show the splash screen when activating the app.
    }

    [ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IApplicationActivationManager
    {
        // Activates the specified immersive application for the "Launch" contract, passing the provided arguments
        // string into the application.  Callers can obtain the process Id of the application instance fulfilling this contract.
        IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
        IntPtr ActivateForFile([In] String appUserModelId, [In] [MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] /*IShellItemArray* */ IShellItemArray itemArray, [In] String verb, [Out] out UInt32 processId);
        IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
    }

    [ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]//Application Activation Manager
    public class ApplicationActivationManager : IApplicationActivationManager
    {
        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/]
        public extern IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId);
        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public extern IntPtr ActivateForFile([In] String appUserModelId, [In] [MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)]  /*IShellItemArray* */ IShellItemArray itemArray, [In] String verb, [Out] out UInt32 processId);
        [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
        public extern IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId);
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
    public interface IShellItem
    {
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("b63ea76d-1f85-456f-a19c-48159efa858b")]
    public interface IShellItemArray
    {
    }

    public static class MainProgram
    {
        [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
        private static extern void SHCreateItemFromParsingName(
            [In] [MarshalAs(UnmanagedType.LPWStr)] string pszPath,
            [In] IntPtr pbc,
            [In] [MarshalAs(UnmanagedType.LPStruct)] Guid iIdIShellItem,
            [Out] [MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItem iShellItem);

        [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
        private static extern void SHCreateShellItemArrayFromShellItem(
            [In] [MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] IShellItem psi,
            [In] [MarshalAs(UnmanagedType.LPStruct)] Guid iIdIShellItem,
            [Out] [MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItemArray iShellItemArray);


        public static IShellItemArray GetShellItemArray(string sourceFile)
        {
            IShellItem item = GetShellItem(sourceFile);
            IShellItemArray array = GetShellItemArray2(item);

            return array;
        }

        public static IShellItem GetShellItem(string sourceFile)
        {
            IShellItem iShellItem = null;
            Guid iIdIShellItem = new Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe");
            SHCreateItemFromParsingName(sourceFile, IntPtr.Zero, iIdIShellItem, out iShellItem);

            return iShellItem;
        }

        public static IShellItemArray GetShellItemArray2(IShellItem shellItem)
        {
            IShellItemArray iShellItemArray = null;
            Guid iIdIShellItemArray = new Guid("b63ea76d-1f85-456f-a19c-48159efa858b");
            SHCreateShellItemArrayFromShellItem(shellItem, iIdIShellItemArray, out iShellItemArray);

            return iShellItemArray;
        }

        public static void LaunchMetroProgram(string progID, string filePath) {
            ApplicationActivationManager appActiveManager = new ApplicationActivationManager(); 
            uint pid;
            IShellItemArray array = GetShellItemArray(filePath);
   
            appActiveManager.ActivateForFile(progID, array, "Open", out pid);

        }
    }
}
'@

add-type -TypeDefinition $code

[StartMetroApp.MainProgram]::LaunchMetroProgram($args[0], $args[1]);