/*
Copyright (C) 2010, Heikki Salo
All rights reserved.
Distributed under the BSD license:
http://www.opensource.org/licenses/bsd-license.php
*/
#include "stdafx.h"
//Set to 1 for OutputDebugString() hooks.
#if 0
#include <C:/Ohjelmointi/Detours Express 2.1/include/detours.h>
#pragma comment(lib, "C:/Ohjelmointi/Detours Express 2.1/lib/detours.lib")
#pragma comment(lib, "C:/Ohjelmointi/Detours Express 2.1/lib/detoured.lib")
void (WINAPI* OrginalOutputDebugStringW) (LPCWSTR) = OutputDebugStringW;
void (WINAPI* OrginalOutputDebugStringA) (LPCSTR) = OutputDebugStringA;
void WINAPI OutputDebugStringHookA(LPCSTR text)
{
if (Py_IsInitialized()) {
PyGILState_STATE gilState = PyGILState_Ensure();
/* PySys_WriteStdout() could in theory call
OutputDebugString() if someone has replaced
it with his own version. If this happens it
will recurse until it blows the stack.
*/
PySys_WriteStdout(text);
PyGILState_Release(gilState);
}
//Call the orginal.
OrginalOutputDebugStringA(text);
}
void WINAPI OutputDebugStringHookW(LPCWSTR text)
{
try {
std::wstring wide(text);
std::string ansi(wide.begin(), wide.end());
OutputDebugStringHookA(ansi.c_str());
}
catch (...) {
//Call the orginal.
OrginalOutputDebugStringW(text);
}
}
void DPInitDetours()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)OrginalOutputDebugStringW, OutputDebugStringHookW);
DetourAttach(&(PVOID&)OrginalOutputDebugStringA, OutputDebugStringHookA);
LONG error = DetourTransactionCommit();
if (error != NO_ERROR)
DPThrow("Can't commit detours hooks");
PySys_WriteStdout("DirectPython: OutputDebugString() hooked\n");
}
#else
void DPInitDetours()
{
PySys_WriteStdout("DirectPython: Detours not used\n");
}
#endif