1 /*
2  * Copyright 2004 Martin Fuchs
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 
20  //
21  // NotifyHook DLL for ROS Explorer
22  //
23  // notifyhook.cpp
24  //
25  // Martin Fuchs, 17.03.2004
26  //
27 
28 
29 #include "../utility/utility.h"
30 
31 #include "notifyhook.h"
32 
33 
34 static HINSTANCE s_hInstance;
35 static UINT WM_GETMODULEPATH;
36 static HHOOK s_hNotifyHook;
37 
38 
39 BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID param)
40 {
41 	switch(dwReason) {
42 	  case DLL_PROCESS_ATTACH:
43 		s_hInstance = hInst;
44 		DisableThreadLibraryCalls(hInst);
45 		WM_GETMODULEPATH = RegisterWindowMessageA("WM_GETMODULEPATH");
46 		break;
47 	}
48 
49     return TRUE;
50 }
51 
52 
53 struct COPYDATA_STRUCT {
54 	HWND	_hwnd;
55 	int		_len;
56 	char	_path[MAX_PATH];
57 };
58 
59 LRESULT CALLBACK NotifyHookProc(int code, WPARAM wparam, LPARAM lparam)
60 {
61 	MSG* pmsg = (MSG*)lparam;
62 
63 	if (pmsg->message == WM_GETMODULEPATH) {
64 		struct COPYDATA_STRUCT cds;
65 		COPYDATASTRUCT data;
66 
67 		cds._hwnd = pmsg->hwnd;
68 		cds._len = GetWindowModuleFileNameA(pmsg->hwnd, cds._path, COUNTOF(cds._path));
69 
70 		data.dwData = WM_GETMODULEPATH;
71 		data.cbData = sizeof(cds);
72 		data.lpData = &cds;
73 
74 		SendMessage((HWND)pmsg->wParam, WM_COPYDATA, (WPARAM)pmsg->hwnd, (LPARAM)&data);
75 
76 		return 0;
77 	}
78 
79 	return CallNextHookEx(s_hNotifyHook, code, wparam, lparam);
80 }
81 
82 
83 UINT InstallNotifyHook()
84 {
85 	s_hNotifyHook = SetWindowsHookEx(WH_GETMESSAGE, NotifyHookProc, s_hInstance, 0);
86 
87 	return WM_GETMODULEPATH;
88 }
89 
90 void DeinstallNotifyHook()
91 {
92 	UnhookWindowsHookEx(s_hNotifyHook);
93 	s_hNotifyHook = 0;
94 }
95 
96 
97 void GetWindowModulePath(HWND hwnd)
98 {
99 	SendMessage(hwnd, WM_GETMODULEPATH, 0, 0);
100 }
101 
102  // retrieve module path by receiving WM_COPYDATA message
103 DECL_NOTIFYHOOK int GetWindowModulePathCopyData(LPARAM lparam, HWND* phwnd, LPSTR buffer, int size)
104 {
105 	PCOPYDATASTRUCT data = (PCOPYDATASTRUCT) lparam;
106 
107 	if (data->dwData == WM_GETMODULEPATH) {
108 		struct COPYDATA_STRUCT* cds = (struct COPYDATA_STRUCT*) data->lpData;
109 
110 		*phwnd = cds->_hwnd;
111 		lstrcpyn(buffer, cds->_path, size);
112 
113 		return cds->_len;
114 	} else
115 		return 0;
116 }
117