Skip to content

Commit

Permalink
Fixed a terrible programmer error.
Browse files Browse the repository at this point in the history
I learned a lesson about DllMain today. (https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx#general_best_practices)
Funny how this doesn't crash on Windows 10 (at least in my experience), but was causing crashing on Windows 7 for most users. I wonder what MS changed in their PE loader.
  • Loading branch information
Sumwunn authored Jun 30, 2016
1 parent 07f50a2 commit 81b919a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
41 changes: 21 additions & 20 deletions AchievementsModsEnabler_FO4/AchievementsModsEnabler_FO4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/

#include "stdafx.h"
#include <windows.h>
#include <ShlObj.h>
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iostream>
#include <string>

// Defined functions.
// ASM.
Expand All @@ -32,12 +32,16 @@ extern "C" void* BinSearch(void* Search, int SearchLength, unsigned char* Bytes,
extern "C" void* GetTextSectionAddr(HMODULE Module, int DataType);
extern "C" int GetTextSectionSize(HMODULE Module, int DataType);

int Setup() {
// Return values
// -1 = Process is NOT Fallout4.exe.
// -2 = Log file creation failed.

extern "C" __declspec(dllexport) int Setup() {

// These bytes will land us just beneath where the achivements mods disabler code is at.
unsigned char BytesToFind[20] = { 0x8B, 0xD7, 0x4C, 0x8B, 0x00, 0x48, 0x8B, 0xC8, 0x41, 0xFF, 0x50, 0x28, 0x48, 0x8B, 0x9C, 0x24, 0x50, 0x04, 0x00, 0x00 };
unsigned char BytesToFind[] = { 0x8B, 0xD7, 0x4C, 0x8B, 0x00, 0x48, 0x8B, 0xC8, 0x41, 0xFF, 0x50, 0x28, 0x48, 0x8B, 0x9C, 0x24, 0x50, 0x04, 0x00, 0x00 };
// This is what we patch it with (check notes.txt).
unsigned char BytesPatch[4] = { 0x90, 0x90, 0x90, 0x90 };
unsigned char BytesPatch[] = { 0x90, 0x90, 0x90, 0x90 };
// The address we get from GetTextSectionAddr.
void* SearchAddress = (void*)NULL;
// The size too.
Expand All @@ -49,7 +53,7 @@ int Setup() {
// Misc.
DWORD OldVP = NULL;

//////// Setup Part 1 - Addresses ////////
//////// Setup Part 1 - Addresses & Logging ////////

// Get module of target to writes hooks to.
HMODULE TargetModule = GetModuleHandle(L"Fallout4.exe");
Expand All @@ -58,17 +62,13 @@ int Setup() {
return -1;
}

// Setup Log.
PWSTR LogPathPTR = NULL;
TCHAR LogPath[MAX_PATH];
FILE *pLogFile = NULL;

// Get log path.
SHGetKnownFolderPath(FOLDERID_Documents, NULL, NULL, &LogPathPTR);
_tcscpy_s(LogPath, MAX_PATH, LogPathPTR);
_tcscat_s(LogPath, MAX_PATH, L"\\My Games\\Fallout4\\AchievementsModsEnabler_FO4.log");
// Open up fresh log file.
_tfopen_s(&pLogFile, LogPath, L"w+");
std::ofstream LogFile;
LogFile.open("AchievementsModsEnabler_FO4.log");
// Log file creation failed.
if (!LogFile) {
return -2;
}

// Get size and address of Fallout4.exe's .text section.
SearchSize = GetTextSectionSize(TargetModule, 1);
Expand All @@ -79,7 +79,7 @@ int Setup() {
// Bytes not found!
if (PatchAddress == NULL) {
// Log message.
_fputts(L"NO", pLogFile);
LogFile << "NO" << std::endl;
}
// Bytes found!
else {
Expand All @@ -88,10 +88,11 @@ int Setup() {
memcpy(PatchAddress, BytesPatch, sizeof BytesPatch);
VirtualProtect(PatchAddress, sizeof BytesPatch, OldVP, &OldVP);
// Log message.
_fputts(L"YES", pLogFile);
LogFile << "YES" << std::endl;
}

// Cleanup.
fclose(pLogFile);
LogFile.close();

return 0;
}
Binary file modified AchievementsModsEnabler_FO4/AchievementsModsEnabler_FO4.rc
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;ACHIEVEMENTSMODSENABLER_FO4_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
Expand Down
3 changes: 0 additions & 3 deletions AchievementsModsEnabler_FO4/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

int Setup();

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
Expand All @@ -11,7 +9,6 @@ BOOL APIENTRY DllMain( HMODULE hModule,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Setup();
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
Expand Down

0 comments on commit 81b919a

Please sign in to comment.