This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.hpp
More file actions
155 lines (129 loc) · 7.53 KB
/
process.hpp
File metadata and controls
155 lines (129 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* ============================================================================= *
* SA-MP Injector - C++ injection ↓ *
* library for SA-MP (San Andreas Multiplayer) and OMP (Open Multiplayer) *
* ============================================================================= *
* *
* Copyright (c) 2025, AlderGrounds *
* All rights reserved. *
* *
* Developed by: Calasans *
* Provided by: AlderGrounds *
* Repository: https://github.com/aldergrounds/samp-injector *
* *
* ============================================================================= *
* *
* Licensed under the MIT License (MIT); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at: *
* *
* https://opensource.org/licenses/MIT *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
* *
* ============================================================================= */
#pragma once
#include <string>
#include <memory>
#include <functional>
#include <windows.h>
//
#include "constants.hpp"
#include "error_utils.hpp"
#include "resource_handle.hpp"
#include "types.hpp"
#include "version.hpp"
//
#if defined(SAMP_INJECTOR_CXX_MODERN)
#include <optional>
#endif
class Process {
public:
Process() = default;
Process(const Process&) = delete;
Process& operator=(const Process&) = delete;
Process(Process&&) = default;
Process& operator=(Process&&) = default;
~Process() = default;
struct Process_Info {
Resource_Handle::Unique_Resource<HANDLE, std::function<void(HANDLE)>> process_handle;
Resource_Handle::Unique_Resource<HANDLE, std::function<void(HANDLE)>> thread_handle;
bool Resume() {
return thread_handle && ResumeThread(thread_handle.get()) != static_cast<DWORD>(-1);
}
};
enum class Injection_Status {
SUCCESS,
ALLOCATION_FAILED,
WRITE_FAILED,
KERNEL32_HANDLE_FAILED,
LOADLIBRARY_ADDRESS_FAILED,
THREAD_CREATION_FAILED,
THREAD_WAIT_FAILED,
GET_EXIT_CODE_FAILED,
INJECTION_RETURNED_ERROR
};
#if defined(SAMP_INJECTOR_CXX_MODERN)
std::optional<Process_Info> Create_Game_Process(const std::wstring& game_path, std::wstring& command_args, const std::wstring& working_dir) {
#elif defined(SAMP_INJECTOR_CXX_14)
std::unique_ptr<Process_Info> Create_Game_Process(const std::wstring& game_path, std::wstring& command_args, const std::wstring& working_dir) {
#endif
STARTUPINFOW startup_info{};
startup_info.cb = sizeof(STARTUPINFOW);
PROCESS_INFORMATION process_info{};
bool success = CreateProcessW(game_path.c_str(), command_args.empty() ? nullptr : &command_args[0], nullptr, nullptr, FALSE, Constants::PROCESS_CREATION_FLAGS, nullptr, working_dir.c_str(), &startup_info, &process_info);
if (!success) {
std::wstring error_msg = Error_Utils::Get_System_Error_Message(GetLastError());
Error_Utils::Show_Error(L"Failed to create game process. Ensure 'gta_sa.exe' is not running and you have " L"sufficient permissions to execute the file. System Error: " + error_msg, Types::Inject_Type::SAMP);
#if defined(SAMP_INJECTOR_CXX_MODERN)
return std::nullopt;
#elif defined(SAMP_INJECTOR_CXX_14)
return nullptr;
#endif
}
#if defined(SAMP_INJECTOR_CXX_MODERN)
return Process_Info{
Resource_Handle::Make_Unique_Handle(process_info.hProcess),
Resource_Handle::Make_Unique_Handle(process_info.hThread)
};
#elif defined(SAMP_INJECTOR_CXX_14)
auto info = std::unique_ptr<Process_Info>(new Process_Info());
info->process_handle = Resource_Handle::Make_Unique_Handle(process_info.hProcess);
info->thread_handle = Resource_Handle::Make_Unique_Handle(process_info.hThread);
return info;
#endif
}
SAMP_INJECTOR_NODISCARD
Injection_Status Inject_DLL(HANDLE process, const std::wstring& DLL_path) {
static const LPVOID load_library_addr = reinterpret_cast<LPVOID>(GetProcAddress(GetModuleHandleW(Constants::KERNEL32_DLL), Constants::LOAD_LIBRARY_FUNC));
if (!load_library_addr)
return Injection_Status::LOADLIBRARY_ADDRESS_FAILED;
const size_t dll_path_size = (DLL_path.length() + 1) * sizeof(wchar_t);
LPVOID remote_memory = VirtualAllocEx(process, nullptr, dll_path_size, Constants::MEMORY_ALLOCATION_TYPE, Constants::MEMORY_PROTECTION);
if (!remote_memory)
return Injection_Status::ALLOCATION_FAILED;
auto memory_guard = Resource_Handle::Unique_Resource<LPVOID, std::function<void(LPVOID)>>(remote_memory, [process](LPVOID ptr) {
if (ptr)
VirtualFreeEx(process, ptr, 0, MEM_RELEASE);
});
if (!WriteProcessMemory(process, remote_memory, DLL_path.c_str(), dll_path_size, nullptr))
return Injection_Status::WRITE_FAILED;
HANDLE remote_thread = CreateRemoteThread(process, nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(load_library_addr), remote_memory, 0, nullptr);
if (!remote_thread)
return Injection_Status::THREAD_CREATION_FAILED;
auto thread_guard = Resource_Handle::Make_Unique_Handle(remote_thread);
if (WaitForSingleObject(remote_thread, Constants::DLL_INJECTION_TIMEOUT_MS) != WAIT_OBJECT_0)
return Injection_Status::THREAD_WAIT_FAILED;
DWORD exit_code = 0;
if (!GetExitCodeThread(remote_thread, &exit_code))
return Injection_Status::GET_EXIT_CODE_FAILED;
if (exit_code == 0)
return Injection_Status::INJECTION_RETURNED_ERROR;
return Injection_Status::SUCCESS;
}
};