Thread overview
How to work Get & Set text in clipboard in Windows ?
Jun 20, 2020
Vinod K Chandran
Jun 20, 2020
Dennis
Jun 20, 2020
Vinod K Chandran
Jun 22, 2020
aberba
Jun 22, 2020
Vinod K Chandran
June 20, 2020
Hi all,
I would like to know how to get & set text in clipboard. I am using windows machine. Thanks in advance.
--Vinod Chandran
June 20, 2020
On Saturday, 20 June 2020 at 13:32:22 UTC, Vinod K Chandran wrote:
> I would like to know how to get & set text in clipboard. I am using windows machine.

This is an example of setting the clipboard using the Windows API in D:
```
/// Returns: true on success
bool setClipboard(string str) {
	import core.stdc.string: memcpy;
	import std.string: toStringz;
	import core.sys.windows.windows: GlobalAlloc, GlobalLock, GlobalUnlock,
		OpenClipboard, EmptyClipboard, SetClipboardData, CloseClipboard,
		CF_TEXT, CF_UNICODETEXT, GMEM_MOVEABLE;
		
	auto hMem = GlobalAlloc(GMEM_MOVEABLE, str.length + 1);
	if (hMem is null) {
		return false;
	}
	memcpy(GlobalLock(hMem), str.ptr, str.length);
	(cast(char*) hMem)[str.length] = '\0'; // zero terminator
	GlobalUnlock(hMem);
	if (!OpenClipboard(null)) {
		return false;
	}
	EmptyClipboard();
	if (!SetClipboardData(CF_TEXT, hMem)) {
		return false;
	}
	CloseClipboard();
	return true;
}
```

Here's an example of getting it, from arsd:
https://github.com/adamdruppe/arsd/blob/ae17d5a497de93454f58421cd9d4a5ecd70594c0/terminal.d#L2043

Here is a C example from GLFW:
https://github.com/glfw/glfw/blob/51a465ee2b50234f984efce0e229f7e9afceda9a/src/win32_window.c#L2150
June 20, 2020
On Saturday, 20 June 2020 at 13:46:05 UTC, Dennis wrote:
>
Thanks a lot. Well, i thought it should be a one liner like-
Clipboard.SetText(sText)
But after reading your reply, i realized that this is D, not a scripting language. :)


June 22, 2020
On Saturday, 20 June 2020 at 19:39:56 UTC, Vinod K Chandran wrote:
> On Saturday, 20 June 2020 at 13:46:05 UTC, Dennis wrote:
>>
> Thanks a lot. Well, i thought it should be a one liner like-
> Clipboard.SetText(sText)
> But after reading your reply, i realized that this is D, not a scripting language. :)

It would be a one-liner if it was an api. Such utility APIs are quite missing in D.

How about putting them together into a package?
June 22, 2020
On Monday, 22 June 2020 at 10:26:12 UTC, aberba wrote:

>
> It would be a one-liner if it was an api. Such utility APIs are quite missing in D.
Um, You are right.

>
> How about putting them together into a package?
Well, this is an inspiration to me. Let me try. :)