I'm developing some software which copies a large string to the windows clipboard to paste into some other software. Pasting in the other software does not work, and when I paste into Notepad, the newlines in the initial strings are gone, which is why it is failing to paste in the other software. I know this because when I re-add the newlines to Notepad, and do a Copy, Pasting then works in the other program. When I paste into Wordpad, the newlines are there mysteriously.
I'm using SetClipboardData() in C++ with the CF_TEXT clipboard format type. I've tried using CF_OEMTEXT, CF_DSPTEXT but neither of those work. I saw some documentation on CF_SYLK (Symbolic Link) for spreadsheets, as the software I'm pasting in is similar to a spreadsheet, but I couldn't get that to work either. Below is my code for copying to the clipboard.
void ClipBoardManager::CopyExcelStringToClipBoard(std::string excel_str)
{
OpenClipboard(nullptr);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, excel_str.size() + 1);
if (!hg) {
CloseClipboard();
return;
}
memcpy(GlobalLock(hg), excel_str.c_str(), excel_str.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
}
Any help is appreciated.
The
excel_strmust have the CRLF line endings. Here is example code to convert string to the good format: