Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@

#include <string>

class ScopedCOMInit final // never use this in DllMain
{
public:
ScopedCOMInit() {
HRESULT hr = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); // attempt STA init 1st (older CoInitialize(NULL))

if (hr == RPC_E_CHANGED_MODE)
{
hr = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); // STA init failed, switch to MTA
}

if (SUCCEEDED(hr))
{
// S_OK or S_FALSE, both needs subsequent CoUninitialize()
_bInitialized = true;
}
}

~ScopedCOMInit() {
if (_bInitialized)
{
_bInitialized = false;
::CoUninitialize();
}
}

bool isInitialized() const {
return _bInitialized;
}

private:
bool _bInitialized = false;

ScopedCOMInit(const ScopedCOMInit&) = delete;
ScopedCOMInit& operator=(const ScopedCOMInit&) = delete;
};

void expandEnv(std::wstring& s);
std::wstring getDateTimeStrFrom(const std::wstring& dateTimeFormat, const SYSTEMTIME& st);
void writeLog(const wchar_t* logFileName, const wchar_t* logSuffix, const wchar_t* log2write);
Expand All @@ -27,4 +64,4 @@ std::string getFileContentA(const char* file2read);
std::wstring GetLastErrorAsString(DWORD errorCode);
std::wstring stringToUpper(std::wstring strToConvert);
std::wstring stringToLower(std::wstring strToConvert);
std::wstring stringReplace(std::wstring subject, const std::wstring& search, const std::wstring& replace);
std::wstring stringReplace(std::wstring subject, const std::wstring& search, const std::wstring& replace);
Loading