MCPcopy Create free account
hub / github.com/NPP-JSONViewer/JSON-Viewer / CopyToClipboard

Method CopyToClipboard

src/UtilityLib/Utility.cpp:230–281  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

228}
229
230bool CUtility::CopyToClipboard(const std::wstring& str2cpy, HWND hWnd)
231{
232 size_t len2Allocate = (str2cpy.size() + 1) * sizeof(TCHAR);
233 HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len2Allocate);
234 if (hglbCopy == NULL)
235 {
236 return false;
237 }
238
239 if (!::OpenClipboard(hWnd))
240 {
241 ::GlobalFree(hglbCopy);
242 ::CloseClipboard();
243 return false;
244 }
245
246 if (!::EmptyClipboard())
247 {
248 ::GlobalFree(hglbCopy);
249 ::CloseClipboard();
250 return false;
251 }
252
253 // Lock the handle and copy the text to the buffer.
254 wchar_t* pStr = reinterpret_cast<wchar_t*>(::GlobalLock(hglbCopy));
255 if (pStr == NULL)
256 {
257 ::GlobalUnlock(hglbCopy);
258 ::GlobalFree(hglbCopy);
259 ::CloseClipboard();
260 return false;
261 }
262
263 wcscpy_s(pStr, len2Allocate / sizeof(wchar_t), str2cpy.c_str());
264 ::GlobalUnlock(hglbCopy);
265
266 // Place the handle on the clipboard.
267 unsigned int clipBoardFormat = CF_UNICODETEXT;
268 if (::SetClipboardData(clipBoardFormat, hglbCopy) == NULL)
269 {
270 ::GlobalFree(hglbCopy);
271 ::CloseClipboard();
272 return false;
273 }
274
275 if (!::CloseClipboard())
276 {
277 return false;
278 }
279
280 return true;
281}
282
283bool CUtility::IsNumber(const std::wstring& str)
284{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected