Add CWindowsComLifecycle RAII wrapper for Windows COM library

This commit is contained in:
Robert Müller 2022-08-20 23:26:05 +02:00
parent 4fc6a5b924
commit 37cadc5c09
3 changed files with 27 additions and 0 deletions

View file

@ -65,6 +65,7 @@
#include <cerrno> #include <cerrno>
#include <io.h> #include <io.h>
#include <objbase.h>
#include <process.h> #include <process.h>
#include <share.h> #include <share.h>
#include <shellapi.h> #include <shellapi.h>
@ -4199,6 +4200,17 @@ int net_socket_read_wait(NETSOCKET sock, std::chrono::nanoseconds nanoseconds)
return ::net_socket_read_wait(sock, (nanoseconds / std::chrono::nanoseconds(1us).count()).count()); return ::net_socket_read_wait(sock, (nanoseconds / std::chrono::nanoseconds(1us).count()).count());
} }
#if defined(CONF_FAMILY_WINDOWS)
CWindowsComLifecycle::CWindowsComLifecycle()
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
}
CWindowsComLifecycle::~CWindowsComLifecycle()
{
CoUninitialize();
}
#endif
size_t std::hash<NETADDR>::operator()(const NETADDR &Addr) const noexcept size_t std::hash<NETADDR>::operator()(const NETADDR &Addr) const noexcept
{ {
return std::hash<std::string_view>{}(std::string_view((const char *)&Addr, sizeof(Addr))); return std::hash<std::string_view>{}(std::string_view((const char *)&Addr, sizeof(Addr)));

View file

@ -2515,6 +2515,19 @@ public:
} }
}; };
#if defined(CONF_FAMILY_WINDOWS)
/**
* This is a RAII wrapper to initialize/uninitialize the Windows COM library,
* which may be necessary for using the open_file and open_link functions.
*/
class CWindowsComLifecycle
{
public:
CWindowsComLifecycle();
~CWindowsComLifecycle();
};
#endif
/** /**
* Copies a string to a fixed-size array of chars. * Copies a string to a fixed-size array of chars.
* *

View file

@ -4561,6 +4561,8 @@ int main(int argc, const char **argv)
{ {
#if defined(CONF_PLATFORM_ANDROID) #if defined(CONF_PLATFORM_ANDROID)
const char **argv = const_cast<const char **>(argv2); const char **argv = const_cast<const char **>(argv2);
#elif defined(CONF_FAMILY_WINDOWS)
CWindowsComLifecycle WindowsComLifecycle;
#endif #endif
CCmdlineFix CmdlineFix(&argc, &argv); CCmdlineFix CmdlineFix(&argc, &argv);
bool Silent = false; bool Silent = false;