Clipboard
- OleGetClipboard function (ole2.h) - Win32 apps | Microsoft Learn
- OleIsCurrentClipboard function (ole2.h) - Win32 apps | Microsoft Learn
void PrintClipboardContents() {
IDataObject* pDataObject = nullptr;
HRESULT hr = OleGetClipboard(&pDataObject);
if (SUCCEEDED(hr)) {
FORMATETC fmtetc = { CF_UNICODETEXT, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
if (OleIsCurrentClipboard(pDataObject)) {
STGMEDIUM stgmedium;
// Retrieve the data
hr = pDataObject->GetData(&fmtetc, &stgmedium);
if (SUCCEEDED(hr)) {
wchar_t* data = static_cast<wchar_t*>(GlobalLock(stgmedium.hGlobal));
if (data != nullptr) {
std::wcout << L"Clipboard Contents:" << std::endl;
std::wcout << data << std::endl;
GlobalUnlock(stgmedium.hGlobal);
}
ReleaseStgMedium(&stgmedium);
}
}
pDataObject->Release();
}
}
Write to Clipboard
- OleSetClipboard function (ole2.h) - Win32 apps | Microsoft Learn
- OleFlushClipboard function (ole2.h) - Win32 apps | Microsoft Learn
Alternatively, you can use the clipboard API
- OpenClipboard function (winuser.h) - Win32 apps | Microsoft Learn
- EmptyClipboard function (winuser.h) - Win32 apps | Microsoft Learn
- SetClipboardData function (winuser.h) - Win32 apps | Microsoft Learn
- CloseClipboard function (winuser.h) - Win32 apps | Microsoft Learn