I have a strange problem trying to read from the clipboard for C or C++ programs I write using Visual Studio (I've tried 2010, 2012 and 2013) on Windows 8.1 64 bit. The following code works fine on Window 7 64 bit but fails to read from the keyboard after a web browser (any one) has been launched on Windows 8.1:
char * ReadClipboard()
{
char * Result = 0;
if (OpenClipboard(NULL))
{
HANDLE ClipboardHandle = GetClipboardData(CF_TEXT);
if (ClipboardHandle)
{
char * Text = (char *)GlobalLock(ClipboardHandle);
if (Text)
{
size_t StringLength = strlen(Text);
Result = (char *)malloc(StringLength + 1);
strcpy(Result, Text);
}
GlobalUnlock(ClipboardHandle);
}
CloseClipboard();
}
return Result;
}
The call to GetClipboardData
returns null even when there is data on the clipboard. When the following java code is executed under the same conditions, it reads the clipboard fine:
String textData = (String) Toolkit.getDefaultToolkit().
getSystemClipboard()
.getData(DataFlavor.stringFlavor);
I have also tried reading the clipboard from C/C++ using the Ole clipboard with the same outcome - failure. Is there something I am doing wrong in the above code? Can others reproduce this problem?