[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Copying text to clipboard from within Windows console application:

I have a simple console application which searches through a file for a character string.  I want to copy this string to the clipboard ( I believe this is the correct place) in order that I can paste the string somewhere later.  Could someone provide an example of how to do this?

  • 2
1 Solution
Jaime OlivaresCommented:
Writing from my mind (not tested):

CopyTextToClipboard(char *text)
    HGLOBAL h;
    char *lock;

    h = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE , strlen(text)+1);
    lock = (char *)GlobalLock(h);
    strcpy(lock, text);
    SetClipboardData(CF_TEXT, h);
mrdtnAuthor Commented:
OpenClipboard() required an argument.  I used NULL since it was a console app.

I did see some strange behavior.  The VC++ editor would always paste the correct string, but other apps would paste some previous string.  I added a call to EmptyClipboard, and it seems to be consistent now.  However, I am still confused as to why it was behaving the way it was.

If you can explain what is/was going on, it would be very helpful.

Anyway, thanks for the quick solution.

Jaime OlivaresCommented:
>The VC++ editor would always paste the correct string, but other apps would paste some previous string
I think this occur when an application copies many version of a string (plain text, formatted text, html text), so you are replacing only plain text version (by example, when copying from MS-Word), but try with Notepad, maybe this doesn't occur.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now