CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2014
    Posts
    61

    [RESOLVED] Get active url in Google Chrome from v.29 with IAccessible

    Based in some examples of source codes as this => http://stackoverflow.com/questions/2...urrent-version, I'm trying get active url on address bar from Google Chrome with IAccessible, but it always return NULL (0). Could someone help me please?

    Any suggestion will be welcome.

    Here is my last attempt:

    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include <Oleacc.h>
    #pragma comment( lib,"Oleacc.lib")
    
    HWINEVENTHOOK LHook = 0;
    
    void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) {
    
        IAccessible* pAcc = NULL;
        VARIANT varChild;
        HRESULT hr = AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild);
    
        if ((hr == S_OK) && (pAcc != NULL)) {
            BSTR bstrValue;
            pAcc->get_accValue(varChild, &bstrValue);
    
            char className[500];
            GetClassName(hwnd, (LPWSTR)className, 500);
    
            if (event == EVENT_OBJECT_VALUECHANGE){
    
                /*
    
               Window classe name of each browser =>
    
                Safari => SafariTaskbarTabWindow
                Chrome => Chrome_WidgetWin_1
                IE => IEFrame
                Firefox => MozillaWindowClass
                Opera => OperaWindowClass
    
                */
    
                if (strcmp(className, "Chrome_WidgetWin_1") != 0) {
                    printf("Active URL: %ls\n", bstrValue);
                }
            }
            SysFreeString(bstrValue);
            pAcc->Release();
        }
    }
    
    void Hook() {
    
        if (LHook != 0) return;
        CoInitialize(NULL);
        LHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_VALUECHANGE, 0, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
    }
    
    void Unhook() {
    
        if (LHook == 0) return;
        UnhookWinEvent(LHook);
        CoUninitialize();
    }
    
    
    int main(int argc, const char* argv[]) {
    
        MSG msg;
        Hook();
    
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        Unhook();
    
        return 0;
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Get active url in Google Chrome from v.29 with IAccessible

    I would use the Active Accessibility Tools that used to come with the Active Accessibility 2.0 SDK. Specifically use the AccExplorer32.exe tool to identify the aa elements - then you can compare with what your code does and what the tool discovers. Due to security considerations, this may not work within a hook.

    At any rate, the AA 2.0 SDK doesn't seem to be available through Microsoft - you'll need to get it from the Win7 SDK: http://www.microsoft.com/en-us/downl...s.aspx?id=8279

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get active url in Google Chrome from v.29 with IAccessible

    The approach works just fine, see the attached sample. However, there are lots of (null) lines produced when you refuse to filter by "Address and search bar".

    Code:
    J:\Temp\58>58.exe all
    Catching all events...
    Press any key to leave.
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    Leaving...
    With the filtering you get a more concise output:
    Code:
    J:\Temp\58>58.exe
    
    Press any key to leave.
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    Leaving...
    Attached Files Attached Files
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 2014
    Posts
    61

    Re: Get active url in Google Chrome from v.29 with IAccessible

    Quote Originally Posted by Igor Vartanov View Post
    The approach works just fine, see the attached sample. However, there are lots of (null) lines produced when you refuse to filter by "Address and search bar".

    Code:
    J:\Temp\58>58.exe all
    Catching all events...
    Press any key to leave.
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    Leaving...
    With the filtering you get a more concise output:
    Code:
    J:\Temp\58>58.exe
    
    Press any key to leave.
    URL change: (null)
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    URL change: (null)
    URL change: (null)
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?47-C-and-WinAPI
    URL change: forums.codeguru.com/forumdisplay.php?7-Visual-C-Programming
    Leaving...
    Dear Igor,

    Thank you very much for help me! I'm using VC++ 2013 and was necessary change kbhit() for _kbhit(), and then I compiled your code in attachment with success. But when execute, showed "Press any key to leave." and when pressed any key, the program finished. Then, how do fix?

  5. #5
    Join Date
    Apr 2014
    Posts
    61

    Re: Get active url in Google Chrome from v.29 with IAccessible

    Here, this stretch:

    Code:
    printf("Catching all events...\n");
    printAllEvents = TRUE;
    never is executed . Why?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Get active url in Google Chrome from v.29 with IAccessible

    Quote Originally Posted by FL4SHC0D3R View Post
    Here, this stretch:

    Code:
    printf("Catching all events...\n");
    printAllEvents = TRUE;
    never is executed . Why?
    Perhaps this condition
    Code:
    int _tmain(int argc, const TCHAR* argv[]) 
    {
    
    	if (argc > 1 && _tcsicmp(argv[1], TEXT("all")) == 0) 
    	{
    		printf("Catching all events...\n");
    		printAllEvents = TRUE;
    	}
    is false?
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get active url in Google Chrome from v.29 with IAccessible

    Quote Originally Posted by FL4SHC0D3R View Post
    But when execute, showed "Press any key to leave." and when pressed any key, the program finished. Then, how do fix?
    Well, the program is expected to work in background until you press any key, this is what "Press any key to leave" informs you about.

    If you want to see any events from Chrome, you need to interact with your Chrome to invoke some. In my tests I just opened new tab and navigated to CG forum. This is what you can see in my logs.
    Best regards,
    Igor

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: [RESOLVED] Get active url in Google Chrome from v.29 with IAccessible

    Quote Originally Posted by FL4SHC0D3R View Post
    I'm trying get active url on address bar from Google Chrome with IAccessible
    Maybe this is not what you think it is. The approach gives you an ability to subscribe to events from Chrome, but not to directly get the URL of already opened web page.
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2014
    Posts
    61

    Re: [RESOLVED] Get active url in Google Chrome from v.29 with IAccessible

    Quote Originally Posted by Igor Vartanov View Post
    Maybe this is not what you think it is. The approach gives you an ability to subscribe to events from Chrome, but not to directly get the URL of already opened web page.
    Igor,

    I already solved the problem . Thank you very much one more time for all your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured