Created
April 17, 2020 10:30
-
-
Save ArunTS96/eda897c5ad0400bba16571402a7e7edd to your computer and use it in GitHub Desktop.
C++ wrapper around windows Events.
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
#define USERMSG(format,...) printf("[INFO]\t"##format##"\n", __VA_ARGS__) | |
#define ERRMSG(format,...) printf("[ERROR]\t"##format##"\n", __VA_ARGS__) | |
#define DEBUGMSG(format,...) printf("[DEBUG]\t"##format##"\n", __VA_ARGS__) | |
#define EMPTYMSG printf; | |
#define INSIDE_FUNCTION USERMSG("@@@@@ \t Inside %s() [%s](%d) \t @@@@@\n", __FUNCTION__, __FILE__, __LINE__) | |
#define DINSIDE_FUNCTION DEBUGMSG("@@@@@ \t Inside %s() [%s](%d) \t @@@@@\n", __FUNCTION__, __FILE__, __LINE__) | |
#define EXITING_FUNCTION DEBUGMSG("@@@@@ \t Exiting %s() [%s](%d) \t @@@@@\n", __FUNCTION__, __FILE__, __LINE__) | |
#define UEXITING_FUNCTION USERMSG("@@@@@ \t Exiting %s() [%s](%d) \t @@@@@\n", __FUNCTION__, __FILE__, __LINE__) | |
#define STANDARD_GET_LAST_ERROR(X) ERRMSG("Failed while using function name %s with error %lu\n", X, GetLastError()) | |
#define RETURN_BASED_ERROR(X, Y) ERRMSG("Failed while using function name %s with error %lu\n", X, Y) | |
#define DEBUG_FILE_LINE LOG(plog::info) << __FILE__ << " : " << __FUNCTION__ << " : " << __LINE__ << "\n" | |
class OpenEventWrapper | |
{ | |
private: | |
HANDLE event_; | |
bool eventObjectPresent_; | |
std::wstring eventName_; | |
std::string sEventName_; | |
public: | |
explicit OpenEventWrapper(std::wstring eventName); | |
explicit OpenEventWrapper(std::string eventName); | |
bool IsEventObjectPresent() const; | |
HANDLE GetEventHandle() const; | |
bool IsEventStillPresent(); | |
bool SetEvent(); | |
~OpenEventWrapper(); | |
}; | |
OpenEventWrapper::OpenEventWrapper(const std::wstring eventName) | |
{ | |
this->event_ = nullptr; | |
this->eventObjectPresent_ = false; | |
this->eventName_ = eventName; | |
this->event_ = OpenEventW(EVENT_ALL_ACCESS, FALSE, eventName.c_str()); | |
if(this->event_ == nullptr) | |
{ | |
STANDARD_GET_LAST_ERROR("OpenEventW"); | |
if(GetLastError() == ERROR_ACCESS_DENIED) | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
} | |
else | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
} | |
OpenEventWrapper::OpenEventWrapper(const std::string eventName) | |
{ | |
this->event_ = nullptr; | |
this->eventObjectPresent_ = false; | |
this->sEventName_ = eventName; | |
this->event_ = OpenEventA(EVENT_ALL_ACCESS, FALSE, eventName.c_str()); | |
if (this->event_ == nullptr) | |
{ | |
STANDARD_GET_LAST_ERROR("OpenEventW"); | |
if (GetLastError() == ERROR_ACCESS_DENIED) | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
} | |
else | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
} | |
bool OpenEventWrapper::IsEventObjectPresent() const | |
{ | |
return this->eventObjectPresent_; | |
} | |
HANDLE OpenEventWrapper::GetEventHandle() const | |
{ | |
return this->event_; | |
} | |
bool OpenEventWrapper::IsEventStillPresent() | |
{ | |
if (this->event_ != nullptr) | |
{ | |
CloseHandle(this->event_); | |
} | |
this->event_ = OpenEventW(EVENT_ALL_ACCESS, FALSE, this->eventName_.c_str()); | |
if (this->event_ == nullptr) | |
{ | |
STANDARD_GET_LAST_ERROR("OpenEventW"); | |
if (GetLastError() == ERROR_ACCESS_DENIED) | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
} | |
else | |
{ | |
this->eventObjectPresent_ = true; | |
} | |
return this->eventObjectPresent_; | |
} | |
bool OpenEventWrapper::SetEvent() | |
{ | |
auto ret = true; | |
if(!::SetEvent(this->event_)) | |
{ | |
STANDARD_GET_LAST_ERROR("SetEvent"); | |
ret = false; | |
} | |
return ret; | |
} | |
OpenEventWrapper::~OpenEventWrapper() | |
{ | |
if (this->event_ != nullptr) | |
{ | |
CloseHandle(this->event_); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment