|
| 1 | +--- |
| 2 | +sidebar_label: Creating a Plugin |
| 3 | +sidebar_position: 2 |
| 4 | +--- |
| 5 | + |
| 6 | +# Creating a Plugin |
| 7 | + |
| 8 | +A plugin is a standard Windows DLL (`.dll`) or ASI file (`.asi`) placed in `.interposer\Plugins\`. It has no link-time dependency on the Interposer — all API functions are resolved at runtime via `GetProcAddress`. |
| 9 | + |
| 10 | +## Project Setup |
| 11 | + |
| 12 | +Create a new DLL project targeting the same architecture as the game (x86 for 32-bit games, x64 for 64-bit games). No additional libraries or headers are required beyond the Windows SDK. |
| 13 | + |
| 14 | +The only entry point needed is `DllMain`: |
| 15 | + |
| 16 | +```cpp |
| 17 | +BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD fdwReason, LPVOID /*lpReserved*/) |
| 18 | +{ |
| 19 | + if (fdwReason == DLL_PROCESS_ATTACH) |
| 20 | + Initialize(); |
| 21 | + return TRUE; |
| 22 | +} |
| 23 | +``` |
| 24 | +
|
| 25 | +## Resolving the API |
| 26 | +
|
| 27 | +Declare function pointer types for the Interposer exports you need and resolve them with `GetProcAddress`. The Interposer may be loaded under different filenames depending on the deployment variant, so check the known names in order: |
| 28 | +
|
| 29 | +```cpp |
| 30 | +using FnInterposerLog = void (WINAPI*)(const wchar_t* verb, const wchar_t* message); |
| 31 | +using FnInterposerGetConfigString = BOOL (WINAPI*)(const wchar_t* dotPath, wchar_t* buf, DWORD bufSize); |
| 32 | +
|
| 33 | +static FnInterposerLog pfnLog = nullptr; |
| 34 | +static FnInterposerGetConfigString pfnGetConfig = nullptr; |
| 35 | +
|
| 36 | +static bool ResolveAPI() |
| 37 | +{ |
| 38 | + static const wchar_t* kCandidates[] = { |
| 39 | + L"LANCommander.Interposer.dll", |
| 40 | + L"version.dll", // proxy variant |
| 41 | + }; |
| 42 | +
|
| 43 | + HMODULE hInterposer = nullptr; |
| 44 | + for (const wchar_t* name : kCandidates) |
| 45 | + { |
| 46 | + hInterposer = GetModuleHandleW(name); |
| 47 | + if (hInterposer) break; |
| 48 | + } |
| 49 | +
|
| 50 | + if (!hInterposer) return false; |
| 51 | +
|
| 52 | + pfnLog = (FnInterposerLog) GetProcAddress(hInterposer, "InterposerLog"); |
| 53 | + pfnGetConfig = (FnInterposerGetConfigString)GetProcAddress(hInterposer, "InterposerGetConfigString"); |
| 54 | +
|
| 55 | + return pfnLog && pfnGetConfig; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## API Reference |
| 60 | + |
| 61 | +All exported functions use the `WINAPI` (`__stdcall`) calling convention and undecorated `extern "C"` names. |
| 62 | + |
| 63 | +### `InterposerLog` |
| 64 | + |
| 65 | +```cpp |
| 66 | +void InterposerLog(const wchar_t* verb, const wchar_t* message); |
| 67 | +``` |
| 68 | +
|
| 69 | +Writes a line to the session log regardless of the `Logging` flags in `Config.yml`. The log line format matches the rest of the session log: |
| 70 | +
|
| 71 | +``` |
| 72 | +YYYY-MM-DD HH:MM:SS [VERB] <message> |
| 73 | +``` |
| 74 | +
|
| 75 | +`verb` is normalised automatically: any existing `[`/`]` brackets and surrounding whitespace are stripped, the content is truncated to 16 characters, and it is re-wrapped as `[verb]` right-padded to 18 characters. Pass a plain string such as `L"MYPLUGIN"` — no manual padding required. |
| 76 | +
|
| 77 | +--- |
| 78 | +
|
| 79 | +### `InterposerGetConfigString` |
| 80 | +
|
| 81 | +```cpp |
| 82 | +BOOL InterposerGetConfigString(const wchar_t* dotPath, wchar_t* buffer, DWORD bufferSize); |
| 83 | +``` |
| 84 | + |
| 85 | +Reads a scalar value from `Config.yml` by dot-separated YAML path. Returns `TRUE` on success, `FALSE` if the key does not exist, is not a scalar, or the buffer is too small. |
| 86 | + |
| 87 | +`bufferSize` is in `wchar_t` units and must include room for the null terminator. |
| 88 | + |
| 89 | +```cpp |
| 90 | +wchar_t setting[256]; |
| 91 | +if (pfnGetConfig(L"Plugins.MyPlugin.Setting", setting, ARRAYSIZE(setting))) |
| 92 | +{ |
| 93 | + // use setting |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Plugin configuration should live under a `Plugins.<PluginName>` namespace in `Config.yml` to avoid collisions: |
| 98 | + |
| 99 | +```yaml |
| 100 | +Plugins: |
| 101 | + MyPlugin: |
| 102 | + Setting: hello |
| 103 | + Count: 42 |
| 104 | +``` |
| 105 | +
|
| 106 | +--- |
| 107 | +
|
| 108 | +### `InterposerGetUsername` |
| 109 | + |
| 110 | +```cpp |
| 111 | +BOOL InterposerGetUsername(wchar_t* buffer, DWORD bufferSize); |
| 112 | +``` |
| 113 | + |
| 114 | +Returns the effective player username: the value configured in `Config.yml` under `Player.Username` or passed via the `--username` injector flag. Falls back to the real Windows account name (`GetUserNameW`) if no override is configured. |
| 115 | + |
| 116 | +`bufferSize` is in `wchar_t` units including the null terminator. Returns `TRUE` on success. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +### `InterposerSetRegistryValue` |
| 121 | + |
| 122 | +```cpp |
| 123 | +void InterposerSetRegistryValue(const wchar_t* keyPath, const wchar_t* valueName, const wchar_t* value); |
| 124 | +``` |
| 125 | + |
| 126 | +Injects a `REG_SZ` string value into the in-memory virtual registry store. Subsequent `RegQueryValueEx` calls for `keyPath\valueName` return `value` without touching the real registry. The injection is transient — it is not persisted to `.interposer\Registry.reg`. |
| 127 | + |
| 128 | +`keyPath` must be a full path beginning with a hive name: |
| 129 | + |
| 130 | +``` |
| 131 | +HKEY_LOCAL_MACHINE\SOFTWARE\MyGame\1.0 |
| 132 | +``` |
| 133 | + |
| 134 | +Set `valueName` to `L"@"`, `L""`, or `nullptr` to target the default (unnamed) registry value — the entry shown as `(Default)` in Registry Editor. |
| 135 | + |
| 136 | +:::note |
| 137 | +The target key must already exist in `.interposer\Registry.reg` for reads to be intercepted. Add an empty key header if no values need to be pre-populated: |
| 138 | + |
| 139 | +``` |
| 140 | +[HKEY_LOCAL_MACHINE\SOFTWARE\MyGame\1.0] |
| 141 | +``` |
| 142 | +::: |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +### `InterposerSetRegistryValueBySuffix` |
| 147 | + |
| 148 | +```cpp |
| 149 | +DWORD InterposerSetRegistryValueBySuffix(const wchar_t* keySuffix, const wchar_t* valueName, const wchar_t* value); |
| 150 | +``` |
| 151 | + |
| 152 | +Like `InterposerSetRegistryValue`, but matches by suffix rather than exact path. Any key in the virtual store whose path ends with `\keySuffix` (matched case-insensitively on a backslash component boundary) receives the injected value. |
| 153 | + |
| 154 | +Returns the number of keys updated. A return value of `0` means the suffix matched nothing in the virtual store — check that the target key is present in `.interposer\Registry.reg`. |
| 155 | + |
| 156 | +This is useful when the full registry path varies between game versions or installations: |
| 157 | + |
| 158 | +```cpp |
| 159 | +// Matches HKEY_LOCAL_MACHINE\...\Electronic Arts\EA Games\Battlefield 1942\ergc |
| 160 | +// regardless of any intermediate path components. |
| 161 | +pfnSetBySuffix(L"Battlefield 1942\\ergc", L"@", generatedKey); |
| 162 | +``` |
| 163 | + |
| 164 | +## Minimal Example |
| 165 | + |
| 166 | +```cpp |
| 167 | +#define WIN32_LEAN_AND_MEAN |
| 168 | +#include <windows.h> |
| 169 | +#include <string> |
| 170 | +
|
| 171 | +using FnInterposerLog = void (WINAPI*)(const wchar_t*, const wchar_t*); |
| 172 | +using FnInterposerGetConfigString = BOOL (WINAPI*)(const wchar_t*, wchar_t*, DWORD); |
| 173 | +
|
| 174 | +static FnInterposerLog pfnLog = nullptr; |
| 175 | +static FnInterposerGetConfigString pfnGetConfig = nullptr; |
| 176 | +
|
| 177 | +static void Initialize() |
| 178 | +{ |
| 179 | + HMODULE h = GetModuleHandleW(L"LANCommander.Interposer.dll"); |
| 180 | + if (!h) h = GetModuleHandleW(L"version.dll"); |
| 181 | + if (!h) return; |
| 182 | +
|
| 183 | + pfnLog = (FnInterposerLog) GetProcAddress(h, "InterposerLog"); |
| 184 | + pfnGetConfig = (FnInterposerGetConfigString)GetProcAddress(h, "InterposerGetConfigString"); |
| 185 | + if (!pfnLog || !pfnGetConfig) return; |
| 186 | +
|
| 187 | + wchar_t greeting[256] = L"hello"; |
| 188 | + pfnGetConfig(L"Plugins.MyPlugin.Greeting", greeting, ARRAYSIZE(greeting)); |
| 189 | +
|
| 190 | + pfnLog(L"MYPLUGIN", greeting); |
| 191 | +} |
| 192 | +
|
| 193 | +BOOL APIENTRY DllMain(HMODULE, DWORD reason, LPVOID) |
| 194 | +{ |
| 195 | + if (reason == DLL_PROCESS_ATTACH) |
| 196 | + Initialize(); |
| 197 | + return TRUE; |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +```yaml |
| 202 | +# .interposer\Config.yml |
| 203 | +Plugins: |
| 204 | + MyPlugin: |
| 205 | + Greeting: "Plugin loaded successfully" |
| 206 | +``` |
0 commit comments