• R/O
  • HTTP
  • SSH
  • HTTPS

yamy: Commit

source code repository of yamy


Commit MetaInfo

Revision7dbb48abb9d2df4406965049f9e06c539fabe5e3 (tree)
Time2009-09-13 16:48:20
AuthorU-i7\gimy <gimy@user...>
CommiterU-i7\gimy

Log Message

1. use WM_COPYDATA to notify from yamy{32,64}.dll to yamy instead of mailslot if process can't open mailslot(typically in low integrity level as protected mode IE), this fix tickets #17769 and #18662
2. yamy{32,64}.dll put log to file in %USEPROFILE%\AppData\LocalLow folder for process in low integrity level as protected mode IE. to enable this, define HOOK_LOG_TO_FILE in hook.{h,cpp}

Change Summary

Incremental Difference

--- a/engine.cpp
+++ b/engine.cpp
@@ -1213,6 +1213,13 @@ Engine::Engine(tomsgstream &i_log)
12131213 m_afShellExecute(NULL),
12141214 m_variable(0),
12151215 m_log(i_log) {
1216+ BOOL (WINAPI *pChangeWindowMessageFilter)(UINT, DWORD) =
1217+ reinterpret_cast<BOOL (WINAPI*)(UINT, DWORD)>(GetProcAddress(GetModuleHandle(_T("user32.dll")), "ChangeWindowMessageFilter"));
1218+
1219+ if(pChangeWindowMessageFilter != NULL) {
1220+ pChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
1221+ }
1222+
12161223 for (size_t i = 0; i < NUMBER_OF(m_lastPressedKey); ++ i)
12171224 m_lastPressedKey[i] = NULL;
12181225
--- a/hook.cpp
+++ b/hook.cpp
@@ -78,6 +78,9 @@ struct Globals {
7878 DWORD m_hwndTaskTray; ///
7979 HANDLE m_hMailslot;
8080 bool m_isInitialized;
81+#ifdef HOOK_LOG_TO_FILE
82+ HANDLE m_logFile;
83+#endif // HOOK_LOG_TO_FILE
8184 #ifndef NDEBUG
8285 bool m_isLogging;
8386 _TCHAR m_moduleName[GANA_MAX_PATH];
@@ -94,15 +97,33 @@ static Globals g;
9497 static void notifyThreadDetach();
9598 static void notifyShow(NotifyShow::Show i_show, bool i_isMDI);
9699 static void notifyLog(_TCHAR *i_msg);
97-static bool mapHookData();
100+static bool mapHookData(bool i_isYamy);
98101 static void unmapHookData();
99-static bool initialize();
102+static bool initialize(bool i_isYamy);
100103
101104
102105 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103106 // Functions
104107
105-bool initialize()
108+
109+#ifdef HOOK_LOG_TO_FILE
110+static void WriteToLog(const char *data)
111+{
112+ char buf[1024];
113+ DWORD count;
114+
115+ WideCharToMultiByte(CP_THREAD_ACP, 0, g.m_moduleName, -1, buf, NUMBER_OF(buf), NULL, NULL);
116+ strcat(buf, ": ");
117+ strcat(buf, data);
118+ SetFilePointer(g.m_logFile, 0, NULL, FILE_END);
119+ WriteFile(g.m_logFile, buf, strlen(buf), &count, NULL);
120+ FlushFileBuffers(g.m_logFile);
121+}
122+#else // !HOOK_LOG_TO_FILE
123+#define WriteToLog(data)
124+#endif // !HOOK_LOG_TO_FILE
125+
126+bool initialize(bool i_isYamy)
106127 {
107128 #ifndef NDEBUG
108129 _TCHAR path[GANA_MAX_PATH];
@@ -113,7 +134,14 @@ bool initialize()
113134 g.m_isLogging = true;
114135 }
115136 #endif // !NDEBUG
116-#ifdef USE_MAILSLOT
137+#ifdef HOOK_LOG_TO_FILE
138+ _TCHAR logFileName[GANA_MAX_PATH];
139+ GetEnvironmentVariable(_T("USERPROFILE"), logFileName, NUMBER_OF(logFileName));
140+ _tcsncat(logFileName, _T("\\AppData\\LocalLow\\yamydll.txt"), _tcslen(_T("\\AppData\\LocalLow\\yamydll.log")));
141+ g.m_logFile = CreateFile(logFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
142+ OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
143+#endif // HOOK_LOG_TO_FILE
144+ WriteToLog("try to open mailslot\r\n");
117145 g.m_hMailslot =
118146 CreateFile(NOTIFY_MAILSLOT_NAME, GENERIC_WRITE,
119147 FILE_SHARE_READ | FILE_SHARE_WRITE,
@@ -121,11 +149,12 @@ bool initialize()
121149 FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
122150 if (g.m_hMailslot == INVALID_HANDLE_VALUE) {
123151 HOOK_RPT2("MAYU: %S create mailslot failed(0x%08x)\r\n", g.m_moduleName, GetLastError());
152+ WriteToLog("open mailslot NG\r\n");
124153 } else {
125154 HOOK_RPT1("MAYU: %S create mailslot successed\r\n", g.m_moduleName);
155+ WriteToLog("open mailslot OK\r\n");
126156 }
127-#endif //USE_MAILSLOT
128- if (!mapHookData())
157+ if (!mapHookData(i_isYamy))
129158 return false;
130159 _tsetlocale(LC_ALL, _T(""));
131160 g.m_WM_MAYU_MESSAGE =
@@ -153,12 +182,16 @@ BOOL WINAPI DllMain(HINSTANCE i_hInstDLL, DWORD i_fdwReason,
153182 case DLL_PROCESS_DETACH:
154183 notifyThreadDetach();
155184 unmapHookData();
156-#ifdef USE_MAILSLOT
157185 if (g.m_hMailslot != INVALID_HANDLE_VALUE) {
158186 CloseHandle(g.m_hMailslot);
159187 g.m_hMailslot = INVALID_HANDLE_VALUE;
160188 }
161-#endif //USE_MAILSLOT
189+#ifdef HOOK_LOG_TO_FILE
190+ if (g.m_logFile != INVALID_HANDLE_VALUE) {
191+ CloseHandle(g.m_logFile);
192+ g.m_logFile = INVALID_HANDLE_VALUE;
193+ }
194+#endif // HOOK_LOG_TO_FILE
162195 break;
163196 case DLL_THREAD_DETACH:
164197 notifyThreadDetach();
@@ -171,36 +204,27 @@ BOOL WINAPI DllMain(HINSTANCE i_hInstDLL, DWORD i_fdwReason,
171204
172205
173206 /// map hook data
174-static bool mapHookData()
207+static bool mapHookData(bool i_isYamy)
175208 {
176- g.m_hHookData = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
177- 0, sizeof(HookData),
178- addSessionId(HOOK_DATA_NAME).c_str());
179- if (!g.m_hHookData) {
180- unmapHookData();
181- return false;
182- }
209+ DWORD access = FILE_MAP_READ;
183210
184- g_hookData =
185- (HookData *)MapViewOfFile(g.m_hHookData, FILE_MAP_READ | FILE_MAP_WRITE,
186- 0, 0, sizeof(HookData));
187- if (!g_hookData) {
188- unmapHookData();
189- return false;
211+ if (i_isYamy) {
212+ access |= FILE_MAP_WRITE;
213+ g.m_hHookData = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(HookData), addSessionId(HOOK_DATA_NAME).c_str());
214+ g.m_hHookDataArch = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(HookDataArch), addSessionId(HOOK_DATA_NAME_ARCH).c_str());
215+ } else {
216+ g.m_hHookData = OpenFileMapping(access, FALSE, addSessionId(HOOK_DATA_NAME).c_str());
217+ g.m_hHookDataArch = OpenFileMapping(access, FALSE, addSessionId(HOOK_DATA_NAME_ARCH).c_str());
190218 }
191219
192- g.m_hHookDataArch = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
193- 0, sizeof(HookDataArch),
194- addSessionId(HOOK_DATA_NAME_ARCH).c_str());
195- if (!g.m_hHookDataArch) {
220+ if (g.m_hHookData == NULL || g.m_hHookDataArch == NULL) {
196221 unmapHookData();
197222 return false;
198223 }
199224
200- s_hookDataArch =
201- (HookDataArch *)MapViewOfFile(g.m_hHookDataArch, FILE_MAP_READ | FILE_MAP_WRITE,
202- 0, 0, sizeof(HookDataArch));
203- if (!s_hookDataArch) {
225+ g_hookData = (HookData *)MapViewOfFile(g.m_hHookData, access, 0, 0, sizeof(HookData));
226+ s_hookDataArch = (HookDataArch *)MapViewOfFile(g.m_hHookDataArch, access, 0, 0, sizeof(HookDataArch));
227+ if (g_hookData == NULL || s_hookDataArch == NULL) {
204228 unmapHookData();
205229 return false;
206230 }
@@ -237,7 +261,6 @@ DllExport bool notify(void *i_data, size_t i_dataSize)
237261 DWORD result;
238262 #endif // MAYU64
239263
240-#ifdef USE_MAILSLOT
241264 DWORD len;
242265 if (g.m_hMailslot != INVALID_HANDLE_VALUE) {
243266 BOOL ret;
@@ -249,20 +272,19 @@ DllExport bool notify(void *i_data, size_t i_dataSize)
249272 HOOK_RPT1("MAYU: %S WriteFile to mailslot successed\r\n", g.m_moduleName);
250273 }
251274 #endif // !NDEBUG
275+ } else {
276+ cd.dwData = reinterpret_cast<Notify *>(i_data)->m_type;
277+ cd.cbData = i_dataSize;
278+ cd.lpData = i_data;
279+ if (g.m_hwndTaskTray == 0 || cd.dwData == Notify::Type_threadDetach)
280+ return false;
281+ if (!SendMessageTimeout(reinterpret_cast<HWND>(g.m_hwndTaskTray),
282+ WM_COPYDATA, NULL, reinterpret_cast<LPARAM>(&cd),
283+ SMTO_ABORTIFHUNG | SMTO_NORMAL, 5000, &result)) {
284+ _RPT0(_CRT_WARN, "MAYU: SendMessageTimeout() timeouted\r\n");
285+ return false;
286+ }
252287 }
253-#else // !USE_MAILSLOT
254- cd.dwData = reinterpret_cast<Notify *>(i_data)->m_type;
255- cd.cbData = i_dataSize;
256- cd.lpData = i_data;
257- if (g.m_hwndTaskTray == 0)
258- return false;
259- if (!SendMessageTimeout(reinterpret_cast<HWND>(g.m_hwndTaskTray),
260- WM_COPYDATA, NULL, reinterpret_cast<LPARAM>(&cd),
261- SMTO_ABORTIFHUNG | SMTO_NORMAL, 5000, &result)) {
262- _RPT0(_CRT_WARN, "MAYU: SendMessageTimeout() timeouted\r\n");
263- return false;
264- }
265-#endif // !USE_MAILSLOT
266288 return true;
267289 }
268290
@@ -554,7 +576,7 @@ DllExport void notifyLockState()
554576 LRESULT CALLBACK getMessageProc(int i_nCode, WPARAM i_wParam, LPARAM i_lParam)
555577 {
556578 if (!g.m_isInitialized)
557- initialize();
579+ initialize(false);
558580
559581 if (!g_hookData)
560582 return 0;
@@ -635,7 +657,7 @@ finally:
635657 LRESULT CALLBACK callWndProc(int i_nCode, WPARAM i_wParam, LPARAM i_lParam)
636658 {
637659 if (!g.m_isInitialized)
638- initialize();
660+ initialize(false);
639661
640662 if (!g_hookData)
641663 return 0;
@@ -747,7 +769,7 @@ LRESULT CALLBACK callWndProc(int i_nCode, WPARAM i_wParam, LPARAM i_lParam)
747769 static LRESULT CALLBACK lowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
748770 {
749771 if (!g.m_isInitialized)
750- initialize();
772+ initialize(false);
751773
752774 if (!g_hookData || nCode < 0)
753775 goto through;
@@ -771,7 +793,7 @@ static LRESULT CALLBACK lowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lP
771793 KBDLLHOOKSTRUCT *pKbll = (KBDLLHOOKSTRUCT*)lParam;
772794
773795 if (!g.m_isInitialized)
774- initialize();
796+ initialize(false);
775797
776798 if (!g_hookData || nCode < 0)
777799 goto through;
@@ -790,11 +812,14 @@ through:
790812
791813
792814 /// install message hook
793-DllExport int installMessageHook()
815+DllExport int installMessageHook(DWORD i_hwndTaskTray)
794816 {
795817 if (!g.m_isInitialized)
796- initialize();
818+ initialize(true);
797819
820+ if (i_hwndTaskTray) {
821+ g_hookData->m_hwndTaskTray = i_hwndTaskTray;
822+ }
798823 g.m_hwndTaskTray = g_hookData->m_hwndTaskTray;
799824 s_hookDataArch->m_hHookGetMessage =
800825 SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)getMessageProc,
@@ -824,7 +849,7 @@ DllExport int installKeyboardHook(INPUT_DETOUR i_keyboardDetour, Engine *i_engin
824849 {
825850 if (i_install) {
826851 if (!g.m_isInitialized)
827- initialize();
852+ initialize(true);
828853
829854 g.m_keyboardDetour = i_keyboardDetour;
830855 g.m_engine = i_engine;
@@ -845,7 +870,7 @@ DllExport int installMouseHook(INPUT_DETOUR i_mouseDetour, Engine *i_engine, boo
845870 {
846871 if (i_install) {
847872 if (!g.m_isInitialized)
848- initialize();
873+ initialize(true);
849874
850875 g.m_mouseDetour = i_mouseDetour;
851876 g.m_engine = i_engine;
--- a/hook.h
+++ b/hook.h
@@ -13,11 +13,9 @@
1313 # define HOOK_PIPE_NAME \
1414 _T("\\\\.\\pipe\\GANAware\\mayu\\{4B22D464-7A4E-494b-982A-C2B2BBAAF9F3}") _T(VERSION)
1515 ///
16-#ifdef USE_MAILSLOT
1716 # define NOTIFY_MAILSLOT_NAME \
1817 _T("\\\\.\\mailslot\\GANAware\\mayu\\{330F7914-EB5B-49be-ACCE-D2B8DF585B32}") _T(VERSION)
1918 ///
20-#endif // USE_MAILSLOT
2119 # define WM_MAYU_MESSAGE_NAME _T("GANAware\\mayu\\WM_MAYU_MESSAGE")
2220
2321 ///
@@ -140,7 +138,7 @@ public:
140138
141139 # ifndef _HOOK_CPP
142140 extern DllImport HookData *g_hookData;
143-extern DllImport int installMessageHook();
141+extern DllImport int installMessageHook(DWORD i_hwndTaskTray);
144142 extern DllImport int uninstallMessageHook();
145143 extern DllImport int installKeyboardHook(INPUT_DETOUR i_keyboardDetour, Engine *i_engine, bool i_install);
146144 extern DllImport int installMouseHook(INPUT_DETOUR i_mouseDetour, Engine *i_engine, bool i_install);
--- a/mayu.cpp
+++ b/mayu.cpp
@@ -70,12 +70,10 @@ class Mayu
7070 PROCESS_INFORMATION m_pi;
7171 #endif // _WIN64
7272 HANDLE m_mutex;
73-#ifdef USE_MAILSLOT
7473 HANDLE m_hNotifyMailslot; /// mailslot to receive notify
7574 HANDLE m_hNotifyEvent; /// event on receive notify
7675 OVERLAPPED m_olNotify; ///
7776 BYTE m_notifyBuf[NOTIFY_MESSAGE_SIZE];
78-#endif // USE_MAILSLOT
7977 static const DWORD SESSION_LOCKED = 1<<0;
8078 static const DWORD SESSION_DISCONNECTED = 1<<1;
8179 static const DWORD SESSION_END_QUERIED = 1<<2;
@@ -103,7 +101,6 @@ class Mayu
103101 };
104102
105103 private:
106-#ifdef USE_MAILSLOT
107104 static VOID CALLBACK mailslotProc(DWORD i_code, DWORD i_len, LPOVERLAPPED i_ol) {
108105 Mayu *pThis;
109106
@@ -131,7 +128,6 @@ private:
131128 &m_olNotify, Mayu::mailslotProc);
132129 return result;
133130 }
134-#endif // USE_MAILSLOT
135131
136132 /// register class for tasktray
137133 ATOM Register_tasktray() {
@@ -1020,7 +1016,6 @@ public:
10201016 m_engine(m_log) {
10211017 Registry reg(MAYU_REGISTRY_ROOT);
10221018 reg.read(_T("escapeNLSKeys"), &m_escapeNlsKeys, 0);
1023-#ifdef USE_MAILSLOT
10241019 m_hNotifyMailslot = CreateMailslot(NOTIFY_MAILSLOT_NAME, 0, MAILSLOT_WAIT_FOREVER, (SECURITY_ATTRIBUTES *)NULL);
10251020 ASSERT(m_hNotifyMailslot != INVALID_HANDLE_VALUE);
10261021 int err;
@@ -1036,7 +1031,6 @@ public:
10361031 m_olNotify.Offset = 0;
10371032 m_olNotify.OffsetHigh = 0;
10381033 m_olNotify.hEvent = m_hNotifyEvent;
1039-#endif // USE_MAILSLOT
10401034 time(&m_startTime);
10411035
10421036 CHECK_TRUE( Register_focus() );
@@ -1062,10 +1056,7 @@ public:
10621056 CHECK_TRUE( m_hwndTaskTray );
10631057
10641058 // set window handle of tasktray to hooks
1065-#ifndef USE_MAILSLOT
1066- g_hookData->m_hwndTaskTray = reinterpret_cast<DWORD>(m_hwndTaskTray);
1067-#endif // !USE_MAILSLOT
1068- CHECK_FALSE( installMessageHook() );
1059+ CHECK_FALSE( installMessageHook(reinterpret_cast<DWORD>(m_hwndTaskTray)) );
10691060 m_usingSN = wtsRegisterSessionNotification(m_hwndTaskTray,
10701061 NOTIFY_FOR_THIS_SESSION);
10711062
@@ -1179,12 +1170,10 @@ public:
11791170
11801171 ///
11811172 ~Mayu() {
1182-#ifdef USE_MAILSLOT
11831173 CancelIo(m_hNotifyMailslot);
11841174 SleepEx(0, TRUE);
11851175 CloseHandle(m_hNotifyMailslot);
11861176 CloseHandle(m_hNotifyEvent);
1187-#endif // USE_MAILSLOT
11881177 ReleaseMutex(m_mutex);
11891178 WaitForSingleObject(m_mutex, INFINITE);
11901179 // first, detach log from edit control to avoid deadlock
@@ -1236,7 +1225,6 @@ public:
12361225 showBanner(false);
12371226 load();
12381227
1239-#ifdef USE_MAILSLOT
12401228 mailslotHandler(0, 0);
12411229 while (1) {
12421230 HANDLE handles[] = { m_hNotifyEvent };
@@ -1273,20 +1261,6 @@ public:
12731261 break;
12741262 }
12751263 }
1276-#else // !USE_MAILSLOT
1277- MSG msg;
1278- while (0 < GetMessage(&msg, NULL, 0, 0)) {
1279- if (IsDialogMessage(m_hwndLog, &msg))
1280- continue;
1281- if (IsDialogMessage(m_hwndInvestigate, &msg))
1282- continue;
1283- if (IsDialogMessage(m_hwndVersion, &msg))
1284- continue;
1285- TranslateMessage(&msg);
1286- DispatchMessage(&msg);
1287- }
1288- return msg.wParam;
1289-#endif // !USE_MAILSLOT
12901264 }
12911265 };
12921266
--- a/proj/yamy.vsprops
+++ b/proj/yamy.vsprops
@@ -9,7 +9,7 @@
99 <Tool
1010 Name="VCCLCompilerTool"
1111 AdditionalIncludeDirectories="../../boost_$(BOOST_MAJOR)_$(BOOST_MINOR)"
12- PreprocessorDefinitions="VERSION=\&quot;$(VERSION)\&quot;;LOGNAME=\&quot;$(USERNAME)\&quot;;COMPUTERNAME=\&quot;$(COMPUTERNAME)\&quot;;_CRT_SECURE_NO_WARNINGS;MAYU64;USE_MAILSLOT;USE_INI"
12+ PreprocessorDefinitions="VERSION=\&quot;$(VERSION)\&quot;;LOGNAME=\&quot;$(USERNAME)\&quot;;COMPUTERNAME=\&quot;$(COMPUTERNAME)\&quot;;_CRT_SECURE_NO_WARNINGS;MAYU64;USE_INI"
1313 />
1414 <Tool
1515 Name="VCLinkerTool"
--- a/yamyd.cpp
+++ b/yamyd.cpp
@@ -10,7 +10,7 @@ int WINAPI _tWinMain(HINSTANCE /* i_hInstance */, HINSTANCE /* i_hPrevInstance *
1010 {
1111 HANDLE mutex = OpenMutex(SYNCHRONIZE, FALSE, MUTEX_YAMYD_BLOCKER);
1212 if (mutex != NULL) {
13- CHECK_FALSE( installMessageHook() );
13+ CHECK_FALSE( installMessageHook(0) );
1414
1515 // wait for master process exit
1616 WaitForSingleObject(mutex, INFINITE);
Show on old repository browser