Skip to content

Hook example #1

Gratt-5r2 edited this page Apr 9, 2022 · 3 revisions

← Hooks

Show Hello, world! box in the game startup.

The engine has a few functions when called in the game startup. But the first and easy function is a oCarsten_StartUp. This functions prepares some console and game settings. So, we know the name of the function, which means we can use the CreateHookByName macro.

1. Open the Plugin.cpp file.

You can find it in the project explorer.

image

2. Open the G2A_Names.hpp (or G1_, G1A_, G2_)

image

And find the oCarsten_StartUp name. In this line we can see the function signature.

image

3. Create a new oCarsten_StartUp_Hooked declaration

We can ignore __cdecl and void arguments, because it expected by default.

void oCarsten_StartUp_Hooked();

4. Create a hook object

Use a CreateHookByName to create a hook by the famous engine name.

auto Hook_oCarsten_StartUp = CreateHookByName( &oCarsten_StartUp, &oCarsten_StartUp_Hooked, Hook_Auto );

5. Create a oCarsten_StartUp_Hooked body

void oCarsten_StartUp_Hooked() {
  Message::Box( "Hello, world!" );
  // TODO
}

6. Return to the original function

Use the Hook_oCarsten_StartUp object like as function.

Hook_oCarsten_StartUp()

7. Full code of this hook

void oCarsten_StartUp_Hooked();

auto Hook_oCarsten_StartUp = CreateHookByName( &oCarsten_StartUp, &oCarsten_StartUp_Hooked, Hook_Auto );

void oCarsten_StartUp_Hooked() {
  Message::Box( "Hello, world!" );
  Hook_oCarsten_StartUp();
}

8. Select the game platform

image

9. Check the result

Compile and place .dll to the Gothic\System\Autorun\ directory. Run the game and wait the start screen.

image


← Previous page | Next page →

Clone this wiki locally