Skip to content

Commit

Permalink
Update door.py use OutputDebugStringW for logging. Use Visual Studio …
Browse files Browse the repository at this point in the history
…2017 Community sln.
  • Loading branch information
8ming committed Sep 30, 2018
1 parent 6951981 commit ec06219
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 35 deletions.
68 changes: 49 additions & 19 deletions door.py
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 7 additions & 1 deletion pydoor/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,8 +38,11 @@ int RunDoor() {
}

PyRun_SimpleFile(file, PY_SCRIPT_NAME);

Py_Finalize();
fclose(file);

AppendLog("RunDoor end.");
return 0;
}

Expand All @@ -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;
Expand All @@ -71,3 +75,5 @@ BOOL WINAPI DllMain(
}
return TRUE;
}

// vim: sw=2:ts=2
12 changes: 6 additions & 6 deletions pydoor/pydoor.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -22,32 +22,32 @@
<ProjectGuid>{C8AEF1D1-B282-4F1B-B1C7-3A78A3B628AF}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>pydoor</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions python.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros">
<PythonDir Condition="'$(PlatformTarget)' == 'x86'">C:\Python36-32</PythonDir>
<PythonDir Condition="'$(PlatformTarget)' == 'x64'">C:\Python36</PythonDir>
<PythonDir Condition="'$(PlatformTarget)' == 'x86'">C:\Python37-32</PythonDir>
<PythonDir Condition="'$(PlatformTarget)' == 'x64'">C:\Python37</PythonDir>
</PropertyGroup>
<PropertyGroup />
<ItemDefinitionGroup>
Expand Down
6 changes: 5 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions test/test.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -22,32 +22,32 @@
<ProjectGuid>{30DBF561-8F24-4872-9E33-37ED4F4E877B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>test</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down

0 comments on commit ec06219

Please sign in to comment.