none
Get URL from Microsoft Edge in Windows Service RRS feed

  • Question

  • 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 

    Wednesday, September 6, 2017 1:58 PM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 7, 2017 7:45 AM
    Moderator
  • Starting with Vista, Microsoft Windows Services run in Session 0 isolation while interactive user processes run in Session 1 or another session. This was done in order to prevent malware, installed as a Windows Service, from taking over or disabling the OS. Attempting to work around this security enhancement would be unsupported and could easily be disabled by Microsoft in a patch or future version of Windows.

    I would recommend using the Windows Form app, either hidden or displayed as an icon in the tray, and communicate with the Windows Service via IPC (e.g. WCF, named pipes, etc.) instead of attempting to interact directly with the desktop from the Windows Service.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, September 7, 2017 2:55 PM
  • Thanks for the reply Cherry Bu. This code works fine in Windows Form application but a major hurdle is i'm trying to achieve this in Windows Services which runs in different session then interactive user session so this code fials to retrieve URL in Windows Service.

    Thanks and Regards

    Friday, September 8, 2017 6:28 AM
  • Thanks for the reply Paul. I'm bound to use the existing infrastrucutre thats why i'm using the existing Windows Service for modifications. Is their any other way or work around to get the URL from Edge in Windows Service???

    Thanks and Regards

     
    Friday, September 8, 2017 6:36 AM
  • This is probably going to be complicated regardless of how you do it, but I would take a look at the below link (code will need to be converted to VB):

    https://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-32-and-64-bit-Archite

    Beyond that, I can't suggest anything short of disabling security features in the OS, which of course I would not do.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, September 8, 2017 1:28 PM