HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Global.h
Go to the documentation of this file.
1
8
9/*
10MIT License
11
12Copyright (c) 2018-2026 Benjamin Höglinger-Stelzer
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34
35#include <string>
36#include <Psapi.h>
37
38namespace 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};
std::string get_module_directory(HMODULE hMod)
Returns the directory containing the given module.
Definition Global.h:100
std::string process_name()
Returns the full path of the current process executable.
Definition Global.h:72
std::string get_process_directory()
Returns the directory containing the main process executable.
Definition Global.h:129
std::string expand_environment_variables(const std::string &str)
Expands environment variables (e.g. TEMP%) in a string.
Definition Global.h:49