This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTopmost.c
73 lines (65 loc) · 2.43 KB
/
Topmost.c
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
/* Copyright (C) 2008-2011, Manuel Meitinger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Windows.h>
#ifdef _WIN64
#define MUTEX_NAME TEXT("{E88E4EFC-BEDA-463C-AC93-65154B1817FC}")
#else
#define MUTEX_NAME TEXT("{91383FDC-AF5D-4C17-9347-C3186177EAF6}")
#endif
INT WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, INT cmdShow)
{
// ensure that only one instance is running (but don't fail if the test fails)
HANDLE mutex = CreateMutex(NULL, FALSE, MUTEX_NAME);
if (mutex == NULL || GetLastError() != ERROR_ALREADY_EXISTS)
{
HMODULE library;
// load the hook library
if ((library = LoadLibrary(TEXT("Hook.dll"))) == NULL) return GetLastError();
__try
{
HOOKPROC callWndProc;
HHOOK callWndHook;
// initialize the hook for injecting the "always on top" entry into the system menu
if ((callWndProc = (HOOKPROC)GetProcAddress(library, "CallWndProc")) == NULL) return GetLastError();
if ((callWndHook = SetWindowsHookEx(WH_CALLWNDPROC, callWndProc, library, 0)) == NULL) return GetLastError();
__try
{
HOOKPROC getMsgProc;
HHOOK getMsgHook;
// initialize the hook for intercepting the entry's command messages
if ((getMsgProc = (HOOKPROC)GetProcAddress(library, "GetMsgProc")) == NULL) return GetLastError();
if ((getMsgHook = SetWindowsHookEx(WH_GETMESSAGE, getMsgProc, library, 0)) == NULL) return GetLastError();
__try
{
MSG msg;
BOOL ret;
// pump the messages until the end of the session
while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (ret == -1) return GetLastError();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
__finally { UnhookWindowsHookEx(getMsgHook); }
}
__finally { UnhookWindowsHookEx(callWndHook); }
}
__finally { FreeLibrary(library); }
}
// return success
return ERROR_SUCCESS;
}