HydraHook
Direct3D and Core Audio hooking library for Windows
Toggle main menu visibility
Loading...
Searching...
No Matches
Global.h
Go to the documentation of this file.
1
8
9
/*
10
MIT License
11
12
Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
13
14
Permission is hereby granted, free of charge, to any person obtaining a copy
15
of this software and associated documentation files (the "Software"), to deal
16
in the Software without restriction, including without limitation the rights
17
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
copies of the Software, and to permit persons to whom the Software is
19
furnished to do so, subject to the following conditions:
20
21
The above copyright notice and this permission notice shall be included in all
22
copies or substantial portions of the Software.
23
24
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
SOFTWARE.
31
*/
32
33
#pragma once
34
35
#include <string>
36
#include <Psapi.h>
37
38
namespace
HydraHook
39
{
40
namespace
Core
41
{
42
namespace
Util
43
{
49
inline
std::string
expand_environment_variables
(
const
std::string & str)
50
{
51
const
DWORD neededSize = ExpandEnvironmentStringsA(str.c_str(),
nullptr
, 0);
52
if
(neededSize <= 1)
53
return
str;
54
55
std::string expanded(
static_cast<
size_t
>
(neededSize),
'\0'
);
56
const
DWORD written = ExpandEnvironmentStringsA(
57
str.c_str(),
58
expanded.data(),
59
neededSize);
60
if
(written == 0 || written > neededSize)
61
return
str;
62
63
// ExpandEnvironmentStringsA counts the terminating NUL.
64
expanded.resize(
static_cast<
size_t
>
(written - 1));
65
return
expanded;
66
}
67
72
inline
std::string
process_name
()
73
{
74
DWORD capacity = MAX_PATH;
75
std::string path(capacity,
'\0'
);
76
for
(;;)
77
{
78
const
DWORD len = GetProcessImageFileNameA(
79
GetCurrentProcess(),
80
path.data(),
81
capacity);
82
if
(len == 0)
83
{
84
if
(GetLastError() != ERROR_INSUFFICIENT_BUFFER || capacity > 32768)
85
return
std::string();
86
capacity *= 2;
87
path.assign(capacity,
'\0'
);
88
continue
;
89
}
90
path.resize(len);
91
return
path;
92
}
93
}
94
100
inline
std::string
get_module_directory
(HMODULE hMod)
101
{
102
DWORD capacity = MAX_PATH;
103
std::string path(capacity,
'\0'
);
104
for
(;;)
105
{
106
const
DWORD len = GetModuleFileNameA(hMod, path.data(), capacity);
107
if
(len == 0)
108
return
std::string();
109
if
(len >= capacity)
110
{
111
if
(capacity > 32768)
112
return
std::string();
113
capacity *= 2;
114
path.assign(capacity,
'\0'
);
115
continue
;
116
}
117
path.resize(len);
118
const
auto
pos = path.find_last_of(
"\\/"
);
119
if
(pos == std::string::npos)
120
return
std::string();
121
return
path.substr(0, pos + 1);
122
}
123
}
124
129
inline
std::string
get_process_directory
()
130
{
131
return
get_module_directory
(
nullptr
);
132
}
133
};
134
};
135
};
HydraHook::Core::Util
Definition
Global.h:43
HydraHook::Core::Util::get_module_directory
std::string get_module_directory(HMODULE hMod)
Returns the directory containing the given module.
Definition
Global.h:100
HydraHook::Core::Util::process_name
std::string process_name()
Returns the full path of the current process executable.
Definition
Global.h:72
HydraHook::Core::Util::get_process_directory
std::string get_process_directory()
Returns the directory containing the main process executable.
Definition
Global.h:129
HydraHook::Core::Util::expand_environment_variables
std::string expand_environment_variables(const std::string &str)
Expands environment variables (e.g. TEMP%) in a string.
Definition
Global.h:49
HydraHook::Core
Definition
Exceptions.hpp:15
HydraHook
Definition
Exceptions.hpp:13
src
HydraHook
Utils
Global.h
Generated by
1.18.0