Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hooks project #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions Software/hooks/D3D9FrameGrabber.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "D3D9FrameGrabber.hpp"
#include "IPCContext.hpp"

#include "msvcstub.h"
#include "hooksutils.h"
#include "../common/msvcstub.h"
#include <initguid.h>
#include "d3d9types.h"
#include "d3d9.h"
Expand Down Expand Up @@ -64,15 +65,15 @@ void D3D9FrameGrabber::free() {
void ** D3D9FrameGrabber::calcD3d9PresentPointer() {
void * hD3d9 = reinterpret_cast<void *>(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<void ** >(hD3d9 + m_ipcContext->m_memDesc.d3d9PresentFuncOffset);
void ** result = static_cast<void ** >(incPtr(hD3d9 , m_ipcContext->m_memDesc.d3d9PresentFuncOffset));
m_logger->reportLogDebug(L"d3d9.dll = 0x%x, swapchain::present location = 0x%x", hD3d9, result);
return result;
}

void ** D3D9FrameGrabber::calcD3d9SCPresentPointer() {
void * hD3d9 = reinterpret_cast<void *>(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<void ** >(hD3d9 + m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset);
void ** result = static_cast<void ** >(incPtr(hD3d9, m_ipcContext->m_memDesc.d3d9SCPresentFuncOffset));
m_logger->reportLogDebug(L"d3d9.dll = 0x%x, swapchain::present location = 0x%x", hD3d9, result);
return result;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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<UINT>(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++;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<UINT>(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++;
Expand Down
24 changes: 13 additions & 11 deletions Software/hooks/DxgiFrameGrabber.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "DxgiFrameGrabber.hpp"

#include "msvcstub.h"
#include "hooksutils.h"
#include "../common/msvcstub.h"
#include <initguid.h>
#include "DXGI.h"
#include "D3D11.h"
#include "D3DX11tex.h"
#include "DXGI.h"
#include "D3D10.h"
#include "D3DX10tex.h"

Expand All @@ -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);

Expand Down Expand Up @@ -68,7 +68,7 @@ void ** DxgiFrameGrabber::calcDxgiPresentPointer() {
void * hDxgi = reinterpret_cast<void *>(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<void ** >(hDxgi + m_ipcContext->m_memDesc.dxgiPresentFuncOffset);
void ** result = static_cast<void ** >(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;
Expand All @@ -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;

Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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++;
Expand Down
6 changes: 3 additions & 3 deletions Software/hooks/GAPIProxyFrameGrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

GAPIProxyFrameGrabber::GAPIProxyFrameGrabber(HANDLE syncRunMutex) {
LARGE_INTEGER liFreq;
QueryPerformanceFrequency(&m_liFreq);
QueryPerformanceFrequency(&liFreq);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bug I think. In the next line we will overwrite m_liFreq value by uninitialized value of liFreq.

m_liFreq.QuadPart = liFreq.QuadPart / 1000;
m_pcOverall.QuadPart = 0;
m_pcOriginal.QuadPart = 0;
Expand All @@ -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() {
Expand Down
16 changes: 8 additions & 8 deletions Software/hooks/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,7 @@ void Logger::setLogLevel(DWORD logLevel) {
m_logLevel = logLevel;
}

void Logger::reportLogDebug(const LPWSTR message, ...) {
void Logger::reportLogDebug(LPCWSTR message, ...) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a minor mistake.
const LPWSTR == const (wchar *) but not (const wchar) *

va_list ap;
va_start( ap, message );
if (m_logLevel <= PRISMATIK_LOG_SEVERITY_DEBUG) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here only sizeof void* bytes will be set to zero instead of REPORTLOGBUFSIZE.
It is a good place to use a qvector or std::vector instead.

int sprintfResult = wvsprintfW(m_reportLogBuf, message, ap);
if (sprintfResult > -1)
ReportEventW(m_hEventSrc, type, 0, msgId, NULL, 1, 0, const_cast<const WCHAR **>(&m_reportLogBuf), NULL);
ReportEventW(m_hEventSrc, type & 0xFFFF, 0, msgId, NULL, 1, 0, const_cast<const WCHAR **>(&m_reportLogBuf), NULL);
}
}

Expand Down
13 changes: 6 additions & 7 deletions Software/hooks/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Software/hooks/ProxyFuncJmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions Software/hooks/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 8 additions & 0 deletions Software/hooks/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions Software/hooks/hooks.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -44,6 +50,7 @@ HEADERS += \
hooks.h \
../common/D3D10GrabberDefs.hpp \
../common/defs.h \
../common/msvcstub.h \
ProxyFunc.hpp \
ProxyFuncJmp.hpp \
hooksutils.h \
Expand All @@ -56,5 +63,4 @@ HEADERS += \
Logger.hpp \
LoggableTrait.hpp \
../common/BufferFormat.h \
msvcstub.h \
D3D9FrameGrabber.hpp
9 changes: 8 additions & 1 deletion Software/hooks/hooksutils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef HOOKSUTILS_H
#define HOOKSUTILS_H
#include<windows.h>

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//#include <stdarg.h>

//void InitLog(const LPWSTR name, DWORD logLevel);
Expand All @@ -13,4 +16,8 @@

bool WriteToProtectedMem(void * mem, void * newVal, void * savedVal, size_t size);

inline void * incPtr(void * ptr, UINT offset) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All compilers will give a warning when adding integer to a void *.

return (void *)( (DWORD_PTR)ptr + offset );
}

#endif // HOOKSUTILS_H