MCPcopy Create free account
hub / github.com/DiscordMessenger/dm / CopyStringToClipboard

Function CopyStringToClipboard

src/windows/WinUtils.cpp:199–241  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

197}
198
199void CopyStringToClipboard(const std::string& str)
200{
201 if (OpenClipboard(g_Hwnd))
202 {
203 EmptyClipboard();
204
205 // This expands all of the new line characters inside str into
206 // a carriagereturn-linefeed sequence.
207 std::string str2;
208 str2.reserve(24 + str.size());
209
210 for (size_t i = 0; i < str.size(); i++)
211 {
212 if (str[i] == '\n' && (i == 0 || str[i] != '\r'))
213 str2.push_back('\r');
214 str2.push_back(str[i]);
215 }
216
217 LPCTSTR ctstr = ConvertCppStringToTString(str2);
218
219 size_t stringSize = (str.length() + 1) * sizeof(TCHAR);
220
221 // create a global buffer and fill its text in
222 HGLOBAL clipbuffer;
223 char* buffer;
224 clipbuffer = GlobalAlloc(GMEM_DDESHARE, stringSize);
225 buffer = (char*)GlobalLock(clipbuffer);
226 memcpy(buffer, ctstr, stringSize);
227 GlobalUnlock(clipbuffer);
228
229 // set the clipboard data
230#if UNICODE
231 SetClipboardData(CF_UNICODETEXT, clipbuffer);
232#else
233 SetClipboardData(CF_TEXT, clipbuffer);
234#endif
235
236 // release everything we have
237 CloseClipboard();
238
239 free((void*)ctstr);
240 }
241}
242
243// Borrowed from NanoShellOS: https://github.com/iProgramMC/NanoShellOS/blob/master/src/utf8.c
244#define REPLACEMENT_CHAR 0xFFFD

Callers 2

WndProcMethod · 0.85

Calls 4

reserveMethod · 0.45
sizeMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected