-3

Quick question: in this example code:

printf ("\nType in 1st address: ");
scanf ("%x", &address1);
address1 = (address1 - number1) * 2;
printf ("\nResult = %08X\n\n", address1);

How can I copy the contents of var address1 onto the clipboard?

Cœur
  • 37,241
  • 25
  • 195
  • 267
alexstx
  • 91
  • 6

1 Answers1

0

For the future readers, I decided to show an example how to do this - in Windows.

First format the data by using sprintf (http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm) if it's not already in correct format(const char*).

Then use the following Windows API to set the clipboard data.

  • OpenClipBoard
  • EmptyClipboard
  • SetClipboardData
  • CloseClipboard
  • GetClipboardData

Here is a very basic example usage of those API's without any return/error value checking.

const char *Str = "Hello world";
const size_t strLen = lstrlenA(Str) + 1;
HGLOBAL hGlobal =  GlobalAlloc(GMEM_MOVEABLE, strLen); //Memory must be moveable!
memcpy(GlobalLock(hGlobal), Str, strLen);
GlobalUnlock(hGlobal);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, hGlobal);
CloseClipboard();
Okkaaj
  • 115
  • 8