NO

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

gbr

  • Guest
Simple way to copy string to the clipboard ?
« on: May 24, 2020, 08:38:48 AM »
Hi,
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 ?

Thanks.

Offline John Z

  • Member
  • *
  • Posts: 862
Re: Simple way to copy string to the clipboard ?
« Reply #1 on: May 24, 2020, 11:09:29 AM »
Hi,
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.
Reference:
// modified from https://www.codeproject.com/Articles/2242/Using-the-Clipboard-Part-I-Transferring-Simple-Tex

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
EmptyClipboard();

// 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
GlobalUnlock(hClipboardData);

// 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
CloseClipboard();
            }

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

Regards

gbr

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

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

EDIT :
I just copied the code from SetClipboardText here :

https://cboard.cprogramming.com/windows-programming/57178-copying-clipboard-post400729.html#post400729

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