博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows资源管理器及ie监听
阅读量:2431 次
发布时间:2019-05-10

本文共 5278 字,大约阅读时间需要 17 分钟。

Okay, it’s been a while since we set aside our Little Program to learn a bit about connection points and using dispatch interfaces as connection point interfaces. Now we can put that knowledge to use.

Internet Explorer and Explorer windows fire a group of events known as DWeb­Browser­Events, so we just need to listen on those events to follow the window as it navigates around.

Take our scratch program and make these changes:

#define UNICODE#define _UNICODE#define STRICT#define STRICT_TYPED_ITEMIDS#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
...// DispInterfaceBase incorporated by referencevoid UpdateText(HWND hwnd, PCWSTR pszText);class CWebBrowserEventsSink : public CDispInterfaceBase
public: CWebBrowserEventsSink(HWND hwnd) : m_hwnd(hwnd) { } IFACEMETHODIMP SimpleInvoke( DISPID dispid, DISPPARAMS *pdispparams, VARIANT *pvarResult) { switch (dispid) { case DISPID_NAVIGATECOMPLETE: UpdateText(m_hwnd, pdispparams->rgvarg[0].bstrVal); break; case DISPID_QUIT: UpdateText(m_hwnd, L"
"); Disconnect(); break; } return S_OK; };private: HWND m_hwnd;};

Our event sink class listens for DISPID_NAVIGATE­COMPLETE and DISPID_QUIT and updates the text with the new navigation location or the string L”” if the window exited. In the exit case, we also disconnect from the connection point to break the circular reference.

The IDL file for Navigate­Complete says

[id(DISPID_NAVIGATECOMPLETE), helpstring(“…”)]

void NavigateComplete([in] BSTR URL );
Therefore, we know that the URL parameter arrives as a VT_BSTR in position zero, so we can access it as pdispparams->rgvarg[0].bstrVal.

That class is basically the guts of the program. The rest is scaffolding. Like hooking up this guy to a listview item so it can report its findings somewhere.

struct ItemInfo{ ItemInfo(HWND hwnd, IDispatch *pdisp)  : hwnd(hwnd) {  spSink.Attach(new(std::nothrow) CWebBrowsrEventsSink(hwnd));  if (spSink) spSink->Connect(pdisp); } ~ItemInfo() { if (spSink) spSink->Disconnect(); } HWND hwnd; CComPtr
spSink;};ItemInfo *GetItemByIndex(int iItem){ LVITEM item; item.mask = LVIF_PARAM; item.iItem = iItem; item.iSubItem = 0; item.lParam = 0; ListView_GetItem(g_hwndChild, &item); return reinterpret_cast
(item.lParam);}ItemInfo *GetItemByWindow(HWND hwnd, int *piItem){ int iItem = ListView_GetItemCount(g_hwndChild); while (--iItem >= 0) { ItemInfo *pii = GetItemByIndex(iItem); if (pii->hwnd == hwnd) { if (piItem) *piItem = iItem; return pii; } } return nullptr;}void UpdateText(HWND hwnd, PCWSTR pszText){ int iItem; if (GetItemByWindow(hwnd, &iItem)) { ListView_SetItemText(g_hwndChild, iItem, 0, const_cast
(pszText)); }}

Attached to each listview item is an Item­Info structure which remembers the browser window it is associated with and the event sink that is listening for events.

// GetLocationFromView, GetLocationFromBrowser, and GetBrowserInfo// incorporated by referenceCComPtr
g_spWindows;// rename DumpWindows to BuildWindowListHRESULT BuildWindowList(){ CComPtr
spunkEnum; HRESULT hr = g_spWindows->_NewEnum(&spunkEnum); if (FAILED(hr)) return hr; CComQIPtr
spev(spunkEnum); for (CComVariant svar; spev->Next(1, &svar, nullptr) == S_OK; svar.Clear()) { if (svar.vt != VT_DISPATCH) continue; HWND hwnd; CComHeapPtr
spszLocation; if (FAILED(GetBrowserInfo(svar.pdispVal, &hwnd, &spszLocation))) continue; ItemInfo *pii = new(std::nothrow) ItemInfo(hwnd, svar.pdispVal); if (!pii) continue; LVITEM item; item.mask = LVIF_TEXT | LVIF_PARAM; item.iItem = MAXLONG; item.iSubItem = 0; item.pszText = spszLocation; item.lParam = reinterpret_cast
(pii); int iItem = ListView_InsertItem(g_hwndChild, &item); if (iItem < 0) delete pii; } return S_OK;}

To build the window list, we enumerate the contents of the IShell­Windows. For each window, we get its window handle and current location and create a listview item for it. The reference data for the listview item is the Item­Info.

BOOLOnCreate(HWND hwnd, LPCREATESTRUCT lpcs){ g_hwndChild = CreateWindow(WC_LISTVIEW, 0,    LVS_LIST | WS_CHILD | WS_VISIBLE |    WS_HSCROLL | WS_VSCROLL, 0, 0, 0, 0,    hwnd, (HMENU)1, g_hinst, 0); g_spWindows.CoCreateInstance(CLSID_ShellWindows); BuildWindowList(); return TRUE;}

Our creation function creates a child listview and fills it with stuff.

And of course we clean up our objects when the items are deleted and when the window is destroyed.

LRESULT OnNotify(HWND hwnd, int idFrom, NMHDR *pnm){ switch (idFrom) { case 1:  switch (pnm->code) {  case LVN_DELETEITEM:   {    auto pnmlv = CONTAINING_RECORD(pnm, NMLISTVIEW, hdr);    delete reinterpret_cast
(pnmlv->lParam); } break; } } return 0;}void OnDestroy(HWND hwnd){ g_spWindows.Release(); PostQuitMessage(0);} HANDLE_MSG(hwnd, WM_NOTIFY, OnNotify);

And there we have it, a program that displays all the Internet Explorer and Explorer windows and updates their locations as you navigate.

Note, however, that our program doesn’t notice when new windows are created. We’ll hook that up next time.

原文:

转载地址:http://chvmb.baihongyu.com/

你可能感兴趣的文章
简化Windows 2003域控制器密码(转)
查看>>
GSM无线网络的虚拟分层(转)
查看>>
不用重装 轻松解决Windows系统棘手问题(转)
查看>>
对移动通信网络优化工作的一些见解(转)
查看>>
正确网络配置建议 减少卡机死机的关键(转)
查看>>
智能手机Smartphone开发从零起步(五)(转)
查看>>
防止木马,教你找回被盗的QQ号码(转)
查看>>
SEO技巧中你可能没有注意的细节(转)
查看>>
微软开始二代Windows Live 不见Cloud OS踪影
查看>>
SQL Server 7.0 入门(四)(转)
查看>>
心得分享:防火墙程序使用的几点经验(转)
查看>>
创建ISAPI扩展(转)
查看>>
ORACLE 9I 重建 TEMP 表空间(转)
查看>>
Windows Server 2003 R2 Beta 2将公测(转)
查看>>
Pocket PC应用程序中使用SQL Server CE(转)
查看>>
完美解决不修改NTLDR-最后一次正确配置的问题(修正)(转)
查看>>
在双硬盘上安装独立32位和64位双系统(转)
查看>>
MYSQL列类型参考(转)
查看>>
病毒及木马预警一周播报(06.04.17~04.23)(转)
查看>>
黑客口述:我的第一台3389肉鸡的经历(转)
查看>>