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