Skip to content

Commit

Permalink
Add Comdlg32 hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Dec 24, 2024
1 parent ee67634 commit 16eaea2
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7405
#define BUILD_NUMBER 7406
133 changes: 133 additions & 0 deletions GDI/Comdlg32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Copyright (C) 2024 Elisha Riedlinger
*
* This software is provided 'as-is', without any express or implied warranty. In no event will the
* authors be held liable for any damages arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
* original software. If you use this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
* being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "ddraw\ddrawExternal.h"
#include "GDI.h"
#include "Settings\Settings.h"
#include "Logging\Logging.h"

#undef GetOpenFileName
#undef GetSaveFileName

namespace GdiWrapper
{
INITIALIZE_OUT_WRAPPED_PROC(GetOpenFileNameA, unused);
INITIALIZE_OUT_WRAPPED_PROC(GetOpenFileNameW, unused);
INITIALIZE_OUT_WRAPPED_PROC(GetSaveFileNameA, unused);
INITIALIZE_OUT_WRAPPED_PROC(GetSaveFileNameW, unused);
}

using namespace GdiWrapper;

template <class T>
void UpdateOpenFileNameStruct(T& OpenFile)
{
OpenFile.Flags &= ~(OFN_ENABLEHOOK | OFN_ENABLETEMPLATE);
}

BOOL WINAPI comdlg_GetOpenFileNameA(LPOPENFILENAMEA lpOpenFile)
{
Logging::LogDebug() << __FUNCTION__;

DEFINE_STATIC_PROC_ADDRESS(GetOpenFileNameAProc, GetOpenFileName, GetOpenFileNameA_out);

if (!GetOpenFileName)
{
return 0;
}

if (lpOpenFile && (lpOpenFile->Flags & OFN_ENABLEHOOK))
{
OPENFILENAMEA OpenFile = {};
memcpy(&OpenFile, lpOpenFile, lpOpenFile->lStructSize);
UpdateOpenFileNameStruct(OpenFile);

return GetOpenFileName(&OpenFile);
}

return GetOpenFileName(lpOpenFile);
}

BOOL WINAPI comdlg_GetOpenFileNameW(LPOPENFILENAMEW lpOpenFile)
{
Logging::LogDebug() << __FUNCTION__;

DEFINE_STATIC_PROC_ADDRESS(GetOpenFileNameWProc, GetOpenFileName, GetOpenFileNameW_out);

if (!GetOpenFileName)
{
return 0;
}

if (lpOpenFile && (lpOpenFile->Flags & OFN_ENABLEHOOK))
{
OPENFILENAMEW OpenFile = {};
memcpy(&OpenFile, lpOpenFile, lpOpenFile->lStructSize);
UpdateOpenFileNameStruct(OpenFile);

return GetOpenFileName(&OpenFile);
}

return GetOpenFileName(lpOpenFile);
}

BOOL WINAPI comdlg_GetSaveFileNameA(LPOPENFILENAMEA lpOpenFile)
{
Logging::LogDebug() << __FUNCTION__;

DEFINE_STATIC_PROC_ADDRESS(GetSaveFileNameAProc, GetSaveFileName, GetSaveFileNameA_out);

if (!GetSaveFileName)
{
return 0;
}

if (lpOpenFile && (lpOpenFile->Flags & OFN_ENABLEHOOK))
{
OPENFILENAMEA OpenFile = {};
memcpy(&OpenFile, lpOpenFile, lpOpenFile->lStructSize);
UpdateOpenFileNameStruct(OpenFile);

return GetSaveFileName(&OpenFile);
}

return GetSaveFileName(lpOpenFile);
}

BOOL WINAPI comdlg_GetSaveFileNameW(LPOPENFILENAMEW lpOpenFile)
{
Logging::LogDebug() << __FUNCTION__;

DEFINE_STATIC_PROC_ADDRESS(GetSaveFileNameWProc, GetSaveFileName, GetSaveFileNameW_out);

if (!GetSaveFileName)
{
return 0;
}

if (lpOpenFile && (lpOpenFile->Flags & OFN_ENABLEHOOK))
{
OPENFILENAMEW OpenFile = {};
memcpy(&OpenFile, lpOpenFile, lpOpenFile->lStructSize);
UpdateOpenFileNameStruct(OpenFile);

return GetSaveFileName(&OpenFile);
}

return GetSaveFileName(lpOpenFile);
}
21 changes: 21 additions & 0 deletions GDI/Comdlg32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "commdlg.h"

typedef BOOL(WINAPI* GetOpenFileNameAProc)(LPOPENFILENAMEA lpOpenFile);
typedef BOOL(WINAPI* GetOpenFileNameWProc)(LPOPENFILENAMEW lpOpenFile);
typedef BOOL(WINAPI* GetSaveFileNameAProc)(LPOPENFILENAMEA lpOpenFile);
typedef BOOL(WINAPI* GetSaveFileNameWProc)(LPOPENFILENAMEW lpOpenFile);

BOOL WINAPI comdlg_GetOpenFileNameA(LPOPENFILENAMEA lpOpenFile);
BOOL WINAPI comdlg_GetOpenFileNameW(LPOPENFILENAMEW lpOpenFile);
BOOL WINAPI comdlg_GetSaveFileNameA(LPOPENFILENAMEA lpOpenFile);
BOOL WINAPI comdlg_GetSaveFileNameW(LPOPENFILENAMEW lpOpenFile);

namespace GdiWrapper
{
EXPORT_OUT_WRAPPED_PROC(GetOpenFileNameA, unused);
EXPORT_OUT_WRAPPED_PROC(GetOpenFileNameW, unused);
EXPORT_OUT_WRAPPED_PROC(GetSaveFileNameA, unused);
EXPORT_OUT_WRAPPED_PROC(GetSaveFileNameW, unused);
}
1 change: 1 addition & 0 deletions GDI/GDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include "Wrappers\wrapper.h"
#include "Gdi32.h"
#include "User32.h"
#include "Comdlg32.h"
10 changes: 10 additions & 0 deletions ddraw/ddraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void InitDDraw()
if (!GetModuleHandleA("user32.dll")) LoadLibrary("user32.dll");
HMODULE gdi32 = GetModuleHandleA("gdi32.dll");
HMODULE user32 = GetModuleHandleA("user32.dll");
HMODULE comdlg32 = GetModuleHandleA("Comdlg32.dll");
HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
if (gdi32)
{
Expand All @@ -86,8 +87,17 @@ void InitDDraw()
//SetWindowLongA_out = (FARPROC)Hook::HotPatch(GetProcAddress(user32, "SetWindowLongA"), "SetWindowLongA", user_SetWindowLongA);
//SetWindowLongW_out = (FARPROC)Hook::HotPatch(GetProcAddress(user32, "SetWindowLongW"), "SetWindowLongW", user_SetWindowLongW);
}
if (comdlg32)
{
Logging::Log() << "Installing Comdlg32 hooks";
GetOpenFileNameA_out = (FARPROC)Hook::HotPatch(GetProcAddress(comdlg32, "GetOpenFileNameA"), "GetOpenFileNameA", comdlg_GetOpenFileNameA);
GetOpenFileNameW_out = (FARPROC)Hook::HotPatch(GetProcAddress(comdlg32, "GetOpenFileNameW"), "GetOpenFileNameW", comdlg_GetOpenFileNameW);
GetSaveFileNameA_out = (FARPROC)Hook::HotPatch(GetProcAddress(comdlg32, "GetSaveFileNameA"), "GetSaveFileNameA", comdlg_GetSaveFileNameA);
GetSaveFileNameW_out = (FARPROC)Hook::HotPatch(GetProcAddress(comdlg32, "GetSaveFileNameW"), "GetSaveFileNameW", comdlg_GetSaveFileNameW);
}
if (kernel32)
{
Logging::Log() << "Installing Kernel32 hooks";
Utils::GetDiskFreeSpaceA_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "GetDiskFreeSpaceA"), "GetDiskFreeSpaceA", Utils::kernel_GetDiskFreeSpaceA);
Utils::CreateThread_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "CreateThread"), "CreateThread", Utils::kernel_CreateThread);
Utils::VirtualAlloc_out = (FARPROC)Hook::HotPatch(GetProcAddress(kernel32, "VirtualAlloc"), "VirtualAlloc", Utils::kernel_VirtualAlloc);
Expand Down
2 changes: 2 additions & 0 deletions dxwrapper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ copy /Y "$(ProjectDir)Settings\Settings.ini" "$(TargetDir)Build\dxwrapper.ini" &
<ClCompile Include="External\Hooking\IATPatch.cpp" />
<ClCompile Include="External\Logging\Logging.cpp" />
<ClCompile Include="External\MemoryModule\MemoryModule.c" />
<ClCompile Include="GDI\Comdlg32.cpp" />
<ClCompile Include="GDI\Gdi32.cpp" />
<ClCompile Include="GDI\User32.cpp" />
<ClCompile Include="GDI\WndProc.cpp" />
Expand Down Expand Up @@ -1269,6 +1270,7 @@ copy /Y "$(ProjectDir)Settings\Settings.ini" "$(TargetDir)Build\dxwrapper.ini" &
<ClInclude Include="External\Hooking\Hook.h" />
<ClInclude Include="External\Logging\Logging.h" />
<ClInclude Include="External\MemoryModule\MemoryModule.h" />
<ClInclude Include="GDI\Comdlg32.h" />
<ClInclude Include="GDI\GDI.h" />
<ClInclude Include="GDI\Gdi32.h" />
<ClInclude Include="GDI\User32.h" />
Expand Down
6 changes: 6 additions & 0 deletions dxwrapper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@
<ClCompile Include="External\Hooking.Patterns\Hooking.Patterns.cpp">
<Filter>External\Hooking.Patterns</Filter>
</ClCompile>
<ClCompile Include="GDI\Comdlg32.cpp">
<Filter>GDI</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Settings\AllSettings.ini">
Expand Down Expand Up @@ -1886,6 +1889,9 @@
<ClInclude Include="External\Hooking.Patterns\Hooking.Patterns.h">
<Filter>External\Hooking.Patterns</Filter>
</ClInclude>
<ClInclude Include="GDI\Comdlg32.h">
<Filter>GDI</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Dllmain\BuildNo.rc">
Expand Down

0 comments on commit 16eaea2

Please sign in to comment.