Skip to content

Commit ea3362a

Browse files
committed
Improve exception handling in hookcheck program
1 parent 46ff069 commit ea3362a

File tree

8 files changed

+51
-11
lines changed

8 files changed

+51
-11
lines changed

utils/hookcheck.exe

-1 KB
Binary file not shown.

utils/src/gen_language_list/build-solution.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pause
88
exit /b 1
99

1010
:FindVisualStudio
11-
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -latest -requires Microsoft.Component.MSBuild -find "**\VsDevCmd.bat"`) do (
11+
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -products * -latest -requires Microsoft.Component.MSBuild -find "**\VsDevCmd.bat"`) do (
1212
set VSDEVCMD=%%i
1313
)
1414

utils/src/hookcheck/build-solution.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pause
88
exit /b 1
99

1010
:FindVisualStudio
11-
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -latest -requires Microsoft.Component.MSBuild -find "**\VsDevCmd.bat"`) do (
11+
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -products * -latest -requires Microsoft.Component.MSBuild -find "**\VsDevCmd.bat"`) do (
1212
set VSDEVCMD=%%i
1313
)
1414

utils/src/hookcheck/dll-analyzer.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515

1616
#include <string>
1717
#include <cstddef>
18-
#include <filesystem>
1918

2019
#define NOMINMAX
2120
#define WIN32_LEAN_AND_MEAN
2221
#include <Windows.h>
2322

24-
namespace fs = std::filesystem;
25-
2623
static const std::string EmptyString;
2724

2825
struct CV_INFO_PDB70

utils/src/hookcheck/hookcheck.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
namespace fs = std::filesystem;
3636

3737
int wmain(int argc, wchar_t* argv[])
38+
try
3839
{
3940
SetConsoleOutputCP(CP_UTF8);
4041

@@ -102,10 +103,12 @@ int wmain(int argc, wchar_t* argv[])
102103
return 1;
103104
}
104105

105-
const fs::path dllFilePath = fs::absolute(arguments.dll.value());
106+
std::error_code ec{};
107+
108+
const fs::path dllFilePath = fs::absolute(arguments.dll.value(), ec);
106109
std::wstring labelName = arguments.label.value_or(L"" DEFAULT_LABEL);
107110

108-
if (!fs::exists(dllFilePath) || !fs::is_regular_file(dllFilePath))
111+
if (!fs::exists(dllFilePath, ec) || !fs::is_regular_file(dllFilePath, ec))
109112
{
110113
PrintErrorLn(L"DLL file '{}' does not exist or is not readable", dllFilePath.wstring());
111114
return 1;
@@ -175,7 +178,7 @@ int wmain(int argc, wchar_t* argv[])
175178
}
176179
else
177180
{
178-
pdbFilePath = fs::absolute(filePath);
181+
pdbFilePath = fs::absolute(filePath, ec);
179182
}
180183

181184
#ifdef MTA_DEBUG
@@ -188,7 +191,7 @@ int wmain(int argc, wchar_t* argv[])
188191
PrintLn(L"[~] Ignore: '{}'", ignore);
189192
#endif
190193

191-
if (!fs::exists(pdbFilePath) || !fs::is_regular_file(pdbFilePath))
194+
if (!fs::exists(pdbFilePath, ec) || !fs::is_regular_file(pdbFilePath, ec))
192195
{
193196
PrintErrorLn(L"{} is not a regular file", pdbFilePath.wstring());
194197
return 1;
@@ -281,7 +284,7 @@ int wmain(int argc, wchar_t* argv[])
281284

282285
std::vector<std::wstring> problems;
283286

284-
if (!hasLabel && !fs::is_regular_file(function.SourceFile))
287+
if (!hasLabel && !fs::is_regular_file(function.SourceFile, ec))
285288
continue;
286289

287290
if (function.Name.starts_with(L"std::") || function.Name.starts_with(L'_'))
@@ -336,3 +339,27 @@ int wmain(int argc, wchar_t* argv[])
336339

337340
return exitCode;
338341
}
342+
catch (const fs::filesystem_error& ex)
343+
{
344+
HRESULT hr = S_OK;
345+
346+
if (ex.code())
347+
{
348+
if (ex.code().category() == std::system_category())
349+
{
350+
hr = HRESULT_FROM_WIN32(ex.code().value());
351+
}
352+
else
353+
{
354+
hr = E_FAIL;
355+
}
356+
}
357+
358+
PrintErrorLn(hr, L"A filesystem exception was thrown: {}", ToWideString(ex.what()));
359+
return -1;
360+
}
361+
catch (const std::exception& ex)
362+
{
363+
PrintErrorLn(L"An exception was thrown: {}", ToWideString(ex.what()));
364+
return -1;
365+
}

utils/src/hookcheck/locate-dia-sdk.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if exist "%VSWHERE%" goto :FindVisualStudio
55
exit /b 1
66

77
:FindVisualStudio
8-
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -latest -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
8+
for /f "usebackq tokens=*" %%i in (`call "%VSWHERE%" -products * -latest -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
99
set VSDIR=%%i
1010
)
1111

utils/src/hookcheck/utility.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "utility.h"
1313
#include <filesystem>
1414

15+
#define NOMINMAX
16+
#define WIN32_LEAN_AND_MEAN
17+
#include <Windows.h>
18+
1519
void Trim(std::wstring& input)
1620
{
1721
if (input.empty())
@@ -36,3 +40,12 @@ void NormalizePath(std::wstring& input)
3640

3741
std::replace(input.begin(), input.end(), L'/', L'\\');
3842
}
43+
44+
auto ToWideString(const char* input) -> std::wstring
45+
{
46+
int length = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
47+
std::wstring wide(length, L'\0');
48+
MultiByteToWideChar(CP_UTF8, 0, input, -1, &wide[0], length);
49+
Trim(wide);
50+
return wide;
51+
}

utils/src/hookcheck/utility.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void Trim(std::wstring& input);
2121

2222
void NormalizePath(std::wstring& input);
2323

24+
[[nodiscard]]
25+
auto ToWideString(const char* input) -> std::wstring;
26+
2427
template <typename... Args>
2528
void PrintLn(const std::wformat_string<Args...> fmt, Args&&... args)
2629
{

0 commit comments

Comments
 (0)