Last active
March 7, 2018 11:48
-
-
Save DBJDBJ/e41519bcf5a9dac115e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/* | |
Copyright 2006 - 2018 [email protected] | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http ://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
/* set to 1 if using com */ | |
#define DBJCOM 0 | |
#define WIN32_LEAN_AND_MEAN | |
#define VC_EXTRALEAN | |
#include <Windows.h> | |
#include <combaseapi.h> | |
namespace dbj { | |
namespace win { | |
#if DBJCOM | |
namespace com { | |
namespace { | |
/* | |
In anonymous namespace we hide the auto-initializer | |
This ensures that COM is initialized “as soon as possible” | |
This mechanism really works. Convince yourself once through the | |
debugger, and then just forget about COM init/uninit. | |
*/ | |
struct __declspec(novtable) | |
COMAUTOINIT | |
{ | |
unsigned int & counter() | |
{ | |
static unsigned int counter_ = 0; | |
return counter_; | |
} | |
/* | |
If you call ::CoInitialize(NULL), after this method is used | |
most likely the HRESULT will be : | |
hRes = 0×80010106 — Cannot change thread mode after it is set. | |
*/ | |
COMAUTOINIT() | |
{ | |
const UINT & ctr = (counter())++; | |
if (0 == ctr) | |
#if ( defined(_WIN32_DCOM) || defined(_ATL_FREE_THREADED)) | |
HRESULT result = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); | |
#else | |
HRESULT result = ::CoInitialize(NULL); | |
#endif | |
/*TODO: log the result here*/ | |
} | |
~COMAUTOINIT() | |
{ | |
const UINT ctr = --(counter()); | |
if (ctr < 1) | |
::CoUninitialize(); | |
} | |
}; | |
static const COMAUTOINIT wtlcomautoinit__{}; | |
} // anonspace | |
} // com | |
} // win | |
} // dbj | |
#endif // DBJCOM | |
/* | |
If problems with them, one can undef the macros defined in here | |
#undef DBJINL // static __forceinline | |
#undef WIN32_LEAN_AND_MEAN | |
#undef VC_EXTRALEAN | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2017 Mar. Update