This is a quick little C program which demonstrates how to read data from
the Windows clipboard. If you use the Windows command line, it's a
handy utility to have around. It works with Cygwin.
See Writing to the Windows Clipboard for a symmetrical utility which
copies text to the clipboard from standard input. Between the two, you
could, for example, write a Perl script that would do stuff to text from the
clipboard and then copy the finished product back to the clipboard.
If you have any problems with this, please let me know. If you actually find
it useful or informative, for God's sake let me know!
/*****************************************************************************
* cpaste.c
* Simple program to cat text (if any) from Win32 clipboard to stdout
*
* You'll have to link this to user32.lib (or libuser32.a with cygwin)
*
* I've built this with MSVC++6 and EGCS 2.91.57
*
* wharfinger 1/17/01
*****************************************************************************/
#include <stdio.h>
#include <windows.h>
#define HASSLE_USER "\
usage: Run it. If there's any text in the clipboard, that'll be printed to\n\
standard output. Otherwise, nothing will happen at all.\n\
\n\
This program was written by wharfinger@hotmail.com; no warranty blah\n\
blah blah, public domain etc.\n"
/* Get text from clipboard and copy into string. Return size of string. */
int cbtostr( char **s );
/*---------------------------------------------------------------------------*/
int main( int argc, char *argv[] )
{
/* There aren't any arguments. If the user tries to give us one, s/he's
* obviously disoriented and in need of help. Sadly, we can't help. We
* can't even help ourselves, much less some random user out there in the
* darkness. All we can do is explain what this stupid program does. So
* that's what we do.
*/
if ( argc > 1 ) {
fprintf( stderr, HASSLE_USER );
} else {
char * s = NULL;
/* If we can get the clipboard data in a string, we'll spew it out;
* otherwise, we'll just float on by and return nonzero below.
*/
if ( cbtostr( &s ) ) {
puts( s );
free( s );
return 0;
}
}
return 1;
}
/*---------------------------------------------------------------------------*/
int cbtostr( char **s )
{
if ( ! s )
return 0;
else if ( ! IsClipboardFormatAvailable( CF_TEXT ) )
/* If there's nothing there, we'd look mighty silly trying to get our
* grimly little hands on it, no?
*/
return 0;
else {
HGLOBAL glob;
char * cbtext;
size_t size;
OpenClipboard( NULL ); /* Argument is HWND; we don't have one */
/* CF_TEXT is an int constant which specifies that we're looking for
* text data -- or at least data that somebody *described* as text.
* It's no skin off my ass either way. The clipboard can hold
* different data for each of an arbitrary number of different
* "formats", all at the same time.
*/
glob = GetClipboardData( CF_TEXT );
size = GlobalSize( glob );
/* We're given a "handle" to global memory containing the clipboard
* data. We have to call GlobalLock() to get a pointer to the
* memory that the handle refers to.
*/
if ( ( cbtext = (char *)GlobalLock( glob ) )
&& ( *s = (char *)malloc( size + 1 ) ) )
{
strcpy( *s, cbtext );
}
/* This is good manners. It'll get closed when our process exits
* anyway, and that'll be happening very, very soon -- but this is an
* example program, so we'll do the right thing.
*/
CloseClipboard();
return (*cbtext) ? size : 0;
}
}
/*****************************************************************************/
/*****************************************************************************/