Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (139 loc) · 4.92 KB
/
Copy pathmain.cpp
File metadata and controls
159 lines (139 loc) · 4.92 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
156
157
158
159
#include "Diagnostics.h"
#include "Protocol.h"
#include "ShellProbe.h"
#include <fstream>
#include <iostream>
#include <filesystem>
#include <optional>
#include <string>
#include <objbase.h>
#include <windows.h>
namespace
{
std::optional<std::wstring> ArgumentValue(int argc, wchar_t* argv[], const wchar_t* name)
{
for (int index = 1; index < argc - 1; ++index)
{
if (_wcsicmp(argv[index], name) == 0)
{
return argv[index + 1];
}
}
return std::nullopt;
}
std::string ReadUtf8File(const std::wstring& path)
{
std::ifstream stream(path, std::ios::binary);
if (!stream)
{
throw std::runtime_error("Could not open request JSON.");
}
return std::string(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>());
}
bool WriteUtf8File(const std::wstring& path, const std::string& text)
{
std::filesystem::path filePath(path);
std::error_code error;
if (filePath.has_parent_path())
{
std::filesystem::create_directories(filePath.parent_path(), error);
if (error)
{
Diagnostic("ProbeHostResultFileWriteFailed: CreateDirectory failed.");
return false;
}
}
std::ofstream stream(filePath, std::ios::binary | std::ios::trunc);
if (!stream)
{
Diagnostic("ProbeHostResultFileWriteFailed: Could not open result JSON.");
return false;
}
stream.write(text.data(), static_cast<std::streamsize>(text.size()));
return stream.good();
}
void EnrichProcessDiagnostics(ProbeResult& result)
{
result.probeHostProcessArchitecture = CurrentProcessArchitecture();
result.osArchitecture = CurrentOSArchitecture();
result.is64BitProcess = sizeof(void*) == 8;
#if defined(_M_IX86)
result.actualProbeHostMachineType = "x86";
#elif defined(_M_X64)
result.actualProbeHostMachineType = "x64";
#elif defined(_M_ARM64)
result.actualProbeHostMachineType = "arm64";
#else
result.actualProbeHostMachineType = "unknown";
#endif
}
bool WriteResult(const ProbeResult& result, const std::optional<std::wstring>& resultPath)
{
const auto text = SerializeResultJson(result);
if (resultPath.has_value() && !resultPath->empty())
{
if (WriteUtf8File(*resultPath, text))
{
return true;
}
}
std::cout << text;
return static_cast<bool>(std::cout);
}
}
int wmain(int argc, wchar_t* argv[])
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
Diagnostic("ProbeHostStart: ProcessArchitecture=" + CurrentProcessArchitecture() + ", OSArchitecture=" + CurrentOSArchitecture() + ".");
const auto requestPath = ArgumentValue(argc, argv, L"--request");
const auto resultPath = ArgumentValue(argc, argv, L"--result");
std::optional<ProbeRequest> request;
try
{
Diagnostic("ProbeStage=ValidateRequest");
if (!requestPath.has_value() || requestPath->empty() || !resultPath.has_value() || resultPath->empty())
{
auto result = FailureResult("InvalidProtocol", "Expected command line: ContextMenuMgr.ProbeHost.exe --request <request.json> --result <result.json>");
EnrichProcessDiagnostics(result);
return WriteResult(result, resultPath) ? 2 : 1;
}
const auto requestJson = ReadUtf8File(*requestPath);
request = ParseRequestJson(requestJson);
Diagnostic("ProbeHostRequestParsed: OperationId=" + request->operationId + ", ItemId=" + request->itemId + ", Category=" + CategoryName(request->category) + ", EntryKind=" + EntryKindName(request->entryKind) + ", ProbeMode=" + ProbeModeName(request->probeMode) + ".");
const HRESULT comHr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(comHr))
{
auto result = FailureResult(*request, "ComInitializeFailed", "COM initialization failed.", std::nullopt, "Stage=CoInitializeEx; HRESULT=" + FormatHResult(comHr));
EnrichProcessDiagnostics(result);
return WriteResult(result, resultPath) ? 2 : 1;
}
ProbeResult result;
try
{
result = RunProbe(*request);
EnrichProcessDiagnostics(result);
}
catch (const std::exception& ex)
{
result = FailureResult(*request, "UnhandledException", ex.what());
EnrichProcessDiagnostics(result);
}
CoUninitialize();
Diagnostic("ProbeStage=WriteResult");
if (!WriteResult(result, resultPath))
{
return 1;
}
return result.success ? 0 : 2;
}
catch (const std::exception& ex)
{
Diagnostic(std::string("ProbeHostUnhandledException: ") + ex.what());
ProbeResult result = request.has_value()
? FailureResult(*request, "UnhandledException", ex.what())
: FailureResult("InvalidRequest", ex.what());
EnrichProcessDiagnostics(result);
return WriteResult(result, resultPath) ? 2 : 1;
}
}