Skip to content

Commit

Permalink
Create wrapper stub and wrap more dlls
Browse files Browse the repository at this point in the history
- Created a new wrapper stub to allow loading dxwrapper.dll from memory
- Added MemoryModule to load DxWrapper from memory using the stub
- Added support for wrapping additional dlls: d2d1.dll, d3d10.dll,
d3d10core.dll, d3d11.dll, d3d12.dll, d3dim.dll, d3dim700.dll,
dinput8.dll, msacm32.dll, msvfw32.dll, vorbisfile.dll, winmmbase.dll and
xlive.dll
- Updated to support getting loaded from memory rather than disk
- Fixed issue where memory verification would fail sometimes after
compiling
- Added macros for config settings to simplify the code
- Updated the names of the Config varables to match thier ini file names
- Removed wrapper mode from config file
- Updated wrapper functions to use original API names rather than fake
names
  • Loading branch information
elishacloud committed Oct 2, 2017
1 parent 4b951bf commit 7a3ed4e
Show file tree
Hide file tree
Showing 74 changed files with 5,027 additions and 2,402 deletions.
14 changes: 4 additions & 10 deletions D3d8to9/d3d8.h
@@ -1,18 +1,12 @@
#pragma once

#include "wrappers\wrapper.h"

class __declspec(uuid("1DD9E8DA-1C77-4D40-B0CF-98FEFDFF9512")) Direct3D8;

extern "C" Direct3D8 *WINAPI Direct3DCreate8(UINT);
extern "C" Direct3D8 *WINAPI _Direct3DCreate8(UINT);
HRESULT WINAPI _ValidateVertexShader(DWORD* vertexshader, DWORD* reserved1, DWORD* reserved2, BOOL flag, DWORD* toto);
HRESULT WINAPI _ValidatePixelShader(DWORD* pixelshader, DWORD* reserved1, BOOL flag, DWORD* toto);

namespace Wrapper
namespace D3d8to9
{
namespace D3d8to9
{
FARPROC _Direct3DCreate8 = (FARPROC)*Direct3DCreate8;
FARPROC _Direct3DCreate9 = nullptr;
}
}
FARPROC Direct3DCreate8 = (FARPROC)*_Direct3DCreate8;
}
8 changes: 5 additions & 3 deletions D3d8to9/d3d8to9.cpp
Expand Up @@ -7,18 +7,20 @@

//********** Begin Edit *************
#include "d3d8to9.hpp"
#include "wrappers\wrapper.h"
#include "Utils\Utils.h"
#include "Hooking\Hook.h"
typedef LPDIRECT3D9(WINAPI *PFN_Direct3DCreate9)(UINT SDKVersion);
//********** End Edit ***************


extern "C" Direct3D8 *WINAPI Direct3DCreate8(UINT SDKVersion)
extern "C" Direct3D8 *WINAPI _Direct3DCreate8(UINT SDKVersion)
{
//********** Begin Edit *************
LOG << "Enabling D3d8to9 function (" << SDKVersion << ")";

// Declare Direct3DCreate9
static PFN_Direct3DCreate9 Direct3DCreate9 = reinterpret_cast<PFN_Direct3DCreate9>(Wrapper::D3d8to9::_Direct3DCreate9);
HMODULE dll = Utils::LoadLibrary(dtypename[dtype.d3d9]);
static PFN_Direct3DCreate9 Direct3DCreate9 = reinterpret_cast<PFN_Direct3DCreate9>(Hook::GetProcAddress(dll, "Direct3DCreate9"));
if (!Direct3DCreate9)
{
LOG << "Failed to get 'Direct3DCreate9' ProcAddress of d3d9.dll!";
Expand Down
11 changes: 11 additions & 0 deletions D3d9/D3d9External.h
@@ -0,0 +1,11 @@
#pragma once

struct __declspec(uuid("81BDCBCA-64D4-426d-AE8D-AD0147F4275C")) IDirect3D9;

IDirect3D9 *WINAPI _Direct3DCreate9(UINT);

namespace d3d9_wrap
{
constexpr FARPROC Direct3DCreate9 = (FARPROC)*_Direct3DCreate9;
extern FARPROC Direct3DCreate9_Proxy;
}
18 changes: 7 additions & 11 deletions D3d9/d3d9.cpp
Expand Up @@ -17,25 +17,21 @@
*/

#include "Settings\Settings.h"
#include "Wrappers\Wrapper.h"
#include "d3d9.h"
#include "d3d9External.h"
#include "d3dx9.h"
#include "Logging\Logging.h"

IDirect3D9 *WINAPI _Direct3DCreate9_Wrapper(UINT SDKVersion)
namespace d3d9_wrap
{
Logging::Log() << "Direct3DCreate9 function (" << SDKVersion << ")";
orig_Direct3DCreate9 = (D3DC9)Wrapper::d3d9_Wrapper::_Direct3DCreate9_RealProc;
return new f_iD3D9(orig_Direct3DCreate9(SDKVersion));
FARPROC Direct3DCreate9_Proxy = nullptr;
}

namespace Wrapper
IDirect3D9 *WINAPI _Direct3DCreate9(UINT SDKVersion)
{
namespace d3d9_Wrapper
{
FARPROC _Direct3DCreate9_RealProc;
FARPROC _Direct3DCreate9_WrapperProc = (FARPROC)*_Direct3DCreate9_Wrapper;
}
Logging::Log() << "Direct3DCreate9 function (" << SDKVersion << ")";
orig_Direct3DCreate9 = (D3DC9)d3d9_wrap::Direct3DCreate9_Proxy;
return new f_iD3D9(orig_Direct3DCreate9(SDKVersion));
}

HRESULT f_iD3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType,
Expand Down
20 changes: 7 additions & 13 deletions DDrawCompat/DDrawCompatExternal.h
@@ -1,20 +1,14 @@
#pragma once

#include "Wrappers\Wrapper.h"

BOOL WINAPI DllMain_DDrawCompat(HINSTANCE, DWORD, LPVOID);

extern "C" HRESULT WINAPI DirectDrawCreate(GUID*, LPDIRECTDRAW*, IUnknown*);
extern "C" HRESULT WINAPI DirectDrawCreateEx(GUID*, LPVOID*, REFIID, IUnknown*);
extern "C" HRESULT WINAPI DllGetClassObject(REFCLSID, REFIID, LPVOID*);
extern "C" HRESULT WINAPI _DirectDrawCreate(GUID*, LPDIRECTDRAW*, IUnknown*);
extern "C" HRESULT WINAPI _DirectDrawCreateEx(GUID*, LPVOID*, REFIID, IUnknown*);
extern "C" HRESULT WINAPI _DllGetClassObject(REFCLSID, REFIID, LPVOID*);

namespace Wrapper
namespace DDrawCompat
{
namespace DDrawCompat
{
FARPROC _DirectDrawCreate = (FARPROC)*DirectDrawCreate;
FARPROC _DirectDrawCreateEx = (FARPROC)*DirectDrawCreateEx;
FARPROC _DllGetClassObject = (FARPROC)*DllGetClassObject;
}

constexpr FARPROC DirectDrawCreate = (FARPROC)*_DirectDrawCreate;
constexpr FARPROC DirectDrawCreateEx = (FARPROC)*_DirectDrawCreateEx;
constexpr FARPROC DllGetClassObject = (FARPROC)*_DllGetClassObject;
}
24 changes: 16 additions & 8 deletions DDrawCompat/v0.2.0b/DDrawLog.h
Expand Up @@ -108,20 +108,28 @@ namespace Compat
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)Init, &hModule);
GetModuleFileName(hModule, wrappername, MAX_PATH);

// Set lower case, remove extension and add dash (-)
for (int z = 0; z < MAX_PATH && wrappername[z] != '\0'; z++) { wrappername[z] = (char)tolower(wrappername[z]); }
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), "-");

// Get process name
char exepath[MAX_PATH];
GetModuleFileName(NULL, exepath, MAX_PATH);
char processname[MAX_PATH];
GetModuleFileName(nullptr, processname, MAX_PATH);

// Check if module name is the same as process name
if (_strcmpi(strrchr(wrappername, '\\') + 1, strrchr(processname, '\\') + 1) == 0)
{
strcpy_s(strrchr(wrappername, '\\') + 1, MAX_PATH - strlen(wrappername), "dxwrapper.dll");
}

// Remove path and add process name
strcat_s(wrappername, MAX_PATH, strrchr(exepath, '\\') + 1);
// Remove extension and add dash (-)
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), "-");

// Add process name
strcat_s(wrappername, MAX_PATH, strrchr(processname, '\\') + 1);

// Change extension to .log
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), ".log");

// Set lower case
for (int z = 0; z < MAX_PATH && wrappername[z] != '\0'; z++) { wrappername[z] = (char)tolower(wrappername[z]); }

return wrappername;
}
//********** End Edit ***************
Expand Down
27 changes: 15 additions & 12 deletions DDrawCompat/v0.2.0b/DllMainCompat.cpp
Expand Up @@ -16,7 +16,10 @@
#include "DDrawProcs.h"
//********** Begin Edit *************
#include "Settings\Settings.h"
#include "Wrappers\wrapper.h"
#include "Hooking\Hook.h"
#include "Utils\Utils.h"
#define DllMain DllMain_DDrawCompat
#define GetProcAddress Hook::GetProcAddress
//********** End Edit ***************

struct IDirectInput;
Expand Down Expand Up @@ -152,10 +155,10 @@ namespace
#define LOAD_ORIGINAL_DDRAW_PROC(procName) \
Compat::origProcs.procName = GetProcAddress(g_origDDrawModule, #procName);

//********** Begin Edit *************
BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lpvReserved*/)
//********** End Edit ***************
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
UNREFERENCED_PARAMETER(lpvReserved);

if (fdwReason == DLL_PROCESS_ATTACH)
{
char currentDllPath[MAX_PATH] = {};
Expand All @@ -175,13 +178,13 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
//********** Begin Edit *************
if (Config.RealWrapperMode == dtype.ddraw)
{
g_origDDrawModule = Wrapper::LoadDll(dtype.ddraw);
g_origDDrawModule = Utils::LoadLibrary("ddraw.dll");
}
else
{
g_origDDrawModule = hinstDLL;
}
g_origDInputModule = Wrapper::LoadDll(dtype.dinput);
g_origDInputModule = Utils::LoadLibrary("dinput.dll");
if (!g_origDDrawModule || !g_origDInputModule)
{
return false;
Expand All @@ -192,7 +195,7 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
Compat::origProcs.DirectInputCreateA = GetProcAddress(g_origDInputModule, "DirectInputCreateA");

//********** Begin Edit *************
if (Config.AffinityNotSet) SetProcessAffinityMask(GetCurrentProcess(), 1);
if (Config.SingleProcAffinityNotSet) SetProcessAffinityMask(GetCurrentProcess(), 1);
//********** End Edit ***************

//********** Begin Edit *************
Expand Down Expand Up @@ -221,7 +224,7 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
return TRUE;
}

extern "C" HRESULT WINAPI DirectDrawCreate(
extern "C" HRESULT WINAPI _DirectDrawCreate(
GUID* lpGUID,
LPDIRECTDRAW* lplpDD,
IUnknown* pUnkOuter)
Expand All @@ -234,7 +237,7 @@ extern "C" HRESULT WINAPI DirectDrawCreate(
return result;
}

extern "C" HRESULT WINAPI DirectDrawCreateEx(
extern "C" HRESULT WINAPI _DirectDrawCreateEx(
GUID* lpGUID,
LPVOID* lplpDD,
REFIID iid,
Expand All @@ -249,7 +252,7 @@ extern "C" HRESULT WINAPI DirectDrawCreateEx(
}

//********** Begin Edit *************
extern "C" HRESULT WINAPI DllGetClassObject(
extern "C" HRESULT WINAPI _DllGetClassObject(
REFCLSID rclsid,
REFIID riid,
LPVOID *ppv)
Expand All @@ -262,7 +265,7 @@ extern "C" HRESULT WINAPI DllGetClassObject(
}
//********** End Edit ***************

extern "C" HRESULT WINAPI DirectInputCreateA(
/*extern "C" HRESULT WINAPI DirectInputCreateA(
HINSTANCE hinst,
DWORD dwVersion,
IDirectInput** lplpDirectInput,
Expand All @@ -272,4 +275,4 @@ extern "C" HRESULT WINAPI DirectInputCreateA(
HRESULT result = CALL_ORIG_DDRAW(DirectInputCreateA, hinst, dwVersion, lplpDirectInput, punkOuter);
Compat::LogLeave(__func__, hinst, dwVersion, lplpDirectInput, punkOuter) << result;
return result;
}
}*/
2 changes: 1 addition & 1 deletion DDrawCompat/v0.2.0b/UnmodifiedDDrawProcs.cpp
@@ -1,7 +1,7 @@
#include "DDrawProcs.h"

#define CREATE_DDRAW_PROC_STUB(procName) \
extern "C" __declspec(naked) void __stdcall procName() \
extern "C" __declspec(naked) void __stdcall _ ## procName() \
{ \
__asm jmp Compat::origProcs.procName \
}
Expand Down
24 changes: 16 additions & 8 deletions DDrawCompat/v0.2.1/DDrawLog.h
Expand Up @@ -114,20 +114,28 @@ namespace Compat
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)Init, &hModule);
GetModuleFileName(hModule, wrappername, MAX_PATH);

// Set lower case, remove extension and add dash (-)
for (int z = 0; z < MAX_PATH && wrappername[z] != '\0'; z++) { wrappername[z] = (char)tolower(wrappername[z]); }
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), "-");

// Get process name
char exepath[MAX_PATH];
GetModuleFileName(NULL, exepath, MAX_PATH);
char processname[MAX_PATH];
GetModuleFileName(nullptr, processname, MAX_PATH);

// Check if module name is the same as process name
if (_strcmpi(strrchr(wrappername, '\\') + 1, strrchr(processname, '\\') + 1) == 0)
{
strcpy_s(strrchr(wrappername, '\\') + 1, MAX_PATH - strlen(wrappername), "dxwrapper.dll");
}

// Remove path and add process name
strcat_s(wrappername, MAX_PATH, strrchr(exepath, '\\') + 1);
// Remove extension and add dash (-)
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), "-");

// Add process name
strcat_s(wrappername, MAX_PATH, strrchr(processname, '\\') + 1);

// Change extension to .log
strcpy_s(strrchr(wrappername, '.'), MAX_PATH - strlen(wrappername), ".log");

// Set lower case
for (int z = 0; z < MAX_PATH && wrappername[z] != '\0'; z++) { wrappername[z] = (char)tolower(wrappername[z]); }

return wrappername;
}
//********** End Edit ***************
Expand Down
30 changes: 15 additions & 15 deletions DDrawCompat/v0.2.1/DllMainCompat.cpp
Expand Up @@ -25,7 +25,10 @@
#include "Time1.h"
//********** Begin Edit *************
#include "Settings\Settings.h"
#include "Wrappers\wrapper.h"
#include "Hooking\Hook.h"
#include "Utils\Utils.h"
#define DllMain DllMain_DDrawCompat
#define GetProcAddress Hook::GetProcAddress
//********** End Edit ***************

struct IDirectInput;
Expand Down Expand Up @@ -157,13 +160,10 @@ namespace
#define LOAD_ORIGINAL_DDRAW_PROC(procName) \
Compat::origProcs.procName = GetProcAddress(g_origDDrawModule, #procName);

#define LOAD_ORIGINAL_DDRAW_PROC(procName) \
Compat::origProcs.procName = GetProcAddress(g_origDDrawModule, #procName);

//********** Begin Edit *************
BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lpvReserved*/)
//********** End Edit ***************
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
UNREFERENCED_PARAMETER(lpvReserved);

if (fdwReason == DLL_PROCESS_ATTACH)
{
char currentProcessPath[MAX_PATH] = {};
Expand All @@ -187,13 +187,13 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
//********** Begin Edit *************
if (Config.RealWrapperMode == dtype.ddraw)
{
g_origDDrawModule = Wrapper::LoadDll(dtype.ddraw);
g_origDDrawModule = Utils::LoadLibrary("ddraw.dll");
}
else
{
g_origDDrawModule = hinstDLL;
}
g_origDInputModule = Wrapper::LoadDll(dtype.dinput);
g_origDInputModule = Utils::LoadLibrary("dinput.dll");
if (!g_origDDrawModule || !g_origDInputModule)
{
return false;
Expand All @@ -204,7 +204,7 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
Compat::origProcs.DirectInputCreateA = GetProcAddress(g_origDInputModule, "DirectInputCreateA");

//********** Begin Edit *************
if (Config.AffinityNotSet) SetProcessAffinityMask(GetCurrentProcess(), 1);
if (Config.SingleProcAffinityNotSet) SetProcessAffinityMask(GetCurrentProcess(), 1);
//********** End Edit ***************
SetThemeAppProperties(0);
Time::init();
Expand Down Expand Up @@ -246,7 +246,7 @@ BOOL WINAPI DllMain_DDrawCompat(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID /*lp
return TRUE;
}

extern "C" HRESULT WINAPI DirectDrawCreate(
extern "C" HRESULT WINAPI _DirectDrawCreate(
GUID* lpGUID,
LPDIRECTDRAW* lplpDD,
IUnknown* pUnkOuter)
Expand All @@ -259,7 +259,7 @@ extern "C" HRESULT WINAPI DirectDrawCreate(
return result;
}

extern "C" HRESULT WINAPI DirectDrawCreateEx(
extern "C" HRESULT WINAPI _DirectDrawCreateEx(
GUID* lpGUID,
LPVOID* lplpDD,
REFIID iid,
Expand All @@ -274,7 +274,7 @@ extern "C" HRESULT WINAPI DirectDrawCreateEx(
}

//********** Begin Edit *************
extern "C" HRESULT WINAPI DllGetClassObject(
extern "C" HRESULT WINAPI _DllGetClassObject(
REFCLSID rclsid,
REFIID riid,
LPVOID *ppv)
Expand All @@ -287,7 +287,7 @@ extern "C" HRESULT WINAPI DllGetClassObject(
}
//********** End Edit ***************

extern "C" HRESULT WINAPI DirectInputCreateA(
/*extern "C" HRESULT WINAPI DirectInputCreateA(
HINSTANCE hinst,
DWORD dwVersion,
IDirectInput** lplpDirectInput,
Expand All @@ -297,4 +297,4 @@ extern "C" HRESULT WINAPI DirectInputCreateA(
HRESULT result = CALL_ORIG_DDRAW(DirectInputCreateA, hinst, dwVersion, lplpDirectInput, punkOuter);
Compat::LogLeave(__func__, hinst, dwVersion, lplpDirectInput, punkOuter) << result;
return result;
}
}*/

0 comments on commit 7a3ed4e

Please sign in to comment.