###前提・実現したいこと
C#でEdgeのwindowのURLを取得したいです。
https://www.ka-net.org/blog/?p=6148
のコードをそっくり使っています。
###発生している問題・エラーメッセージ
rootが空です。
###該当のソースコード
using System; using System.Runtime.InteropServices; using System.Text; using System.Windows.Automation;//UIAutomationClient, UIAutomationTypes 参照 namespace getEdge { partial class GetEdge { string EdgeURL = ""; public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam); [DllImport("user32.Dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] static public extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd); private string GetEdge(string titleText="") { AutomationElement root = AutomationElement.RootElement;//<-このrootが空。 Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"); AutomationElement edge = root.FindFirst(TreeScope.Children, condition); if (edge == null) return EdgeURL=""; edge = edge.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Windows.UI.Core.CoreWindow")); if (edge == null) return EdgeURL=""; Win32Callback childProc = new Win32Callback(EnumWindow); EnumChildWindows((IntPtr)edge.Current.NativeWindowHandle, childProc, (IntPtr)0); //Console.ReadKey(true); return EdgeURL; } private bool EnumWindow(IntPtr handle, IntPtr pointer) { StringBuilder stringbuilder = new StringBuilder(255); IntPtr returnvalue = GetClassName(handle, stringbuilder, stringbuilder.Capacity); if (returnvalue == IntPtr.Zero) return false; string className = stringbuilder.ToString(); if (className == "Windows.UI.Core.CoreComponentInputSource") { IntPtr hTitle = GetParent(handle); AutomationElement title = AutomationElement.FromHandle(hTitle); AutomationElement url = AutomationElement.FromHandle(handle); //Console.WriteLine(title.Current.Name + ", " + url.Current.Name); EdgeURL = url.Current.Name; } return true; }
##ERROR
-
CachedChildren 'root.CachedChildren' は型 'System.InvalidOperationException' の例外をスローしました System.Windows.Automation.AutomationElementCollection {System.InvalidOperationException}
-
CachedParent 'root.CachedParent' は型 'System.InvalidOperationException' の例外をスローしました System.Windows.Automation.AutomationElement {System.InvalidOperationException}
のようになっていて、Childrenをとれません。
取得方法をアドバイスお願いします。
##補足情報(言語/FW/ツール等のバージョンなど)
Windows10(32bit), Visual Studio 2015 Community
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。