From f25d5bec31efc981d4bdec19d0f7c80d3b167e81 Mon Sep 17 00:00:00 2001 From: bojanv Date: Fri, 27 Nov 2015 13:49:28 +0100 Subject: [PATCH] added CSharp version of Microsoft Edge Launcher for all the guys, who wants to check, how they can call UWP app - in this case Microsoft Edge - for different purposes - f.e. testing, batch scripting, etc. Also contains URL Regex validation and short instructions --- MIcrosoftEdgeLauncherCsharp/App.config | 6 + .../MIcrosoftEdgeLauncherCsharp.csproj | 60 +++++++ MIcrosoftEdgeLauncherCsharp/Program.cs | 163 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++ 4 files changed, 265 insertions(+) create mode 100644 MIcrosoftEdgeLauncherCsharp/App.config create mode 100644 MIcrosoftEdgeLauncherCsharp/MIcrosoftEdgeLauncherCsharp.csproj create mode 100644 MIcrosoftEdgeLauncherCsharp/Program.cs create mode 100644 MIcrosoftEdgeLauncherCsharp/Properties/AssemblyInfo.cs diff --git a/MIcrosoftEdgeLauncherCsharp/App.config b/MIcrosoftEdgeLauncherCsharp/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/MIcrosoftEdgeLauncherCsharp/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MIcrosoftEdgeLauncherCsharp/MIcrosoftEdgeLauncherCsharp.csproj b/MIcrosoftEdgeLauncherCsharp/MIcrosoftEdgeLauncherCsharp.csproj new file mode 100644 index 0000000..a165af7 --- /dev/null +++ b/MIcrosoftEdgeLauncherCsharp/MIcrosoftEdgeLauncherCsharp.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {AFFCAEFD-5CAB-4491-8FAD-4C4363B78528} + Exe + Properties + MIcrosoftEdgeLauncherCsharp + MIcrosoftEdgeLauncherCsharp + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MIcrosoftEdgeLauncherCsharp/Program.cs b/MIcrosoftEdgeLauncherCsharp/Program.cs new file mode 100644 index 0000000..0e0a263 --- /dev/null +++ b/MIcrosoftEdgeLauncherCsharp/Program.cs @@ -0,0 +1,163 @@ +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; + +namespace MIcrosoftEdgeLauncherCsharp +{ + /// + /// Class Program, which allows for Edge to be runned out of + /// + class Program + { + /// + /// Edge Application Name + /// + private const string EdgeName = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"; + + /// + /// Defines the entry point of the application. + /// + /// The arguments. + static void Main(string[] args) + { + //call to unmanaged code to activate "modern" app + var launcher = new ApplicationActivationManager(); + Debug.WriteLine("Checking args..."); + uint pid; + switch (args.Length) + { + case 0: + Debug.WriteLine("calling without arguments..."); + launcher.ActivateApplication(EdgeName, null, ActivateOptions.None, out pid); + break; + case 1: + { + var url = args[0]; + Debug.WriteLine("calling with url..."); + Console.WriteLine(url); + //check, if valid URL + var regex = new Regex(@"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$"); + if (regex.IsMatch(url)) + launcher.ActivateApplication(EdgeName, + url, + ActivateOptions.None, + out pid); + else + Console.WriteLine("Enter valid URL. f.e. http://www.bing.com"); + break; + } + default: + Console.WriteLine("Application allows ONLY one parameter. Type (without <>)."); + break; + } + + Console.Read(); + } + + /// + /// Enum ActivateOptions + /// + public enum ActivateOptions + { + /// + /// The none + /// + None = 0x00000000, // No flags set + /// + /// The design mode + /// + 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. + /// + /// The no error UI + /// + NoErrorUI = 0x00000002, // Do not show an error dialog if the app fails to activate. + /// + /// The no splash screen + /// + NoSplashScreen = 0x00000004, // Do not show the splash screen when activating the app. + } + + /// + /// Interface IApplicationActivationManager + /// + [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. + /// + /// Activates the application. + /// + /// The application user model identifier. + /// The arguments. + /// The options. + /// The process identifier. + /// IntPtr. + IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId); + /// + /// Activates for file. + /// + /// The application user model identifier. + /// The item array. + /// The verb. + /// The process identifier. + /// IntPtr. + IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId); + /// + /// Activates for protocol. + /// + /// The application user model identifier. + /// The item array. + /// The process identifier. + /// IntPtr. + IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId); + } + + /// + /// Class ApplicationActivationManager. + /// + /// + /// implementation was made from community members at stackoverflow http://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c + /// + [ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]//Application Activation Manager + class ApplicationActivationManager : IApplicationActivationManager + { + /// + /// Activates the application. + /// + /// The application user model identifier. + /// The arguments. + /// The options. + /// The process identifier. + /// IntPtr. + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/] + public extern IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ActivateOptions options, [Out] out UInt32 processId); + /// + /// Activates for file. + /// + /// The application user model identifier. + /// The item array. + /// The verb. + /// The process identifier. + /// IntPtr. + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] + public extern IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] String verb, [Out] out UInt32 processId); + /// + /// Activates for protocol. + /// + /// The application user model identifier. + /// The item array. + /// The process identifier. + /// IntPtr. + [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] + public extern IntPtr ActivateForProtocol([In] String appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out UInt32 processId); + } + } +} diff --git a/MIcrosoftEdgeLauncherCsharp/Properties/AssemblyInfo.cs b/MIcrosoftEdgeLauncherCsharp/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9dbc8c5 --- /dev/null +++ b/MIcrosoftEdgeLauncherCsharp/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MIcrosoftEdgeLauncherCsharp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MIcrosoftEdgeLauncherCsharp")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("affcaefd-5cab-4491-8fad-4c4363b78528")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]