update for HEAD-2003091401
[reactos.git] / subsys / system / explorer / bak / shellhook.cpp
1 /*
2  * Copyright 2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20  //
21  // Explorer clone
22  //
23  // shellhook.cpp
24  //
25  // Martin Fuchs, 17.08.2003
26  //
27
28
29 #include "../utility/utility.h"
30
31 #include "shellhook.h"
32
33
34 static HINSTANCE s_hInstance;
35
36 struct SharedMem {
37         HWND    _callback_hwnd;
38         UINT    _callback_msg;
39         HHOOK   _hshellHook;
40 };
41
42 static HANDLE s_hMapObject;
43 static SharedMem* s_pSharedMem;
44
45
46 BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID)
47 {
48         switch(dwReason) {
49           case DLL_PROCESS_ATTACH: {
50                 s_hInstance = hInst;
51                 DisableThreadLibraryCalls(hInst);
52                 s_hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
53                                                                                                 0, sizeof(SharedMem), _T("ROSExplorerShellHookSharedMem"));
54                 if (!s_hMapObject)
55                         return FALSE;
56
57                  // The first process to attach initializes memory.
58                 BOOL bInit = GetLastError()!=ERROR_ALREADY_EXISTS;
59
60                  // Get a pointer to the file-mapped shared memory.
61                 s_pSharedMem = (SharedMem*) MapViewOfFile(s_hMapObject, FILE_MAP_WRITE, 0, 0, 0);
62                 if (!s_pSharedMem)
63                         return FALSE;
64
65                  // Initialize memory if this is the first process.
66                 if (bInit)
67                         memset(s_pSharedMem, 0, sizeof(SharedMem));
68                 break;}
69
70           case DLL_PROCESS_DETACH:
71                 UnmapViewOfFile(s_pSharedMem);
72                 s_pSharedMem = NULL;
73                 CloseHandle(s_hMapObject);
74                 break;
75         }
76
77     return TRUE;
78 }
79
80
81 static void DoCallBack(int code, WPARAM wparam, LPARAM lparam)
82 {
83         UINT callback_msg = s_pSharedMem->_callback_msg;
84         HWND callback_hwnd = s_pSharedMem->_callback_hwnd;
85
86         if (callback_msg)
87                 PostMessage(callback_hwnd, callback_msg, wparam, code); // lparam unused
88 }
89
90
91 LRESULT CALLBACK ShellHookProc(int code, WPARAM wparam, LPARAM lparam)
92 {
93         DoCallBack(code, wparam, lparam);
94
95         return CallNextHookEx(s_pSharedMem->_hshellHook, code, wparam, lparam);
96 }
97
98
99 void InstallShellHook(HWND callback_hwnd, UINT callback_msg)
100 {
101         s_pSharedMem->_callback_hwnd = callback_hwnd;
102         s_pSharedMem->_callback_msg = callback_msg;
103
104         s_pSharedMem->_hshellHook = SetWindowsHookEx(WH_SHELL, ShellHookProc, s_hInstance, 0);
105 }
106
107 void DeinstallShellHook()
108 {
109         s_pSharedMem->_callback_hwnd = 0;
110         s_pSharedMem->_callback_msg = 0;
111
112         UnhookWindowsHookEx(s_pSharedMem->_hshellHook);
113         s_pSharedMem->_hshellHook = 0;
114 }