2/23/2011

02-23-11 - Some little coder things - Clip

I wrote a little app called "clip" that pastes its args to the clipboard. It turns out to be very handy. For example
it's a nice way to get a file name from my DOS box into some other place, because DOS does arg completion, I can
just type "clip f - tab" and get the name.

The other big place its been useful is copying command lines to the MSVC
debugger property sheet, and turning command lines into batch files.

Clip is obviously trivial, the entire code is :

void CopyStringToClipboard(const char * str);
int main(int argc,const char *argv[])
{
String out;
for(int argi=1;argi < argc;argi++)
{
if ( argi > 1 ) out += " ";
out += argv[argi];
}
lprintf("clip : \"%s\"\n",out);
CopyStringToClipboard(out.CStr());
return 0;
}
void CopyStringToClipboard(const char * str)
{
// test to see if we can open the clipboard first before
// wasting any cycles with the memory allocation
if ( ! OpenClipboard(NULL))
return;
// Empty the Clipboard. This also has the effect
// of allowing Windows to free the memory associated
// with any data that is in the Clipboard
EmptyClipboard();
// Ok. We have the Clipboard locked and it's empty.
// Now let's allocate the global memory for our data.
// Here I'm simply using the GlobalAlloc function to
// allocate a block of data equal to the text in the
// "to clipboard" edit control plus one character for the
// terminating null character required when sending
// ANSI text to the Clipboard.
HGLOBAL hClipboardData;
hClipboardData = GlobalAlloc(GMEM_DDESHARE,strlen(str)+1);
// Calling GlobalLock returns to me a pointer to the
// data associated with the handle returned from
// GlobalAlloc
char * pchData;
pchData = (char*)GlobalLock(hClipboardData);
// At this point, all I need to do is use the standard
// C/C++ strcpy function to copy the data from the local
// variable to the global memory.
strcpy(pchData, str);
// Once done, I unlock the memory - remember you
// don't call GlobalFree because Windows will free the
// memory automatically when EmptyClipboard is next
// called.
GlobalUnlock(hClipboardData);
// Now, set the Clipboard data by specifying that
// ANSI text is being used and passing the handle to
// the global memory.
SetClipboardData(CF_TEXT,hClipboardData);
// Finally, when finished I simply close the Clipboard
// which has the effect of unlocking it so that other
// applications can examine or modify its contents.
CloseClipboard();
}

(BTW note that the lprintf of my string class in main is not a bug - that's an autoprintf which handles everything magically
and fantastically)

The officially approved bindings are Alt+C for SetCmdLine, and Shift+Alt+C for AppendToCmdLine, but whatever.

Highlight text in the text editor, and use the above as appropriate to build the command line. I usually end up with a text file in each solution that's got a bunch of file names and command line snippets in it, and assemble the command line from those parts depending on what I'm testing.