Skip to main content
Participant
February 14, 2025
Question

Trying to add to windows clipboard from plugin using custom clipboard format

  • February 14, 2025
  • 1 reply
  • 187 views

Hi,

In the code below Im trying to save a string (in this case the value "") into a custom clipboard format. Ive looked at samples using the windows c++ sdk and their code is the same as mine but in acrobat it fails when calling SetClipboardData for the custom format. GetLastError returns zero and no exceptions are thrown when debugging in visual studio but as you can see I can read and write just fine to CF_TEXT. Ive tried lots of different values for formatName to ensure they are unique ("CSV" is just the last one I tried)

Any ideas why this could be or what else to try?

void SetCustomClipboardData(const std::string& customMetadata)
{
	HWND hwnd = WinAppGetModelessParent();

	if (!OpenClipboard(hwnd)) {
		std::cerr << "Failed to open clipboard." << std::endl;
	}

	const char* formatName = "CSV";
	UINT customFormat = RegisterClipboardFormatA(formatName);

	if (customFormat == 0) {
		std::cerr << "Error registering custom clipboard format." << std::endl;
		return;
	}

	// hardcoded data for testing
	const size_t len = customMetadata.size() + 1;

	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
	memcpy(GlobalLock(hMem), customMetadata.c_str(), len);
	GlobalUnlock(hMem);

	EmptyClipboard();


	// This works
	if (SetClipboardData(CF_TEXT, hMem) == NULL) {
		GlobalFree(hMem); // Only free if SetClipboardData fails.
	}

	// get data, this works
	HANDLE hData = GetClipboardData(CF_TEXT);
	if (hData != nullptr) {
		char* pszText = static_cast<char*>(GlobalLock(hData));
		if (pszText != nullptr) {
			GlobalUnlock(hData);
		}
	}
	else {
		std::cerr << "No CF_TEXT data available." << std::endl;
	}

	// NO matter what I try I cant get this to work 
	if (SetClipboardData(customFormat, hMem) == NULL) {
		DWORD err = GetLastError(); // error is zero so no detail
		LPVOID lpMsgBuf = nullptr;
		FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			err,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR)&lpMsgBuf,
			0,
			NULL);
		std::cerr << "SetClipboardData failed with error " << err
			<< ": " << static_cast<char*>(lpMsgBuf) << std::endl;
		LocalFree(lpMsgBuf);
		GlobalFree(hMem); // Only free if SetClipboardData fails.
	}
	CloseClipboard();
}

 

1 reply

Participant
February 27, 2025

guessing no one else has had issues. I have made a small ammount of progress. If I disable "protected mode" in acrobat then it all works so in their wisdom adobe must be fine with standard clipboard formats but not custom ones (cant see why one would be more insecure than the other).

Anyone got any wisdom now we roughly know what the issues is, other than disabling protected mode entirely?

Participant
February 27, 2025

I tried adding specific pdf files to the "priveledged locations" but doesnt make any difference