summaryrefslogtreecommitdiff
path: root/win32/common/atlwinhk.h
blob: 4dddf824f495e1da5dce8683f30aa13ccffc4aa4 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * AUTHOR
 * N. Nielsen
 *
 * VERSION
 * 2.2.0b
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#ifndef __ATLWINHK_H__
#define __ATLWINHK_H__

/////////////////////////////////////////////////////////////////////////
// Hooks an CWindowImpl to windows that the API or other code creates
// By Stef: 06/10/99


// Use this macro in the class definition and pass class name
// as first param
#define DECLARE_HOOK_AND_SUBCLASS(Class)					\
	HHOOK m_hHookCur;										\
	static LRESULT CALLBACK HookCreateProc(int nCode,		\
										   WPARAM wParam,	\
										   LPARAM lParam)	\
	{														\
		Class* pThis = (Class*) _Module.ExtractCreateWndData();					\
																				\
		if (nCode != HCBT_CREATEWND)											\
		{																		\
			/*Add it back again*/												\
			_Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);				\
			/*wait for HCBT_CREATEWND just pass others on...*/					\
			return CallNextHookEx(pThis->m_hHookCur, nCode, wParam, lParam);	\
		}																		\
																				\
		pThis->SubclassWindow((HWND)wParam);									\
																				\
		UnhookWindowsHookEx(pThis->m_hHookCur);									\
		HHOOK hHookCur = pThis->m_hHookCur;										\
		pThis->m_hHookCur = NULL;												\
																				\
		return CallNextHookEx(hHookCur, nCode, wParam, lParam);					\
	}

// Use this *immediately* before the code that creates the window
// It will subclass the first window that's created by that thread
// so add immediately before.
#define HOOK_AND_SUBCLASS_NEXT()												\
	((m_hHookCur = SetWindowsHookEx(WH_CBT, HookCreateProc, NULL, ::GetCurrentThreadId())) == NULL)

const LPCTSTR kDialogClass = (LPCTSTR)0x00008002;

template<class _Class>
class CHookWindow
{
public:
	CHookWindow()
	{
		m_hHookCur = NULL;
		m_szHookClass = NULL;
		m_szHookName = NULL;
	}

	bool SubclassNext(LPCTSTR szClass = NULL, LPCTSTR szName = NULL)
	{
		_Class* pThis = (_Class*)this;
		_Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);

		m_hHookCur = SetWindowsHookEx(WH_CBT, HookCreateProc, NULL, ::GetCurrentThreadId());

		if(m_hHookCur)
		{
			m_szHookClass = szClass;
			m_szHookName = szName;
		}

		return m_hHookCur == NULL;
	}

	static LRESULT CALLBACK HookCreateProc(int nCode, WPARAM wParam, LPARAM lParam)
	{
		_Class* pThis = (_Class*) _Module.ExtractCreateWndData();
		HHOOK hHookCur = pThis->m_hHookCur;

		bool bSkip = false;

		if(nCode != HCBT_CREATEWND)
		{
			bSkip = true;
		}
		else
		{
			CBT_CREATEWND* pCreate = (CBT_CREATEWND*)lParam;

			if(pThis->m_szHookClass)
			{
				// If it's not a pointer (for at least two bytes)
				if(IsBadReadPtr(pCreate->lpcs->lpszClass, 2) || IsBadReadPtr(pThis->m_szHookClass, 2))
					// Then just compare values
					bSkip = pCreate->lpcs->lpszClass != pThis->m_szHookClass;

				else
					// Otherwise compare strings
					bSkip = _tcsicmp(pCreate->lpcs->lpszClass, pThis->m_szHookClass) != 0;
			}

			if(pThis->m_szHookName)
			{
				if(IsBadReadPtr(pCreate->lpcs->lpszName, _tcslen(pThis->m_szHookName)) ||
				   _tcsicmp(pCreate->lpcs->lpszName, pThis->m_szHookName))
				    bSkip = true;
			}
		}

		if(bSkip)
		{
			/*Add it back again*/
			_Module.AddCreateWndData(&(pThis->m_thunk.cd), pThis);
		}
		else
		{
			pThis->SubclassWindow((HWND)wParam);

			UnhookWindowsHookEx(pThis->m_hHookCur);
			pThis->m_hHookCur = NULL;
		}

		return CallNextHookEx(hHookCur, nCode, wParam, lParam);
	}

protected:
	HHOOK m_hHookCur;
	LPCTSTR m_szHookName;
	LPCTSTR m_szHookClass;
};


#endif //__ATLWINHK_H__