win32获取浏览器当前Tab的URL
代码都是win10上测试的
1. 获取IE的URL,Enum IE窗口的子句柄,找到className为Edit的句柄,然后用SendMessage WM_GETTEXT消息来获取:
上代码:
1 #include <iostream> 2 #include <string> 3 #include <windows.h> 4 5 6 BOOL CALLBACK BrowserIEChildEnumProc(HWND hwndChild, LPARAM lParam) 7 { 8 char className[256] = { 0 }; 9 GetClassName(hwndChild, className, 256); 10 char url[4028] = { 0 }; 11 12 std::string class_name(className); 13 if (class_name.compare("Edit") == 0) 14 { 15 16 SendMessage(hwndChild, WM_GETTEXT, sizeof(url), (LPARAM)url); // 必须用SendMessage, 不然用GetWindowText会得不到,因为是远程进程 17 std::cout << "IE Level Edit class : URL" << url << std::endl; 18 } 19 else if (class_name.compare("ComboBoxEx32") == 0) 20 { 21 SendMessage(hwndChild, WM_GETTEXT, sizeof(url), (LPARAM)url); 22 std::cout << "ComboBoxEx32 class : URL" << url << std::endl; 23 24 } 25 else if (class_name.compare("TabWindowClass") == 0) 26 { 27 SendMessage(hwndChild, WM_GETTEXT, sizeof(url), (LPARAM)url); 28 std::cout << "TabWindowClass class : URL" << url << std::endl; 29 } 30 else 31 { 32 33 } 34 35 36 return TRUE; 37 } 38 39 40 BOOL CALLBACK BrowserEnumProc(HWND hwndChild, LPARAM lParam) 41 { 42 char className[256] = { 0 }; 43 44 GetClassName(hwndChild, className, 256); 45 46 std::string class_name(className); 47 //std::cout << "Window Class is:" << class_name << std::endl; 48 if (class_name.compare("IEFrame") == 0) 49 { 50 EnumChildWindows(hwndChild, BrowserIEChildEnumProc, NULL); 51 } 52 else if (class_name.compare("Edit") == 0) 53 { 54 char url[4028] = { 0 }; 55 SendMessage(hwndChild, WM_GETTEXT, sizeof(url), (LPARAM)url); 56 std::cout << "Top Level Edit class : URL" << url << std::endl; 57 } 58 //else if () 59 //{ 60 61 //} 62 63 64 return TRUE; 65 } 66 67 68 int main() 69 { 70 EnumChildWindows(NULL, BrowserEnumProc, NULL); 71 72 73 int d; 74 std::cin >> d; 75 return 0; 76 }
通过windows的动态数据交换(DDE)技术来获取FireFox浏览器的URL(不支持Opera 和Chrome):
1 #include <iostream> 2 #include <string> 3 #include <windows.h> 4 5 #include <ddeml.h> 6 7 8 9 10 HDDEDATA CALLBACK 11 DdeCallback(UINT uType, UINT uFmt, HCONV hconv, 12 HSZ hsz1, HSZ hsz2, HDDEDATA hdata, 13 DWORD dwData1, DWORD dwData2) 14 { 15 return ((HDDEDATA)NULL); 16 } 17 18 19 //请求数据 20 void DDERequest(DWORD idInst, HCONV hConv, char* szItem, char* sDesc) 21 { 22 HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0); 23 24 //给szItem和sDesc赋值 25 HDDEDATA hData = DdeClientTransaction(NULL, 0, hConv, hszItem, CF_TEXT, 26 XTYP_REQUEST, 5000, NULL); //5秒超时 27 28 if (hData == NULL) 29 { 30 //请求数据失败 31 printf("Request failed: %s\n", szItem); 32 } 33 else 34 { 35 //成功获取到数据 36 char szResult[1028]; 37 DdeGetData(hData, (unsigned char *)szResult, 1028, 0); 38 39 printf("Request Success: %s\n", szResult); 40 } 41 } 42 43 static void fetchFireFoxURL(){ 44 45 DWORD idDde = 0; 46 HCONV HConversation; 47 HSZ Server = NULL; 48 HSZ Topic = NULL; 49 50 DdeInitialize(&idDde, (PFNCALLBACK)DdeCallback, APPCMD_CLIENTONLY, 0); 51 52 Server = DdeCreateStringHandle(idDde, "FireFox", CP_WINANSI); 53 Topic = DdeCreateStringHandle(idDde, "WWW_GetWindowInfo", CP_WINANSI); 54 55 56 HConversation = DdeConnect(idDde, Server, Topic, NULL); 57 58 59 if (HConversation == 0) 60 { 61 DdeUninitialize(idDde); 62 } 63 64 char recvData[2048] = { 0 }; 65 //请求数据 66 DDERequest(idDde, HConversation, "\\Doc", recvData); 67 68 //断开连接,释放资源 69 DdeDisconnect(HConversation); 70 DdeUninitialize(idDde); 71 72 73 74 } 75 76 int main() 77 { 78 79 80 fetchFireFoxURL(); 81 82 int d; 83 std::cin >> d; 84 return 0; 85 }
references:
https://github.com/heenbo/my_test_c/blob/140f546b72ca7027fbd135978f236f7946ad2822/15_emacs/emacs-25.1/nt/ddeclient.c
https://msdn.microsoft.com/en-us/library/windows/desktop/ms648774(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms648775(v=vs.85).aspx
https://autohotkey.com/board/topic/17633-retrieve-addressbar-of-firefox-through-dde-message/page-2
http://bbs.csdn.net/topics/360011960
http://www.delphitop.com/html/xitong/1763.html
https://answers.microsoft.com/en-us/windows/forum/windows_8-networking/get-safari-browser-url-in-windows-application/b793cdb8-415e-4d4a-94ab-c35e6ea08dd2
http://stackoverflow.com/questions/3579649/get-url-from-browser-to-c-sharp-application
http://stackoverflow.com/questions/5317642/retrieve-current-url-from-c-sharp-windows-forms-application/33191637#33191637