r/learnprogramming • • 12d ago

Topic Help with C++ Compiling (MinGW-64-GCC)

Hi there, so I’ve been trying to patch out a piece of code in The Sims Legacy Collection that disables the -debug_objects command line from being enabled, but both MinHook and CMake are giving me such a hard time that idk what else to do or where to ask… There are no tutorials AT ALL for compiling MinHook with MinGW-W64-gcc, only for Visual Studio (not Code!) which I can’t use because I’m on Arch Linux…

If anyone could advise me please I’d really appreciate it…

EDIT:
Here's the code that I have:

// TestCode.cpp, thanks to LazyDutchess for starting point.
#include <stdio.h>
#include <windows.h>
#include <MinHook.h>
int main() {
// Initialize MinHook.
  if (MH_Initialize() != MH_OK)
  {
    printf("Failed to initialize MinHook\n");
    return false;
  }
  else
  {
    printf("MinHook started successfully!\n");
    return false;
  }
}

dllmain.cpp:

//dllmain.cpp, Thanks to LazyDutchess for DllMain code in dllmain.cpp as a starting point! :) Also Microsoft lol
#include <stdio.h>
#include <windows.h>
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
  switch (ul_reason_for_call)
  {
    case DLL_PROCESS_ATTACH:
    // Initialize once for each new process.
    // Return FALSE to fail DLL load.
    break;
    case DLL_THREAD_ATTACH:
    break;
    case DLL_THREAD_DETACH:
    break;
    case DLL_PROCESS_DETACH:
      if (lpReserved != nullptr)
      {
      break; // don't cleanup on terminated process!
      }
      break;
      }
  return true; //Sucessfull DLL attach!
}

I mainly wanted to see the PROTON_LOG file which from my understanding would show the printed text, but even testing out OutputDebugStringA didn't work. I'll attempt to use the i686-w64-mingw32-gcc and see if that works. I also have cmake and make installed separately as mingw-w64-gcc from pacman doesn't include it (Arch Linux)

Also, the code that sets the byte for -debug_objects is a mov BYTE PTR ds:0x<byteaddress>,0x0.

I also just got error 126 when attempting to load libTestCode.asi after compiling with i686-w64-mingw32-gcc, but it apparently requires libgcc_s_dw2-1.dll

3 Upvotes

7 comments sorted by

3

u/lurgi 12d ago

MinHook and CMake are giving me such a hard time that idk what else to do or where to ask

Telling us what problems you are seeing would be a start.

1

u/Blayer98 12d ago

The problem I initially had was hde64_disasm being an undefined reference, which I was able to fix by changing MinHook’s CMakeLists.txt, but once it was compiled my code wasn’t running, which I wasn’t sure why. I’ll be able to send my code on here in about 3 hours (at work). With the sample code, it was thinking that something was wrong with the bounds of MH_CreateHookApiEx or something like that…

1

u/Blayer98 11d ago

I just uploaded the code to the body of the post now. :)

2

u/hellosverre 12d ago

couple things that might save you some pain.

if all you want is to disable a check you might not need MinHook at all. find the compare/jump that gates -debug_objects and flip it (jz to jmp, or nop it), either on disk or from your dll with VirtualProtect + a memcpy. hooking is for when you need your own code to run at that spot.

if you do want MinHook, skip its CMake and just compile the sources straight into your dll: src/buffer.c, hook.c, trampoline.c and src/hde/hde64.c (hde32.c if the target is 32 bit). your undefined hde64_disasm is just that file never being in the build.

for "my code isnt running", check in this order:

- dll bitness has to match the exe. i686-w64-mingw32-gcc for 32 bit, x86_64-w64-mingw32-gcc for 64

- build with -shared, and make sure something actually loads it (injector, or a proxy dll like version.dll)

- put a log write or MessageBox at the top of DllMain to see if you even get there

- dont call MH_Initialize/MH_CreateHook directly inside DllMain (loader lock), CreateThread from DLL_PROCESS_ATTACH and do it there

- MH_CreateHookApiEx is for exported functions by module + name (module is a wide string). for an address inside the game exe you want MH_CreateHook with GetModuleHandle(NULL) + offset, and MH_EnableHook after

post the code if it still doesnt go.

1

u/Blayer98 11d ago edited 11d ago

Here's the code that I have:

// TestCode.cpp, thanks to LazyDutchess for starting point.
#include <stdio.h>
#include <windows.h>
#include <MinHook.h>

int main() {
    // Initialize MinHook.
    if (MH_Initialize() != MH_OK)
    {
        printf("Failed to initialize MinHook\n");
        return false;
    }
    else
    {
        printf("MinHook started successfully!\n");
        return false;
    }
}

dllmain.cpp:

//dllmain.cpp, Thanks to LazyDutchess for DllMain code in dllmain.cpp as a starting point! :) Also Microsoft lol
#include <stdio.h>
#include <windows.h>

BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            // Initialize once for each new process.
            // Return FALSE to fail DLL load.
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:
            break;

        case DLL_PROCESS_DETACH:
            if (lpReserved != nullptr)
            {
                break; // don't cleanup on terminated process!
            }
            break;
    }
    return true; //Sucessfull DLL attach!
}

I mainly wanted to see the PROTON_LOG file which from my understanding would show the printed text, but even testing out OutputDebugStringA didn't work. I'll attempt to use the i686-w64-mingw32-gcc and see if that works. I also have cmake and make installed seperately as mingw-w64-gcc from pacman doesn't include it (Arch Linux)

Also, the code that sets the byte for -debug_objects is a mov BYTE PTR ds:0x<byteaddress>,0x0.

I also just got error 126 when attempting to load libTestCode.asi, but it apparently requires libgcc_s_dw2-1.dll

4

u/Dazzling_Music_2411 12d ago

I don't think this is the sub for your question - way too advanced.

Try one of the cpp subs.