Get the address bar url from a browser

I would like to know how can I get the url from the address bar in a browser, I would like to do it preferably in the most popular browsers like firefox, chrome, edge, opera etc.

The code from this snswer from stack overflow answer https://stackoverflow.com/a/19296923/11337328 does what I want but only works for Internet Explorer, I don't understand the code, how it is getting that info though.

If someone knows how can I do that for other browsers please help. Thank you.
It works by finding the handle of the IE window that contains the required text, and then obtaining this text.

For this type of program to work with other browsers, then you need to find out the names of the appropriate classes (not C++ classes!) and re-code as appropriate.

To find these names, use Spy++
https://docs.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2019
Thanks for your answer.

I went and used Spy++ but I can't seem to figure where to find thos classes and how I would re-code.
Did you use it on IE to see the names referenced in the code? If you didn't then you're not using spy++ right.
Yeah I was not using it right. However when I did, it returned a lot of stuff and the name referenced in the code I found was "WM_GETTEX" which I don't think is very minimal.

I am guessing I am still not using it the right way or I am unable to to inspect the data it returns to be able to do the re-code.

There are a lot of option in the messages options, I don't really know how to use and what exactly they do.
By changing the keyword used in google search I was able to find some working code( googling is indeed an art :-) )

https://forums.codeguru.com/showthread.php?547597-RESOLVED-Get-active-url-in-Google-Chrome-from-v-29-with-IAccessible

https://stackoverflow.com/a/26062192/11337328

The code that works that I am trying to understand how it works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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;
  if ((AccessibleObjectFromEvent(hwnd, idObject, idChild, &pAcc, &varChild) == S_OK) && (pAcc != NULL)) {
    char className[50];
    if (GetClassName(hwnd, className, 50) && strcmp(className, "Chrome_WidgetWin_1") == 0) {
      BSTR bstrName = nullptr;
      if (pAcc->get_accName(varChild, &bstrName) == S_OK) {
        if (wcscmp(bstrName, L"Address and search bar") == 0) {
          BSTR bstrValue = nullptr;
          if (pAcc->get_accValue(varChild, &bstrValue) == S_OK) {
            printf("URL change: %ls\n", bstrValue);
            SysFreeString(bstrValue);
          }
        }
        SysFreeString(bstrName);
      }
      pAcc->Release();
    }
  }
}

void Hook() {
    if (LHook != 0) return;
    CoInitialize(NULL);
    LHook = SetWinEventHook(EVENT_OBJECT_FOCUS, EVENT_OBJECT_VALUECHANGE, 0, WinEventProc, 0, 0, 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;
}
Topic archived. No new replies allowed.