2 * Copyright 2003 Martin Fuchs
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Martin Fuchs, 17.08.2003
29 #include "../utility/utility.h"
31 #include "shellhook.h"
34 static HINSTANCE s_hInstance;
42 static HANDLE s_hMapObject;
43 static SharedMem* s_pSharedMem;
46 BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID)
49 case DLL_PROCESS_ATTACH: {
51 DisableThreadLibraryCalls(hInst);
52 s_hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
53 0, sizeof(SharedMem), _T("ROSExplorerShellHookSharedMem"));
57 // The first process to attach initializes memory.
58 BOOL bInit = GetLastError()!=ERROR_ALREADY_EXISTS;
60 // Get a pointer to the file-mapped shared memory.
61 s_pSharedMem = (SharedMem*) MapViewOfFile(s_hMapObject, FILE_MAP_WRITE, 0, 0, 0);
65 // Initialize memory if this is the first process.
67 memset(s_pSharedMem, 0, sizeof(SharedMem));
70 case DLL_PROCESS_DETACH:
71 UnmapViewOfFile(s_pSharedMem);
73 CloseHandle(s_hMapObject);
81 static void DoCallBack(int code, WPARAM wparam, LPARAM lparam)
83 UINT callback_msg = s_pSharedMem->_callback_msg;
84 HWND callback_hwnd = s_pSharedMem->_callback_hwnd;
87 PostMessage(callback_hwnd, callback_msg, wparam, code); // lparam unused
91 LRESULT CALLBACK ShellHookProc(int code, WPARAM wparam, LPARAM lparam)
93 DoCallBack(code, wparam, lparam);
95 return CallNextHookEx(s_pSharedMem->_hshellHook, code, wparam, lparam);
99 void InstallShellHook(HWND callback_hwnd, UINT callback_msg)
101 s_pSharedMem->_callback_hwnd = callback_hwnd;
102 s_pSharedMem->_callback_msg = callback_msg;
104 s_pSharedMem->_hshellHook = SetWindowsHookEx(WH_SHELL, ShellHookProc, s_hInstance, 0);
107 void DeinstallShellHook()
109 s_pSharedMem->_callback_hwnd = 0;
110 s_pSharedMem->_callback_msg = 0;
112 UnhookWindowsHookEx(s_pSharedMem->_hshellHook);
113 s_pSharedMem->_hshellHook = 0;