Skip to content

Commit c8a334d

Browse files
committed
Fix #4, reformat some stuff
1 parent 9a40b80 commit c8a334d

File tree

6 files changed

+35
-74
lines changed

6 files changed

+35
-74
lines changed

BL3DX11Injection/BL3DX11Injection.vcxproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7676
<LinkIncremental>false</LinkIncremental>
7777
<TargetName>d3d11</TargetName>
78-
<OutDir>K:\Borderlands3\OakGame\Binaries\Win64</OutDir>
78+
<OutDir>K:\Borderlands3\OakGame\Binaries\Win64\</OutDir>
7979
</PropertyGroup>
8080
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8181
<LinkIncremental>false</LinkIncremental>
@@ -186,9 +186,6 @@
186186
<Library Include="HookLib.lib" />
187187
<Library Include="Zydis.lib" />
188188
</ItemGroup>
189-
<ItemGroup>
190-
<None Include="d3d11.def" />
191-
</ItemGroup>
192189
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
193190
<ImportGroup Label="ExtensionTargets" />
194191
</Project>

BL3DX11Injection/BL3DX11Injection.vcxproj.filters

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,4 @@
5353
<Filter>Header Files\Libraries</Filter>
5454
</Library>
5555
</ItemGroup>
56-
<ItemGroup>
57-
<None Include="d3d11.def">
58-
<Filter>Source Files</Filter>
59-
</None>
60-
</ItemGroup>
6156
</Project>

BL3DX11Injection/INIReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ home page for more info:
1212
https://github.com/benhoyt/inih
1313
1414
*/
15+
#pragma warning(disable: 26495)
1516

1617
#ifndef __INI_H__
1718
#define __INI_H__

BL3DX11Injection/PluginLoadHook.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,21 @@ void InitializePluginHooks(HMODULE gameModule) {
134134
pluginsPath = pPath;
135135
iniPath = pluginsPath + L"pluginLoader.ini";
136136

137-
PVOID Target = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "ExitProcess");
138-
139-
if (SetHook(Target, ExitProcessHook, reinterpret_cast<PVOID*>(&OriginalExitProcess)))
137+
auto kernelDll = GetModuleHandle(L"kernel32.dll");
138+
if (kernelDll == NULL) { // Honestly this is pretty much pointless but doing it like this removes an error & saves on some processing
139+
LogString(L"Unable to locate kernel module...\n");
140+
return;
141+
}
142+
143+
PVOID Target = GetProcAddress(kernelDll, "ExitProcess");
144+
if (Target != NULL && SetHook(Target, ExitProcessHook, reinterpret_cast<PVOID*>(&OriginalExitProcess)))
140145
LogString(L"Initialized ExitProcess(...) hook\n");
141146
else
142147
LogString(L"Unable to initialize ExitProcess(...) hook\n");
143148

144-
PVOID loadLibraryHook = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW");
145-
if (SetHook(loadLibraryHook, LoadLibraryWHook, reinterpret_cast<PVOID*>(&OriginalLoadLibrary)))
149+
PVOID loadLibraryHook = GetProcAddress(kernelDll, "LoadLibraryW");
150+
if (loadLibraryHook != NULL && SetHook(loadLibraryHook, LoadLibraryWHook, reinterpret_cast<PVOID*>(&OriginalLoadLibrary)))
146151
LogString(L"Initialized LoadLibraryW(...) hook...\n");
147152
else
148-
LogString(L"Unable to initialize LoadLibraryW(...) hook");
153+
LogString(L"Unable to initialize LoadLibraryW(...) hook\n");
149154
}

BL3DX11Injection/d3d11.def

Lines changed: 0 additions & 53 deletions
This file was deleted.

BL3DX11Injection/dllmain.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "dllmain.h"
55
#include "PluginLoadHook.h"
66

7+
#pragma warning(disable: 6031)
8+
9+
710
static HINSTANCE hL;
811
static HMODULE gameModule;
912

@@ -51,15 +54,28 @@ int executionThread() {
5154

5255
SetConsoleTitle(L"Borderlands 3 Plugin Loader");
5356

54-
HANDLE hStdout = CreateFile(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
55-
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
56-
HANDLE hStdin = CreateFile(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
57-
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
58-
57+
// All of this is necessary so that way we can properly use the output of the console
58+
freopen("CONIN$", "r", stdin);
59+
freopen("CONOUT$", "w", stderr);
60+
freopen("CONOUT$", "w", stdout);
61+
HANDLE hStdout = CreateFile(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
62+
HANDLE hStdin = CreateFile(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
63+
64+
// Make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
65+
std::ios::sync_with_stdio(true);
66+
SetStdHandle(STD_INPUT_HANDLE, hStdin);
5967
SetStdHandle(STD_OUTPUT_HANDLE, hStdout); // Set our STD handles
6068
SetStdHandle(STD_ERROR_HANDLE, hStdout); // stderr is going back to STDOUT
61-
SetStdHandle(STD_INPUT_HANDLE, hStdin);
6269

70+
// Clear the error states for all of the C++ stream objects.
71+
// Attempting to access the streams before they're valid causes them to enter an error state.
72+
std::wcout.clear();
73+
std::cout.clear();
74+
std::wcerr.clear();
75+
std::cerr.clear();
76+
std::wcin.clear();
77+
std::cin.clear();
78+
6379
LogString(L"Console allocated...\n");
6480
LogString(L"==== Debug ====\n");
6581

0 commit comments

Comments
 (0)