March 28, 2024, 04:22:35 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


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.

billhsln

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:

SUB CopyStringToClipboard(INT hwnd, string text)
UINT handle =  GlobalAlloc(GHND, LEN(text) + 1)
POINTER lock = GlobalLock(handle)
memcpy(lock, text, LEN(text) + 1)
GlobalUnlock(handle)
OpenClipboard(hwnd)
EmptyClipboard()
SetClipboardData(CF_TEXT, handle)
CloseClipboard()
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 :/
ENDSUB

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.

Thanks,
Bill
When all else fails, get a bigger hammer.

Andy

Bill,

Remove this line.....

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

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

fasecero

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.

billhsln

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.

Bill
When all else fails, get a bigger hammer.

fasecero

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

2 * LEN(text) + 2