diff --git a/door.py b/door.py index 1372a6a..9fe3c2a 100644 --- a/door.py +++ b/door.py @@ -1,28 +1,58 @@ import time import sys +import logging +import ctypes +import traceback -f = open('door.log', 'w') +OutputDebugString = ctypes.windll.kernel32.OutputDebugStringW +#OutputDebugString.argtypes = [ctypes.c_char_p] + +class DbgViewHandler(logging.Handler): + def __init__(self): + logging.Handler.__init__(self) + + def emit(self, record): + OutputDebugString(self.format(record)) -f.write("Started...\n") -f.flush() +logger = logging.getLogger("pydoor") +def init_logger(): + logger.setLevel(logging.DEBUG) + ch = DbgViewHandler() + ch.setLevel(logging.DEBUG) + formatter = logging.Formatter("%(asctime) -25s [%(thread) 5s] %(levelname) -8s %(name) -25s %(message)s") + ch.setFormatter(formatter) + logger.addHandler(ch) -class Unbuffered(object): - def __init__(self, stream): - self.stream = stream - def write(self, data): - self.stream.write(data) - self.stream.flush() - def flush(self): - self.stream.flush() - def __getattr__(self, attr): - return getattr(self.stream, attr) +class InfoStreamToLogger(object): + def write(self, data): + logger.info(data) + def flush(self): + pass -sys.stdout = sys.stdin = sys.__stdout__ = Unbuffered(f) +class ErrorStreamToLogger(object): + def write(self, data): + logger.error(data) + def flush(self): + pass -import IPython -IPython.embed_kernel() +sys.stdout = sys.__stdout__ = InfoStreamToLogger() +sys.stderr = sys.__stderr__ = ErrorStreamToLogger() -f.write("Stoped\n") -f.flush() +def main(): + init_logger() + try: + logger.info("door.py started, embed_kernel...") + + import IPython + IPython.embed_kernel() + logger.info("door.py end.") + except Exception as e: + err = traceback.format_exc() + logger.error("Exception!\n" + err) + +if __name__ == "__main__": + main() + + + -f.close() diff --git a/pydoor/dllmain.cpp b/pydoor/dllmain.cpp index 158ec04..bd9ea04 100644 --- a/pydoor/dllmain.cpp +++ b/pydoor/dllmain.cpp @@ -5,7 +5,6 @@ #include "pydoor.h" #define PY_SCRIPT_NAME "door.py" -#define LOG_FILENAME "door.log" short volatile inited = 0; HANDLE thread = INVALID_HANDLE_VALUE; @@ -39,8 +38,11 @@ int RunDoor() { } PyRun_SimpleFile(file, PY_SCRIPT_NAME); + Py_Finalize(); fclose(file); + + AppendLog("RunDoor end."); return 0; } @@ -56,10 +58,12 @@ BOOL WINAPI DllMain( switch (fdwReason) { case DLL_PROCESS_ATTACH: if (InterlockedCompareExchange16(&inited, 1, 0) == 0) { + AppendLog("Pydoor DLL Attach!"); thread = ::CreateThread(NULL, 0, ThreadWork, NULL, 0, NULL); } break; case DLL_PROCESS_DETACH: { + AppendLog("Pydoor DLL Detach!"); DWORD ret = ::TerminateThread(thread, 1); if (ret == STILL_ACTIVE) { return FALSE; @@ -71,3 +75,5 @@ BOOL WINAPI DllMain( } return TRUE; } + +// vim: sw=2:ts=2 \ No newline at end of file diff --git a/pydoor/pydoor.vcxproj b/pydoor/pydoor.vcxproj index 4846a77..6ed7ba4 100644 --- a/pydoor/pydoor.vcxproj +++ b/pydoor/pydoor.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -22,32 +22,32 @@ {C8AEF1D1-B282-4F1B-B1C7-3A78A3B628AF} Win32Proj pydoor - 8.1 + 10.0.17134.0 DynamicLibrary true - v140 + v141 Unicode DynamicLibrary false - v140 + v141 true Unicode DynamicLibrary true - v140 + v141 Unicode DynamicLibrary false - v140 + v141 true Unicode diff --git a/python.props b/python.props index 3b11987..6b339d3 100644 --- a/python.props +++ b/python.props @@ -2,8 +2,8 @@ - C:\Python36-32 - C:\Python36 + C:\Python37-32 + C:\Python37 diff --git a/test/main.cpp b/test/main.cpp index ba4e384..146cb87 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -6,7 +6,11 @@ int main() { printf("Press enter to load library..."); getchar(); - ::LoadLibraryA("pydoor.dll"); + HMODULE r =::LoadLibraryA("pydoor.dll"); + if (r == NULL) { + printf("LoadLibrary error!"); + return 0; + } printf("Sleep..."); Sleep(60 * 60 * 1000); // 1 hour // RunDoor(); diff --git a/test/test.vcxproj b/test/test.vcxproj index 8cfec31..9145ef8 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -22,32 +22,32 @@ {30DBF561-8F24-4872-9E33-37ED4F4E877B} Win32Proj test - 8.1 + 10.0.17134.0 Application true - v140 + v141 Unicode Application false - v140 + v141 true Unicode Application true - v140 + v141 Unicode Application false - v140 + v141 true Unicode