質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

1回答

4398閲覧

C#でEdgeのwindowのURLを取得したいです。

k-hi

総合スコア21

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/10/01 08:18

###前提・実現したいこと
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

この処理はコンソールアプリケーションもしくは別スレッドから実行されていますか?
であればWinForm等のUIのあるプロジェクトを作成してUIスレッドから実行してみてください。

投稿2017/02/15 12:44

編集2017/02/15 12:45
hmmm

総合スコア818

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問