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
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <memory>
#include "opentelemetry/plugin/detail/dynamic_library_handle.h"
#include "opentelemetry/plugin/detail/loader_info.h"
#include "opentelemetry/plugin/detail/utility.h"
#include "opentelemetry/plugin/factory.h"
#include "opentelemetry/plugin/hook.h"
#include "opentelemetry/version.h"
#include <windows.h>
#include <errhandlingapi.h>
#include <winbase.h>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace plugin
{
namespace detail
{
inline void GetLastErrorMessage(std::string &error_message) noexcept
{
auto error_code = ::GetLastError();
// See https://stackoverflow.com/a/455533/4447365
LPTSTR error_text = nullptr;
auto size = ::FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&error_text), 0, nullptr);
if (size == 0)
{
return;
}
CopyErrorMessage(error_text, error_message);
::LocalFree(error_text);
}
} // namespace detail
class DynamicLibraryHandleWindows final : public DynamicLibraryHandle
{
public:
explicit DynamicLibraryHandleWindows(HINSTANCE handle) : handle_{handle} {}
~DynamicLibraryHandleWindows() override { ::FreeLibrary(handle_); }
private:
HINSTANCE handle_;
};
inline std::unique_ptr<Factory> LoadFactory(const char *plugin, std::string &error_message) noexcept
{
auto handle = ::LoadLibrary(plugin);
if (handle == nullptr)
{
detail::GetLastErrorMessage(error_message);
return nullptr;
}
std::shared_ptr<DynamicLibraryHandle> library_handle{new (std::nothrow)
DynamicLibraryHandleWindows{handle}};
if (library_handle == nullptr)
{
detail::CopyErrorMessage("Allocation failure", error_message);
return nullptr;
}
auto make_factory_impl = reinterpret_cast<OpenTelemetryHook *>(
::GetProcAddress(handle, "OpenTelemetryMakeFactoryImpl"));
if (make_factory_impl == nullptr)
{
detail::GetLastErrorMessage(error_message);
return nullptr;
}
if (*make_factory_impl == nullptr)
{
detail::CopyErrorMessage("Invalid plugin hook", error_message);
return nullptr;
}
LoaderInfo loader_info;
nostd::unique_ptr<char[]> plugin_error_message;
auto factory_impl = (**make_factory_impl)(loader_info, plugin_error_message);
if (factory_impl == nullptr)
{
detail::CopyErrorMessage(plugin_error_message.get(), error_message);
return nullptr;
}
return std::unique_ptr<Factory>{new (std::nothrow)
Factory{std::move(library_handle), std::move(factory_impl)}};
}
} // namespace plugin
OPENTELEMETRY_END_NAMESPACE
|