|
// Test 6.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
// Open the clipboard
if( FALSE == OpenClipboard( NULL ) )
{ return false; }
// Empty Data...
EmptyClipboard();
HGLOBAL hClipboardText = NULL;
std::wstring text = L"Albert Einstein said, Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.";
// Allocate memory for the text
hClipboardText = GlobalAlloc(GMEM_DDESHARE,
(text.length() + 1) * sizeof( wchar_t ) );
if( NULL == hClipboardText )
{
CloseClipboard();
return -1;
}
wchar_t* pClipboardText = static_cast<wchar_t*>( GlobalLock( hClipboardText ) );
try
{
memcpy( pClipboardText, text.c_str(), text.length() * sizeof( wchar_t ) );
// Assure NULL termination
pClipboardText[ text.length() ] = L'\0';
}
catch( ... )
{
CloseClipboard();
return -1;
}
std::wcout << L"Clipboard Text:" << std::endl << text << std::endl;
GlobalUnlock( hClipboardText );
// Place the text on the clipboard.
bool result = ( FALSE != SetClipboardData( CF_UNICODETEXT, hClipboardText ) );
CloseClipboard();
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.