OSDN Git Service

Allow output of debug logs on failures of IPC functions
authorKazuhiro Fujieda <fujieda@users.sourceforge.jp>
Thu, 23 Aug 2012 13:53:00 +0000 (22:53 +0900)
committerKazuhiro Fujieda <fujieda@users.sourceforge.jp>
Thu, 23 Aug 2012 14:15:30 +0000 (23:15 +0900)
xkeymacs/mainfrm.cpp
xkeymacs/xkeymacs.cpp
xkeymacs/xkeymacs64.cpp
xkeymacsdll/xkeymacsdll.cpp

index 35db6a4..b2983a9 100644 (file)
@@ -5,6 +5,7 @@
 #include "xkeymacs.h"\r
 #include "profile.h"\r
 #include "../xkeymacsdll/xkeymacsdll.h"\r
+#include "../xkeymacsdll/Utils.h"\r
 \r
 #ifdef _DEBUG\r
 #define new DEBUG_NEW\r
@@ -171,23 +172,52 @@ void CMainFrame::TerminatePollThread()
 bool SendAck(HANDLE pipe)\r
 {\r
        DWORD written, ack = 0;\r
-       return WriteFile(pipe, &ack, sizeof(DWORD), &written, NULL) && written == sizeof(DWORD) &&\r
-                       FlushFileBuffers(pipe) && DisconnectNamedPipe(pipe);\r
+       if (!WriteFile(pipe, &ack, sizeof(DWORD), &written, NULL) && written != sizeof(DWORD)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: WriteFile failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       if (!FlushFileBuffers(pipe)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: FlushFileBuffers failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       if (!DisconnectNamedPipe(pipe)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: DisconnectNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       return true;\r
 }\r
 \r
 DWORD WINAPI CMainFrame::PollMessage(LPVOID)\r
 {\r
        HANDLE pipe = CreateNamedPipe(XKEYMACS32_PIPE, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1,\r
                                                                        sizeof(DWORD), sizeof(IPC32Message), 0, NULL);\r
-       if (pipe == INVALID_HANDLE_VALUE)\r
+       if (pipe == INVALID_HANDLE_VALUE) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("PollMessage: CreateNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
                return 1;\r
+       }\r
        for (; ;) {\r
-               if (ConnectNamedPipe(pipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED))\r
+               if (ConnectNamedPipe(pipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED)) {\r
+#ifdef DEBUG_IPC\r
+                       CUtils::Log(_T("PollMessage: ConnectNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
                        break;\r
+               }\r
                IPC32Message msg;\r
                DWORD read;\r
-               if (!ReadFile(pipe, &msg, sizeof(msg), &read, NULL))\r
+               if (!ReadFile(pipe, &msg, sizeof(msg), &read, NULL)) {\r
+#ifdef DEBUG_IPC\r
+                       CUtils::Log(_T("PollMessage: ReadFile failed. (%d)"), GetLastError());\r
+#endif\r
                        break;\r
+               }\r
                switch (msg.Type) {\r
                case IPC32_TERMINATE:\r
                        SendAck(pipe);\r
index 578a39f..0833b65 100644 (file)
@@ -4,6 +4,7 @@
 #include "xkeymacs.h"\r
 #include "mainfrm.h"\r
 #include "profile.h"\r
+#include "../xkeymacsdll/Utils.h"\r
 \r
 #ifdef _DEBUG\r
 #define new DEBUG_NEW\r
@@ -121,7 +122,11 @@ void CXkeymacsApp::SendIPC64Message(DWORD msg)
        DWORD ack, read;\r
        for (int i = 0; i < 10; Sleep(100), ++i)\r
                if (CallNamedPipe(XKEYMACS64_PIPE, &msg, sizeof(msg), &ack, sizeof(DWORD), &read, NMPWAIT_WAIT_FOREVER))\r
-                       return;;\r
+                       return;\r
+#ifdef DEBUG_IPC\r
+               else\r
+                       CUtils::Log(_T("SendIPC64Message: CallNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
 }\r
 \r
 int CXkeymacsApp::ExitInstance() \r
index b5ff41a..fa1be7a 100644 (file)
@@ -1,6 +1,7 @@
 #include "xkeymacs64.h"\r
 #include "mainfrm64.h"\r
 #include "../xkeymacsdll/xkeymacsdll.h"\r
+#include "../xkeymacsdll/Utils.h"\r
 \r
 CXkeymacsApp::CXkeymacsApp()\r
 {\r
@@ -37,18 +38,54 @@ BOOL CXkeymacsApp::InitInstance()
        return TRUE;\r
 }\r
 \r
+bool SendAck(HANDLE pipe)\r
+{\r
+       DWORD written, ack = 0;\r
+       if (!WriteFile(pipe, &ack, sizeof(DWORD), &written, NULL) && written != sizeof(DWORD)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: WriteFile failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       if (!FlushFileBuffers(pipe)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: FlushFileBuffers failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       if (!DisconnectNamedPipe(pipe)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendAck: DisconnectNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
+               return false;\r
+       }\r
+       return true;\r
+}\r
+\r
 UINT PollIPCMessage(LPVOID param)\r
 {\r
        HANDLE hPipe = CreateNamedPipe(XKEYMACS64_PIPE, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 512, 512, 0, NULL);\r
-       if (hPipe == INVALID_HANDLE_VALUE)\r
+       if (hPipe == INVALID_HANDLE_VALUE) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("PollIPCMessage: CreateNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
                return 1;\r
+       }\r
        for (; ;) {\r
-               if (ConnectNamedPipe(hPipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED))\r
+               if (ConnectNamedPipe(hPipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED)) {\r
+#ifdef DEBUG_IPC\r
+                       CUtils::Log(_T("PollIPCMessage: ConnectNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
                        break;\r
+               }\r
                DWORD msg;\r
                DWORD read;\r
-               if (!ReadFile(hPipe, &msg, sizeof(msg), &read, NULL) || read != sizeof(msg)) \r
+               if (!ReadFile(hPipe, &msg, sizeof(msg), &read, NULL) || read != sizeof(msg)) {\r
+#ifdef DEBUG_IPC\r
+                       CUtils::Log(_T("PollIPCMessage: ReadFile failed. (%d)"), GetLastError());\r
+#endif\r
                        break;\r
+               }\r
                switch (msg)\r
                {\r
                case IPC64_EXIT:\r
@@ -67,10 +104,7 @@ UINT PollIPCMessage(LPVOID param)
                        CXkeymacsDll::SetHookStateDirect(true);\r
                        break;\r
                }\r
-               DWORD written, ack = 0;\r
-               if (!WriteFile(hPipe, &ack, sizeof(DWORD), &written, NULL) || written != sizeof(DWORD)\r
-                               || !FlushFileBuffers(hPipe) || !DisconnectNamedPipe(hPipe))\r
-                       break;\r
+               SendAck(hPipe);\r
        }\r
 exit:\r
        CloseHandle(hPipe);\r
index 72a8a67..21f77a3 100644 (file)
@@ -222,7 +222,8 @@ void CXkeymacsDll::SetHookState(bool enable)
        IPC32Message msg;\r
        msg.Type = IPC32_HOOKSTATE;\r
        msg.Enable = enable;\r
-       CallNamedPipe(XKEYMACS32_PIPE, &msg, offsetof(IPC32Message, Enable) + sizeof(bool), &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT);\r
+       if (!CallNamedPipe(XKEYMACS32_PIPE, &msg, offsetof(IPC32Message, Enable) + sizeof(bool), &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT))\r
+               CUtils::Log(_T("SetHookState: CallNamedPipe failed. (%d)"), GetLastError());\r
 \r
        ShowHookState();\r
 }\r
@@ -776,7 +777,11 @@ void CXkeymacsDll::SendIconMessage(IconState *state, int num)
        IPC32Message msg;\r
        msg.Type = IPC32_ICON;\r
        memcpy(msg.IconState, state, num * sizeof(IconState));\r
-       CallNamedPipe(XKEYMACS32_PIPE, &msg, offsetof(IPC32Message, IconState) + sizeof(IconState) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT);\r
+       if (!CallNamedPipe(XKEYMACS32_PIPE, &msg, offsetof(IPC32Message, IconState) + sizeof(IconState) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT)) {\r
+#ifdef DEBUG_IPC\r
+               CUtils::Log(_T("SendIconMessage: CallNamedPipe failed. (%d)"), GetLastError());\r
+#endif\r
+       }\r
 }\r
 \r
 void CXkeymacsDll::Kdu(BYTE bVk, DWORD n, BOOL bOriginal)\r