Trao đổi với tôi

http://www.buidao.com

10/12/10

[VC++] Capture WebBrowser URL (IE, Firefox, Opera, ...)

reflink: http://forums.congdongcviet.com/showthread.php?t=35500
Mô tả: Đoạn code lấy đường dẫn hiện hành (URL) của trình duyệt web
Ghi chú: Nếu có nhiều trình duyệt sử dụng nhiều thẻ (tab), thì nó lấy URL của thẻ đang được kích hoạt (active tab)

/**********************************************************

Description: Capture WebBrowser URL
Author: gianghoplus [gianghoplus@gmail.com]
Date: 23/08/2010
Copyright © 2010 www.congdongcviet.com

***********************************************************/


#include
#include
#include

BOOL GetBrowserURL(IN LPCSTR szName, OUT LPSTR szURL)
{
DWORD dwInst = 0, Long;
DdeInitializeA (&dwInst, NULL, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0ul);

HSZ hszService = DdeCreateStringHandleA (dwInst, szName, CP_WINANSI);
HSZ hszTopic = DdeCreateStringHandleA (dwInst, "WWW_GetWindowInfo", CP_WINANSI);
HSZ hszItem = DdeCreateStringHandleA (dwInst, "-1", CP_WINANSI);
HCONV hConv = DdeConnect (dwInst, hszService, hszTopic, NULL);

DdeFreeStringHandle (dwInst, hszService);
DdeFreeStringHandle (dwInst, hszTopic);

if (!hConv) return FALSE;

HDDEDATA hRetVal = DdeClientTransaction (NULL, 0, hConv, hszItem, CF_TEXT, XTYP_REQUEST, 10000L, NULL);
DdeFreeStringHandle(dwInst,hszItem);

if (hRetVal > 0)
sprintf(szURL,(LPSTR)DdeAccessData(hRetVal,(LPDWORD)&Long));

DdeUnaccessData(hRetVal);
DdeFreeDataHandle(hRetVal);
DdeDisconnect(hConv);
return TRUE;
}

void main()
{
CHAR szName[255] = "firefox"; // Ex: "iexplore", "opera", ...
while (true)
{
CHAR szURL[255] = {0};
BOOL bRet = GetBrowserURL(szName, szURL);

if (bRet) printf("%s\n", szURL);
else printf("Error: Cannot connect to %s.\n", szName);

Sleep(1000);
}
}
__________________