从Windows服务中的Microsoft Edge获取URL [英] Get URL from Microsoft Edge in Windows Service

查看:1227
本文介绍了从Windows服务中的Microsoft Edge获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用Windows服务,需要从活动的Microsoft Edge实例获取URL,但我无法在Windows窗体中实现它应用程序。我在这里观察到的主要内容是Microsoft Edge进程是作为"MICROSOFTEDGE"启动的。
只是容纳"MICROSOFTEDGECP"所有实例的容器。其中 实际上包含与标签相关的信息。我使用

Microsoft Edge:获取窗口URL和标题
以获取Windows窗体应用程序中活动选项卡的URL,但此解决方案无法在Windows服务(在其中运行)中获取URL本地系统)。


我正在使用Visual Studio 2017,.Net Framework 3.5和Windows 10专业版。任何有关这方面的帮助都将受到高度赞赏。


谢谢和问候 


解决方案

Hi Wall.E,


根据您的描述,您希望获得Microsoft Edge的窗口URL和标题。我做了一个你可以参考的样本:

< DllImport(" user32.dll")> 
私有函数GetForegroundWindow()As IntPtr
结束函数

私函数TryGetMSEdgeUrlAndTitle(edgeWindow as IntPtr,ByRef url As String,ByRef title as String)As Boolean
Const UIA_NamePropertyId As Integer = 30005
Const UIA_ClassNamePropertyId As Integer = 30012
Const UIA_NativeWindowHandlePropertyId As Integer = 30020

url =""
title =""

Dim uiA As IUIAutomation = New CUIAutomation()
Dim rootElement As IUIAutomationElement = uiA.GetRootElement()

Dim cacheRequest As IUIAutomationCacheRequest = uiA.CreateCacheRequest()
cacheRequest.AddProperty(UIA_NamePropertyId)

Dim windowCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_NativeWindowHandlePropertyId,GetForegroundWindow())
Dim windowElement As IUIAutomationElement = rootElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants,windowCondition ,cacheRequest)
如果windowElement为Nothing则
返回False
结束如果

Dim edgeCondition作为IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_NamePropertyId,"Microsoft Edge")
Dim edgeElement As IUIAutomationElement = windowElement.FindFirstBuildCache(TreeScope.TreeScope_Subtree,edgeCondition,cacheRequest)
if edg eElement是Nothing然后
返回False
结束如果

Dim tabCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId," TabWindowClass")
Dim tabElement As IUIAutomationElement = edgeElement .FindFirstBuildCache(TreeScope.TreeScope_Descendants,tabCondition,cacheRequest)
如果tabElement是Nothing那么
返回False
结束如果

Dim ieCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId ,"Internet Explorer_Server")
Dim ieElement As IUIAutomationElement = tabElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants,ieCondition,cacheRequest)
如果ieElement是Nothing那么
返回False
结束如果

url = ieElement.CachedName
title = tabElement.CachedName

返回True
结束函数

Sub Main()

Dim oldUrl As String =""
Dim oldTitle As String =""

True True
Dim url As String =""
Dim title As String =""

如果TryGetMSEdgeUrlAndTitle(GetForegroundWindow(),url,title)那么
If(url<> oldUrl)OrElse(title<> oldTitle)那么
Console.WriteLine ([String] .Format(" Page title:{0}"& vbCr& vbLf&" URL:{1}",title,url))

oldUrl = url
oldTitle = title
End if
End if

Thread.Sleep(250)
End

End Sub

最好的问候,


Cherry



Hi,

I'm working on Windows Service and need to get URL from active Microsoft Edge instance but i'm unable to get it despite achieving it in Windows Form application. The main thing which i observe here is Microsoft Edge process is launched as "MICROSOFTEDGE" which is just a container to hold all the instances of "MICROSOFTEDGECP" which actually holds the information related to tabs. I use Microsoft Edge: Get Window URL and Title to get the URL of active tab in Windows Form application but this solution fails to get URL in Windows Service (Which is running under Local System).

I'm using Visual Studio 2017, .Net Framework 3.5 and Windows 10 Pro Edition. Any help in this regard will be highly appreciated.

Thanks and Regards 

解决方案

Hi Wall.E,

According to your description, you want to get Window URL and title for Microsoft Edge. I do one sample that you can refer to:

<DllImport("user32.dll")>
    Private Function GetForegroundWindow() As IntPtr
    End Function

    Private Function TryGetMSEdgeUrlAndTitle(edgeWindow As IntPtr, ByRef url As String, ByRef title As String) As Boolean
        Const UIA_NamePropertyId As Integer = 30005
        Const UIA_ClassNamePropertyId As Integer = 30012
        Const UIA_NativeWindowHandlePropertyId As Integer = 30020

        url = ""
        title = ""

        Dim uiA As IUIAutomation = New CUIAutomation()
        Dim rootElement As IUIAutomationElement = uiA.GetRootElement()

        Dim cacheRequest As IUIAutomationCacheRequest = uiA.CreateCacheRequest()
        cacheRequest.AddProperty(UIA_NamePropertyId)

        Dim windowCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_NativeWindowHandlePropertyId, GetForegroundWindow())
        Dim windowElement As IUIAutomationElement = rootElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, windowCondition, cacheRequest)
        If windowElement Is Nothing Then
            Return False
        End If

        Dim edgeCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_NamePropertyId, "Microsoft Edge")
        Dim edgeElement As IUIAutomationElement = windowElement.FindFirstBuildCache(TreeScope.TreeScope_Subtree, edgeCondition, cacheRequest)
        If edgeElement Is Nothing Then
            Return False
        End If

        Dim tabCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "TabWindowClass")
        Dim tabElement As IUIAutomationElement = edgeElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, tabCondition, cacheRequest)
        If tabElement Is Nothing Then
            Return False
        End If

        Dim ieCondition As IUIAutomationCondition = uiA.CreatePropertyCondition(UIA_ClassNamePropertyId, "Internet Explorer_Server")
        Dim ieElement As IUIAutomationElement = tabElement.FindFirstBuildCache(TreeScope.TreeScope_Descendants, ieCondition, cacheRequest)
        If ieElement Is Nothing Then
            Return False
        End If

        url = ieElement.CachedName
        title = tabElement.CachedName

        Return True
    End Function

    Sub Main()

        Dim oldUrl As String = ""
        Dim oldTitle As String = ""

        While True
            Dim url As String = ""
            Dim title As String = ""

            If TryGetMSEdgeUrlAndTitle(GetForegroundWindow(), url, title) Then
                If (url <> oldUrl) OrElse (title <> oldTitle) Then
                    Console.WriteLine([String].Format("Page title: {0} " & vbCr & vbLf & "URL: {1}", title, url))

                    oldUrl = url
                    oldTitle = title
                End If
            End If

            Thread.Sleep(250)
        End While

          End Sub

Best Regards,

Cherry


这篇关于从Windows服务中的Microsoft Edge获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆