Author Topic: Simple way to copy string to the clipboard ?  (Read 1011 times)


  • Guest
Simple way to copy string to the clipboard ?
« on: May 24, 2020, 08:38:48 AM »
I just started coding again, after like 25 years  :) .

I stumbled on Pelles C, and started coding in C.

I would like to copy a string to the clipboard. Is there any simple way to do that - like a user defined function or library ?
Or would I have to write it myself ?


Offline John Z

  • Member
  • *
  • Posts: 566
Re: Simple way to copy string to the clipboard ?
« Reply #1 on: May 24, 2020, 11:09:29 AM »
Yes special functions AND you need to do some work too....
Here is a reference and some code that I used in a Pelles C project. 
This version is for Unicode so if you want just plain text you'll figure that out.
// modified from

Code: [Select]

        // string to send to the clipboard is pointed to by p_data
strsize = wcslen(p_data) * sizeof(wchar_t);
// test to see if we can open the clipboard first
if (OpenClipboard(gHWND))
// Empty the Clipboard and free the memory

// allocate a block of data equal to the text
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE, strsize + 2);

// GlobalLock returns a pointer to the
// data associated with the handle returned from
// GlobalAlloc
wchar_t *pchData;
pchData = (wchar_t *)GlobalLock(hClipboardData);

// wide strcpy function to copy the data from the local
// variable to the global memory.
wcscpy(pchData, p_data);

// unlock the memory

// Set the Clipboard data by specifying that
// unicode text is being used and pass the handle to
// handle to the global memory.
SetClipboardData(CF_UNICODETEXT, hClipboardData);

// close the Clipboard

Hope this gets you started. You still need to do some work around it of course. ;)



  • Guest
Re: Simple way to copy string to the clipboard ?
« Reply #2 on: May 24, 2020, 12:05:06 PM »

There's no 'Like'/'Thanks' button on this forum?

I just copied the code from SetClipboardText here :

works ok.
« Last Edit: May 24, 2020, 04:39:18 PM by gbr »