From 24aac526b3722b0b91b5193fd196ee88bea9b31a Mon Sep 17 00:00:00 2001 From: "dreamer.dead" Date: Thu, 23 Jan 2014 21:18:36 +0400 Subject: [PATCH] Fix hooks project --- Software/hooks/D3D9FrameGrabber.cpp | 23 +++++++++++---------- Software/hooks/DxgiFrameGrabber.cpp | 24 ++++++++++++---------- Software/hooks/GAPIProxyFrameGrabber.cpp | 6 +++--- Software/hooks/Logger.cpp | 16 +++++++-------- Software/hooks/Logger.hpp | 13 ++++++------ Software/hooks/ProxyFuncJmp.hpp | 4 ++-- Software/hooks/hooks.cpp | 2 ++ Software/hooks/hooks.h | 8 ++++++++ Software/hooks/hooks.pro | 26 +++++++++++++++--------- Software/hooks/hooksutils.h | 9 +++++++- 10 files changed, 78 insertions(+), 53 deletions(-) diff --git a/Software/hooks/D3D9FrameGrabber.cpp b/Software/hooks/D3D9FrameGrabber.cpp index 683f4a76..2aaa8319 100644 --- a/Software/hooks/D3D9FrameGrabber.cpp +++ b/Software/hooks/D3D9FrameGrabber.cpp @@ -1,7 +1,8 @@ #include "D3D9FrameGrabber.hpp" #include "IPCContext.hpp" -#include "msvcstub.h" +#include "hooksutils.h" +#include "../common/msvcstub.h" #include #include "d3d9types.h" #include "d3d9.h" @@ -64,7 +65,7 @@ void D3D9FrameGrabber::free() { void ** D3D9FrameGrabber::calcD3d9PresentPointer() { void * hD3d9 = reinterpret_cast(GetModuleHandleA("d3d9.dll")); m_logger->reportLogDebug(L"m_ipcContext->m_memDesc.d3d9PresentFuncOffset = 0x%x, hDxgi = 0x%x", m_ipcContext->m_memDesc.d3d9PresentFuncOffset, hD3d9); - void ** result = static_cast(hD3d9 + m_ipcContext->m_memDesc.d3d9PresentFuncOffset); + void ** result = static_cast(incPtr(hD3d9 , m_ipcContext->m_memDesc.d3d9PresentFuncOffset)); m_logger->reportLogDebug(L"d3d9.dll = 0x%x, swapchain::present location = 0x%x", hD3d9, result); return result; } @@ -72,7 +73,7 @@ void ** D3D9FrameGrabber::calcD3d9PresentPointer() { void ** D3D9FrameGrabber::calcD3d9SCPresentPointer() { void * hD3d9 = reinterpret_cast(GetModuleHandleA("d3d9.dll")); m_logger->reportLogDebug(L"m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset = 0x%x, hDxgi = 0x%x", m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset, hD3d9); - void ** result = static_cast(hD3d9 + m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset); + void ** result = static_cast(incPtr(hD3d9, m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset)); m_logger->reportLogDebug(L"d3d9.dll = 0x%x, swapchain::present location = 0x%x", hD3d9, result); return result; } @@ -86,7 +87,7 @@ HRESULT WINAPI D3D9Present(IDirect3DDevice9 *pDev, CONST RECT* pSourceRect,CONST logger->reportLogDebug(L"D3D9Present"); HRESULT hRes; - RECT newRect = {0}; + RECT newRect = RECT(); IDirect3DSurface9 *pBackBuffer = NULL; IDirect3DSurface9 *pDemultisampledSurf = NULL; IDirect3DSurface9 *pOffscreenSurf = NULL; @@ -165,13 +166,13 @@ HRESULT WINAPI D3D9Present(IDirect3DDevice9 *pDev, CONST RECT* pSourceRect,CONST // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d9 writing description to mem mapped file"); memcpy(ipcContext->m_pMemMap, &(ipcContext->m_memDesc), sizeof (ipcContext->m_memDesc)); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d9 writing data to mem mapped file"); - PVOID pMemDataMap = ipcContext->m_pMemMap + sizeof (ipcContext->m_memDesc); - if (lockedSrcRect.Pitch == surfDesc.Width * 4) { + PVOID pMemDataMap = incPtr(ipcContext->m_pMemMap, sizeof (ipcContext->m_memDesc)); + if (static_cast(lockedSrcRect.Pitch) == surfDesc.Width * 4) { memcpy(pMemDataMap, lockedSrcRect.pBits, surfDesc.Width * surfDesc.Height * 4); } else { UINT i = 0, cleanOffset = 0, pitchOffset = 0; while (i < surfDesc.Height) { - memcpy(pMemDataMap + cleanOffset, lockedSrcRect.pBits + pitchOffset, surfDesc.Width * 4); + memcpy(incPtr(pMemDataMap, cleanOffset), incPtr(lockedSrcRect.pBits, pitchOffset), surfDesc.Width * 4); cleanOffset += surfDesc.Width * 4; pitchOffset += lockedSrcRect.Pitch; i++; @@ -218,7 +219,7 @@ HRESULT WINAPI D3D9SCPresent(IDirect3DSwapChain9 *pSc, CONST RECT* pSourceRect,C logger->reportLogDebug(L"D3D9SCPresent"); IDirect3DSurface9 *pBackBuffer = NULL; D3DPRESENT_PARAMETERS params; - RECT newRect = {0}; + RECT newRect = RECT(); IDirect3DSurface9 *pDemultisampledSurf = NULL; IDirect3DSurface9 *pOffscreenSurf = NULL; IDirect3DDevice9 *pDev = NULL; @@ -298,13 +299,13 @@ HRESULT WINAPI D3D9SCPresent(IDirect3DSwapChain9 *pSc, CONST RECT* pSourceRect,C // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d9sc writing description to mem mapped file"); memcpy(ipcContext->m_pMemMap, &ipcContext->m_memDesc, sizeof (ipcContext->m_memDesc)); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d9sc writing data to mem mapped file"); - PVOID pMemDataMap = ipcContext->m_pMemMap + sizeof (ipcContext->m_memDesc); - if (lockedSrcRect.Pitch == surfDesc.Width * 4) { + PVOID pMemDataMap = incPtr(ipcContext->m_pMemMap, sizeof (ipcContext->m_memDesc)); + if (static_cast(lockedSrcRect.Pitch) == surfDesc.Width * 4) { memcpy(pMemDataMap, lockedSrcRect.pBits, surfDesc.Width * surfDesc.Height * 4); } else { UINT i = 0, cleanOffset = 0, pitchOffset = 0; while (i < surfDesc.Height) { - memcpy(pMemDataMap + cleanOffset, lockedSrcRect.pBits + pitchOffset, surfDesc.Width * 4); + memcpy(incPtr(pMemDataMap, cleanOffset), incPtr(lockedSrcRect.pBits, pitchOffset), surfDesc.Width * 4); cleanOffset += surfDesc.Width * 4; pitchOffset += lockedSrcRect.Pitch; i++; diff --git a/Software/hooks/DxgiFrameGrabber.cpp b/Software/hooks/DxgiFrameGrabber.cpp index 726dd887..0d63c47d 100644 --- a/Software/hooks/DxgiFrameGrabber.cpp +++ b/Software/hooks/DxgiFrameGrabber.cpp @@ -1,10 +1,10 @@ #include "DxgiFrameGrabber.hpp" -#include "msvcstub.h" +#include "hooksutils.h" +#include "../common/msvcstub.h" #include -#include "DXGI.h" #include "D3D11.h" -#include "D3DX11tex.h" +#include "DXGI.h" #include "D3D10.h" #include "D3DX10tex.h" @@ -21,7 +21,7 @@ enum DxgiDevice { DxgiDevice dxgiDevice = DxgiDeviceUnknown; -typedef HRESULT WINAPI (*DXGISCPresentFunc)(IDXGISwapChain *, UINT, UINT); +typedef HRESULT (WINAPI *DXGISCPresentFunc)(IDXGISwapChain *, UINT, UINT); HRESULT WINAPI DXGIPresent(IDXGISwapChain * sc, UINT b, UINT c); @@ -68,7 +68,7 @@ void ** DxgiFrameGrabber::calcDxgiPresentPointer() { void * hDxgi = reinterpret_cast(GetModuleHandleA("dxgi.dll")); if (m_logger) m_logger->reportLogDebug(L"gIpcContext.m_memDesc.dxgiPresentFuncOffset = 0x%x, hDxgi = 0x%x", m_ipcContext->m_memDesc.dxgiPresentFuncOffset, hDxgi); - void ** result = static_cast(hDxgi + m_ipcContext->m_memDesc.dxgiPresentFuncOffset); + void ** result = static_cast(incPtr(hDxgi, m_ipcContext->m_memDesc.dxgiPresentFuncOffset)); if (m_logger) m_logger->reportLogDebug(L"dxgi.dll = 0x%x, swapchain::present location = 0x%x", hDxgi, result); return result; @@ -89,7 +89,8 @@ void D3D10Grab(ID3D10Texture2D* pBackBuffer) { tex_desc.ArraySize = 1; tex_desc.MipLevels = 1; tex_desc.BindFlags = 0; - tex_desc.SampleDesc = (DXGI_SAMPLE_DESC){1, 0}; + tex_desc.SampleDesc.Count = 1; + tex_desc.SampleDesc.Quality = 0; tex_desc.Usage = D3D10_USAGE_STAGING; tex_desc.MiscFlags = 0; @@ -124,13 +125,13 @@ void D3D10Grab(ID3D10Texture2D* pBackBuffer) { // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d10 writing description to mem mapped file"); memcpy(ipcContext->m_pMemMap, &ipcContext->m_memDesc, sizeof (ipcContext->m_memDesc)); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d10 writing data to mem mapped file"); - PVOID pMemDataMap = ipcContext->m_pMemMap + sizeof (ipcContext->m_memDesc); + PVOID pMemDataMap = incPtr(ipcContext->m_pMemMap, sizeof (ipcContext->m_memDesc)); if (mappedTexture.RowPitch == tex_desc.Width * 4) { memcpy(pMemDataMap, mappedTexture.pData, tex_desc.Width * tex_desc.Height * 4); } else { UINT cleanOffset = 0, pitchOffset = 0, i = 0; while (i < tex_desc.Height) { - memcpy(pMemDataMap + cleanOffset, mappedTexture.pData + pitchOffset, tex_desc.Width * 4); + memcpy(incPtr(pMemDataMap, cleanOffset), incPtr(mappedTexture.pData, pitchOffset), tex_desc.Width * 4); cleanOffset += tex_desc.Width * 4; pitchOffset += mappedTexture.RowPitch; i++; @@ -165,7 +166,8 @@ void D3D11Grab(ID3D11Texture2D *pBackBuffer) { tex_desc.ArraySize = 1; tex_desc.MipLevels = 1; tex_desc.BindFlags = 0; - tex_desc.SampleDesc = (DXGI_SAMPLE_DESC){1, 0}; + tex_desc.SampleDesc.Count = 1; + tex_desc.SampleDesc.Quality = 0; tex_desc.Usage = D3D11_USAGE_STAGING; tex_desc.MiscFlags = 0; @@ -199,13 +201,13 @@ void D3D11Grab(ID3D11Texture2D *pBackBuffer) { // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d11 writing description to mem mapped file"); memcpy(ipcContext->m_pMemMap, &ipcContext->m_memDesc, sizeof (ipcContext->m_memDesc)); // reportLog(EVENTLOG_INFORMATION_TYPE, L"d3d11 writing data to mem mapped file"); - PVOID pMemDataMap = ipcContext->m_pMemMap + sizeof (ipcContext->m_memDesc); + PVOID pMemDataMap = incPtr(ipcContext->m_pMemMap, sizeof (ipcContext->m_memDesc)); if (mappedTexture.RowPitch == tex_desc.Width * 4) { memcpy(pMemDataMap, mappedTexture.pData, tex_desc.Width * tex_desc.Height * 4); } else { UINT i = 0, cleanOffset = 0, pitchOffset = 0; while (i < tex_desc.Height) { - memcpy(pMemDataMap + cleanOffset, mappedTexture.pData + pitchOffset, tex_desc.Width * 4); + memcpy(incPtr(pMemDataMap, cleanOffset), incPtr(mappedTexture.pData, pitchOffset), tex_desc.Width * 4); cleanOffset += tex_desc.Width * 4; pitchOffset += mappedTexture.RowPitch; i++; diff --git a/Software/hooks/GAPIProxyFrameGrabber.cpp b/Software/hooks/GAPIProxyFrameGrabber.cpp index e7adaa58..b76451db 100644 --- a/Software/hooks/GAPIProxyFrameGrabber.cpp +++ b/Software/hooks/GAPIProxyFrameGrabber.cpp @@ -4,7 +4,7 @@ GAPIProxyFrameGrabber::GAPIProxyFrameGrabber(HANDLE syncRunMutex) { LARGE_INTEGER liFreq; - QueryPerformanceFrequency(&m_liFreq); + QueryPerformanceFrequency(&liFreq); m_liFreq.QuadPart = liFreq.QuadPart / 1000; m_pcOverall.QuadPart = 0; m_pcOriginal.QuadPart = 0; @@ -18,11 +18,11 @@ GAPIProxyFrameGrabber::~GAPIProxyFrameGrabber() { } bool GAPIProxyFrameGrabber::startOverallPerfCount() { - return QueryPerformanceCounter(&m_pcStart); + return !!QueryPerformanceCounter(&m_pcStart); } bool GAPIProxyFrameGrabber::startOriginalPerfCount() { - return QueryPerformanceCounter(&m_pcIntermediate); + return !!QueryPerformanceCounter(&m_pcIntermediate); } bool GAPIProxyFrameGrabber::stopPerfCount() { diff --git a/Software/hooks/Logger.cpp b/Software/hooks/Logger.cpp index 324c0eb4..d69e267b 100644 --- a/Software/hooks/Logger.cpp +++ b/Software/hooks/Logger.cpp @@ -17,7 +17,7 @@ Logger::~Logger() { this->closeLog(); } -void Logger::initLog(LPWSTR name, DWORD logLevel) { +void Logger::initLog(LPCWSTR name, DWORD logLevel) { if (!m_hEventSrc) m_hEventSrc = RegisterEventSourceW(NULL, name); if (!m_reportLogBuf) m_reportLogBuf = (WCHAR*)malloc(REPORT_LOG_BUF_SIZE); m_logLevel = logLevel; @@ -27,7 +27,7 @@ void Logger::setLogLevel(DWORD logLevel) { m_logLevel = logLevel; } -void Logger::reportLogDebug(const LPWSTR message, ...) { +void Logger::reportLogDebug(LPCWSTR message, ...) { va_list ap; va_start( ap, message ); if (m_logLevel <= PRISMATIK_LOG_SEVERITY_DEBUG) { @@ -36,7 +36,7 @@ void Logger::reportLogDebug(const LPWSTR message, ...) { va_end( ap ); } -void Logger::reportLogInfo(const LPWSTR message, ...) { +void Logger::reportLogInfo(LPCWSTR message, ...) { va_list ap; va_start( ap, message ); if (m_logLevel <= PRISMATIK_LOG_SEVERITY_INFO) { @@ -45,7 +45,7 @@ void Logger::reportLogInfo(const LPWSTR message, ...) { va_end( ap ); } -void Logger::reportLogWarning(const LPWSTR message, ...) { +void Logger::reportLogWarning(LPCWSTR message, ...) { va_list ap; va_start( ap, message ); if (m_logLevel <= PRISMATIK_LOG_SEVERITY_WARNING) { @@ -54,19 +54,19 @@ void Logger::reportLogWarning(const LPWSTR message, ...) { va_end( ap ); } -void Logger::reportLogError(const LPWSTR message, ...) { +void Logger::reportLogError(LPCWSTR message, ...) { va_list ap; va_start( ap, message ); this->reportLog(EVENTLOG_ERROR_TYPE, PRISMATIK_LOG_MSG_ERROR, message, ap); va_end( ap ); } -void Logger::reportLog(DWORD type, DWORD msgId, const LPWSTR message, va_list ap) { +void Logger::reportLog(DWORD type, DWORD msgId, LPCWSTR message, va_list ap) { if (m_hEventSrc && m_reportLogBuf) { - ZeroMemory(m_reportLogBuf, sizeof(m_reportLogBuf)); + ZeroMemory(m_reportLogBuf, REPORT_LOG_BUF_SIZE); int sprintfResult = wvsprintfW(m_reportLogBuf, message, ap); if (sprintfResult > -1) - ReportEventW(m_hEventSrc, type, 0, msgId, NULL, 1, 0, const_cast(&m_reportLogBuf), NULL); + ReportEventW(m_hEventSrc, type & 0xFFFF, 0, msgId, NULL, 1, 0, const_cast(&m_reportLogBuf), NULL); } } diff --git a/Software/hooks/Logger.hpp b/Software/hooks/Logger.hpp index 456276ba..4d9007fd 100644 --- a/Software/hooks/Logger.hpp +++ b/Software/hooks/Logger.hpp @@ -12,20 +12,19 @@ class Logger static Logger *getInstance(); ~Logger(); - void initLog(LPWSTR name, DWORD logLevel); + void initLog(LPCWSTR name, DWORD logLevel); void closeLog(); void setLogLevel(DWORD logLevel); - void reportLogDebug(const LPWSTR message, ...); - void reportLogInfo(const LPWSTR message, ...); - void reportLogWarning(const LPWSTR message, ...); - void reportLogError(const LPWSTR message, ...); + void reportLogDebug(LPCWSTR message, ...); + void reportLogInfo(LPCWSTR message, ...); + void reportLogWarning(LPCWSTR message, ...); + void reportLogError(LPCWSTR message, ...); protected: Logger(); private: - - void reportLog(DWORD type, DWORD msgId, const LPWSTR message, va_list ap); + void reportLog(DWORD type, DWORD msgId, LPCWSTR message, va_list ap); HANDLE m_hEventSrc; unsigned int m_logLevel; diff --git a/Software/hooks/ProxyFuncJmp.hpp b/Software/hooks/ProxyFuncJmp.hpp index 9cb7aac8..4271b89e 100644 --- a/Software/hooks/ProxyFuncJmp.hpp +++ b/Software/hooks/ProxyFuncJmp.hpp @@ -16,8 +16,8 @@ class ProxyFuncJmp : public ProxyFunc private: static const int kJmpInstructionSize = 6; - char m_pOriginalCode[kJmpInstructionSize]; - char m_pJmpCode[kJmpInstructionSize]; + unsigned char m_pOriginalCode[kJmpInstructionSize]; + unsigned char m_pJmpCode[kJmpInstructionSize]; }; #endif // PROXYFUNCJMP_H diff --git a/Software/hooks/hooks.cpp b/Software/hooks/hooks.cpp index 6f00918d..20046446 100644 --- a/Software/hooks/hooks.cpp +++ b/Software/hooks/hooks.cpp @@ -48,6 +48,8 @@ WCHAR *getEventSourceName(char *executableName) { } HOOKSDLL_API BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID lpReserved) { + UNREFERENCED_PARAMETER(lpReserved); + if (fdwReason == DLL_PROCESS_ATTACH /*|| fdwReason == DLL_THREAD_ATTACH*/) // When initializing.... { // __asm__("int $3"); diff --git a/Software/hooks/hooks.h b/Software/hooks/hooks.h index 2eaca66a..cb50deef 100644 --- a/Software/hooks/hooks.h +++ b/Software/hooks/hooks.h @@ -7,6 +7,14 @@ #ifndef HOOKSDLL_H #define HOOKSDLL_H + +#if !defined NOMINMAX +#define NOMINMAX +#endif + +#if !defined WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif #include"windows.h" #ifdef HOOKSDLL_EXPORTS diff --git a/Software/hooks/hooks.pro b/Software/hooks/hooks.pro index 1283a472..6cd93eb7 100644 --- a/Software/hooks/hooks.pro +++ b/Software/hooks/hooks.pro @@ -12,22 +12,28 @@ TEMPLATE = lib include(../build-config.prf) -INCLUDEPATH += "$${DIRECTX_SDK_DIR}/Include" - # ../zeromq/include +# This will suppress gcc warnings in DX headers. +CONFIG(gcc) { + QMAKE_CXXFLAGS += -isystem "$${DIRECTX_SDK_DIR}/Include" +} else { + INCLUDEPATH += "$${DIRECTX_SDK_DIR}/Include" +} - -QMAKE_CXXFLAGS = -std=c++11 -LIBS += -lwsock32 -lshlwapi -ladvapi32 -L"$${DIRECTX_SDK_DIR}/Lib/x86" -ldxguid #-LD:/System/Users/Tim/Projects/Lightpack/Software/zeromq -lzmq.dll -QMAKE_LFLAGS += -static -QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas +LIBS += -lshlwapi -ladvapi32 -luser32 -L"$${DIRECTX_SDK_DIR}/Lib/x86" -ldxguid #-LD:/System/Users/Tim/Projects/Lightpack/Software/zeromq -lzmq.dll +#QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas QMAKE_LFLAGS_EXCEPTIONS_ON -= -mthreads QMAKE_CXXFLAGS_EXCEPTIONS_ON -= -mthreads CONFIG -= rtti -DEFINES += HOOKSDLL_EXPORTS \ - UNICODE +DEFINES += HOOKSDLL_EXPORTS UNICODE +CONFIG(msvc) { + DEFINES += _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE +} else { + QMAKE_CXXFLAGS += -std=c++11 + QMAKE_LFLAGS += -static +} SOURCES += \ hooks.cpp \ @@ -44,6 +50,7 @@ HEADERS += \ hooks.h \ ../common/D3D10GrabberDefs.hpp \ ../common/defs.h \ + ../common/msvcstub.h \ ProxyFunc.hpp \ ProxyFuncJmp.hpp \ hooksutils.h \ @@ -56,5 +63,4 @@ HEADERS += \ Logger.hpp \ LoggableTrait.hpp \ ../common/BufferFormat.h \ - msvcstub.h \ D3D9FrameGrabber.hpp diff --git a/Software/hooks/hooksutils.h b/Software/hooks/hooksutils.h index 0501ec6a..038c2787 100644 --- a/Software/hooks/hooksutils.h +++ b/Software/hooks/hooksutils.h @@ -1,6 +1,9 @@ #ifndef HOOKSUTILS_H #define HOOKSUTILS_H -#include + +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include //#include //void InitLog(const LPWSTR name, DWORD logLevel); @@ -13,4 +16,8 @@ bool WriteToProtectedMem(void * mem, void * newVal, void * savedVal, size_t size); +inline void * incPtr(void * ptr, UINT offset) { + return (void *)( (DWORD_PTR)ptr + offset ); +} + #endif // HOOKSUTILS_H