question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NativeClipboard.SetData related API takes no effect

See original GitHub issue

Describe the bug and how to reproduce Just like the code what I provided below

What code is involved

using (NativeClipboard Clipboard = new NativeClipboard(true))
{
       Clipboard.SetText("12333333");
}

//Check system clipboard: No content like "12333333"

Expected behavior Looks like the content did not write down to clipboard

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:47 (47 by maintainers)

github_iconTop GitHub Comments

1reaction
zhuxb711commented, Jan 29, 2023

It looks great, thanks for your work. The last suggestion is

Design the API like this: NativeClipboard.CreateIDataObjectFromShellItems(params ShellItem[] Items)

if Items.Length == 0, just call NativeClipboard.CreateEmptyIDataObject()

It makes the user easier to use the API if they want to set ShellItems (even they passed an empty array)

0reactions
dahallcommented, Feb 5, 2023

I hate loose ends, so today I wrote the following and confirmed that something is wrong with OleSetClipboard.

HRESULT hr = OleInitialize(NULL);

auto id0 = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
auto id = RegisterClipboardFormat(CFSTR_FILECONTENTS);

IDataObject* pDataObj;
if (SUCCEEDED(hr = SHCreateDataObject(NULL, 0, NULL, NULL, IID_PPV_ARGS(&pDataObj))))
{
	FORMATETC fmt0 = { id0, NULL, DVASPECT::DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
	FILEDESCRIPTORW fd0 = { FD_FILESIZE };
	//fd0.nFileSizeLow = 445;
	lstrcpy(fd0.cFileName, L"C:\\Temp\\test.txt");
	auto hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(fd0));
	auto ptr = GlobalLock(hMem);
	memcpy(ptr, &fd0, sizeof(fd0));
	GlobalUnlock(hMem);
	STGMEDIUM medium0 = { TYMED_HGLOBAL };
	medium0.hGlobal = hMem;
	hr = pDataObj->SetData(&fmt0, &medium0, TRUE);

	FORMATETC fmt = { id, NULL, DVASPECT::DVASPECT_CONTENT, 0, TYMED_ISTREAM };
	STGMEDIUM medium = { TYMED_ISTREAM };
	IStream* pStream;
	if (SUCCEEDED(hr = SHCreateStreamOnFileW(L"C:\\Temp\\test.txt", STGM_READWRITE, &pStream)))
	{
		medium.pstm = pStream;
		if (SUCCEEDED(hr = pDataObj->SetData(&fmt, &medium, TRUE)))
		{
			hr = OleSetClipboard(pDataObj);
			hr = OleFlushClipboard();
		}
		pStream->Release();
	}
	pDataObj->Release();
	pDataObj = NULL;
}

if (SUCCEEDED(hr = OleGetClipboard(&pDataObj)))
{
	FORMATETC fmt = { id, NULL, DVASPECT::DVASPECT_CONTENT, 0, TYMED_ISTREAM | TYMED_HGLOBAL | TYMED_ISTORAGE };
	hr = pDataObj->QueryGetData(&fmt);
	std::cout << "HR = " << hr << std::endl; // This returns the same FORMATETC error as C#
}

OleUninitialize();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Clipboard API and events
The paste action has no effect in a non-editable context, but the paste event fires regardless. The paste event bubbles, is cancelable, ...
Read more >
copy to clipboard - not working in FF,Chrome
Internet Explorer seems to work with window.clipboardData.setData instead, but keep in mind that the IE clipboard will only accept 'text' and ' ...
Read more >
copy is not successful NotAllowedError: The Clipboard API ...
While testing log copying behaviour in VSCode, I noticed that the copy button is no longer working, when using it in the context...
Read more >
Clarification for clipboardData.setData() and native access
Writing to the clipboard currently states: These data types must be placed on the clipboard with a corresponding native type description if ...
Read more >
How to use the Synchronous Clipboard API without native ...
As the clipboardData only is available during the event, I rely on Native code to do side effects: Html.div [ Html.Events.onWithOptions "copy" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found