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);
    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))
    // Empty the Clipboard. This also has the effect
    // of allowing Windows to free the memory associated
    // with any data that is in the Clipboard

    // 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. 
    // Now, set the Clipboard data by specifying that 
    // ANSI text is being used and passing the handle to
    // the global memory.
    // 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.

(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)

(I didn't remember where I got that clipboard code, but a quick Google indicates it came from Tom Archer at CodeProject )


soal said...

According to the docs, you need to alloc the clipboard mem as GMEM_MOVEABLE. DDESHARE is ignored in modern windows. I'm surprised it works at all, but MS does love the compatibility shims.

cbloom said...

That's what I get for copy-pasting code! Anyway it works just fine with DDESHARE , but sure use MOVEABLE instead.

As much as we love to hate on MS and their compatibility nightmare, it's pretty damn nice that my windows code from 10-15 years ago still works fine for the most part.

Tom said...

I solved the VS command line annoyingness by writing some macros, and it sounds like it's time for my once-yearly attempt to plug them to somebody else ;)


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.

old rants