Windows 10

Microsoft Edgeを起動するC#コード

以前書いた記事「「ファイル名を指定して実行」からMicrosoft Edgeを起動する」を見ると分かる通り、Microsoft Edgeは実体となるexeファイルを実行する、通常の方法では起動できません。

Edgeのようなユニバーサル Windows プラットフォーム (UWP) アプリを起動するにはどうすれば良いか調べたところ「Windows 8 アプリのテストを自動化する」にその方法が載っていました。

アプリのアクティベーションは、IApplicationActivationManager インターフェイスを使うことで自動化できます。この API は、Visual Studio 2012 と同時に既定でインストールされる Windows SDK に含まれています。アプリの起動には IApplicationActivationManager::ActivateApplication (英語) メソッドを使います。

IApplicationActivationManager::ActivateApplicationメソッドを使えば良いみたいですね。

さっそくこのメソッドの呼び出し方について検索したところ、すぐに下記ページがヒットしました。

・com – IApplicationActivationManager::ActivateApplication in C#? – Stack Overflow
http://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c

なるほど。VBAのWin32 API呼び出しみたいに、基本はヘッダーファイルを見て自分で書かないといけないわけですね・・・。

でも面倒くさいので、すでに回答者さんが書いているコードをほぼそのまま使うことにしました。

/*
  for reference:
    https://msdn.microsoft.com/en-us/library/hh706902.aspx
    http://stackoverflow.com/questions/12925748/iapplicationactivationmanageractivateapplication-in-c
    http://blogs.msdn.com/b/windowsappdev_ja/archive/2012/09/11/windows-8-automating-the-testing.aspx
*/
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace ExecMSEdge
{
  class Program
  {
    private enum ACTIVATEOPTIONS : uint
    {
      AO_NONE = 0x00000000,
      AO_DESIGNMODE = 0x00000001,
      AO_NOERRORUI = 0x00000002,
      AO_NOSPLASHSCREEN = 0x00000004
    }
    
    [ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IApplicationActivationManager
    {
      IntPtr ActivateApplication([In] String appUserModelId, [In] String arguments, [In] ACTIVATEOPTIONS options, [Out] out UInt32 processId);
      IntPtr ActivateForFile([In] String appUserModelId, [In] IntPtr /*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")]
    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] IntPtr /*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);
    }
    
    public static void Main(string[] args)
    {
      const string edgeID = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge";
      
      uint pid;
      string url;
      
      if (args.Length > 0) {
        url = args[0];
      } else {
        url = "//www.ka-net.org/blog/";
      }
      ApplicationActivationManager aam = new ApplicationActivationManager();
      aam.ActivateApplication(edgeID, url, ACTIVATEOPTIONS.AO_NONE, out pid);
      Console.Write("ProcessId:" + pid);
    }
  }
}

ExecMicrosoft Edge_01

引数で指定しているEdgeのAppUserModelID(Application User Model ID)は“Windows 8 アプリのテストを自動化する”にある通り、Get-AppxPackage コマンドレットで取得することができます。

しかしながら、メモ帳とかペイントであればProcess.Startで一発なものを、UWPアプリの場合はこんなに面倒くさいとは・・・。
microsoft-edgeスキームを使って起動した方がよっぽど簡単そうです。

「microsoft-edge:」って何?前のページ

Selenium(C#)でEdgeをいろいろ操作してみた。次のページ

関連記事

  1. Windows関連

    Windows 8 Consumer PreviewにMicrosoft Security Esse…

    ※ 下記はWindows 8 Consumer Preview(日本語…

  2. Windows 10

    Windows Insider Meetup in Japan 2019 東京に参加しました。

    Windows Insider向けのミートアップイベント「Window…

  3. Windows関連

    Windows 10 IMEの「クラウド候補機能」の仕組みを追ってみた。

    「ついにWindows 10 日本語IMEにクラウド候補機能が搭載され…

  4. Office関連

    Windows 10 Technical PreviewにOffice XPをインストールしてみまし…

    「最新ビルドを詳細レビュー! Windows 10 Technical…

  5. Windows 10

    Chromium版のMicrosoft Edge Insiderを使ってみました。

    下記記事にある通り、Chromium版のMicrosoft Edgeが…

  6. Windows関連

    ダウンロードフォルダーのパスを取得するVBScript

    ダウンロードフォルダーのパスを取得する必要があったので、過去に書いた記…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP