Created
September 27, 2012 02:21
-
-
Save saga/3791807 to your computer and use it in GitHub Desktop.
outputdebugstring python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original code by Dan Brotherston | |
import sys | |
import mmap | |
import struct | |
import win32event | |
import thread | |
import time | |
def OutputDebug(threadName, delay): | |
buffer_ready = win32event.CreateEvent(None, 0, 0, "DBWIN_BUFFER_READY") | |
data_ready = win32event.CreateEvent(None, 0, 0, "DBWIN_DATA_READY") | |
buffer = mmap.mmap(0, 4096, "DBWIN_BUFFER", mmap.ACCESS_WRITE) | |
while True: | |
# Signal that we're ready to accept debug output | |
win32event.SetEvent(buffer_ready) | |
if win32event.WaitForSingleObject(data_ready, win32event.INFINITE) == win32event.WAIT_OBJECT_0: | |
buffer.seek(0) | |
# The first DWORD is the process id which generated the output | |
process_id, = struct.unpack("L", buffer.read (4)) | |
data = buffer.read(4092) | |
if "\0" in data: | |
str1 = data[:data.index("\0")] | |
else: | |
str1 = data | |
if str1.find("##") >= 0: | |
ticks = time.localtime() | |
print "Pid%d [%d:%d:%d]: %s" % (process_id, ticks.tm_hour, ticks.tm_min, ticks.tm_sec, str1) | |
try: | |
thread.start_new_thread(OutputDebug, ("Thread-1", 0, )) | |
except: | |
print "Error: unable to start thread" | |
while 1: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment