Skip to content

Commit

Permalink
[win] Impersonate anonymous token when closing Clipboard (fix #71) (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberrich authored Apr 22, 2024
1 parent 42e0d7c commit ab084ce
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion clip_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class Hglobal {
HGLOBAL m_handle;
};

class AnonymousTokenImpersonator {
public:
AnonymousTokenImpersonator()
: must_revert(ImpersonateAnonymousToken(GetCurrentThread()))
{}

~AnonymousTokenImpersonator() {
if (must_revert)
RevertToSelf();
}
private:
const bool must_revert;
};

}

lock::impl::impl(void* hwnd) : m_locked(false) {
Expand All @@ -72,8 +86,10 @@ lock::impl::impl(void* hwnd) : m_locked(false) {
}

lock::impl::~impl() {
if (m_locked)
if (m_locked) {
AnonymousTokenImpersonator guard{};
CloseClipboard();
}
}

bool lock::impl::clear() {
Expand Down

0 comments on commit ab084ce

Please sign in to comment.