I am trying to get my application to copy a character array to the clipboard so it can be pasted into an IE address bar. I am having an issue with getting this working. This is the code I am working with:
HGLOBAL glob = GlobalAlloc(GMEM_FIXED,32);
memcpy(glob,array,sizeof(array));
OpenClipboard(hDlg);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT,glob);
CloseClipboard();
Array is declared as:
char array[500];
This will cause the program to crash. However if I switch out sizeof(array) with a number it ok but The only 8 characters are copyied to the clipboard.
Can anyone advise me on how to solve this issue? I am targeting the Win32 API directly, not using MFC.
You are only allocating 32 bytes of global memory:
…and then trying to cram 500 bytes in to a 32 byte bag:
Change the GlobalAlloc to:
Further, you are pasting the data as Unicode text (
CF_UNICODETEXT), but it’s not Unicode text. I would imagine that would cause… problems.Paste it as plain text instead: