January 24, 2020, 01:00:05 am


IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!

Copy to Clipboard

Started by billhsln, June 17, 2019, 11:37:21 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


June 17, 2019, 11:37:21 am Last Edit: June 17, 2019, 12:06:01 pm by billhsln
Either I am doing some thing wrong or there is a problem with the subroutine CopyStringToClipboard:

Code Select
SUB CopyStringToClipboard(INT hwnd, string text)
UINT handle =  GlobalAlloc(GHND, LEN(text) + 1)
POINTER lock = GlobalLock(handle)
memcpy(lock, text, LEN(text) + 1)
SetClipboardData(CF_TEXT, handle)
GlobalFree(handle) 'note: this line is not present in any example at all but I'm pretty sure that its absence will produce a memory leak :/

I am sending it the dialog d1.hwnd and an ISTRING [1024] value.  It does copy to the clipboard some times and some times it does not.  Just wondering if any one else has had the problem.  In the program I am using it in, I have it copy once, then do a little more stuff and then try to copy again.  It is the second copy that does not work.

When all else fails, get a bigger hammer.



Remove this line.....

GlobalFree(handle) - that's the problem, remove it and everything should be fine.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.


I did some digging and Andy's right... if the call to SetClipboardData succeeds the system calls GlobalFree(handle) for you, but you MUST pass a valid window handle (hwnd).

If you are still having problems, try the opposite, call CopyStringToClipboard with a null handle (hwnd = 0) and replace GHND by GMEM_MOVEABLE.

CF_TEXT should work for strings only. If you are using wstring stuff change it to CF_UNICODETEXT.


Andy's suggestion worked and the program works as I want it to.

Thanks to both Andy and fasecero.  I will also keep the info on the WSTRING, even though I have not yet found a need for it.

When all else fails, get a bigger hammer.


Quote from: billhslnI will also keep the info on the WSTRING, even though I have not yet found a need for it.

If such case takes place the allocated memory size would need to be updated to

Code Select
2 * LEN(text) + 2