Thread: Get Clipboard Text as String

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Get Clipboard Text as String

    Hello,

    I am writing a c++ program for windows using the API. My program needs to be able to get some text from the clipboard, import it to the program's scope as a string, and do work on the string.

    [what I have tried]

    Code:
    cout << GetClipboardData(CF_TEXT) << endl;
    cout << GetClipboardData(CF_UNICODETEXT) << endl;
    Both of these printed/returned zero, which, msdn says, means the functions failed.
    I found the MSDN documentation wanting...an example would have been nice.
    http://msdn.microsoft.com/en-us/library/ms907128.aspx
    Even if the above functions returned a non-zero result, I am not sure how I would use the returned data handle to get the data as a string.

    [other ideas]
    I could post a paste message to a temporary text file, then import the text using fstream, but this is rather a backwards way of going about it.

    I am hoping someone would please shed some light on how to get text from the clipboard as a string into my c++ program.

    Thanks,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably didn't open the clipboard before you tried to access it. Here's an example that works:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    void BailOut(char *msg)
    {
      fprintf(stderr, "Exiting: %s\n", msg);
      exit(1);
    }
    
    int main()
    {
      HANDLE h;
      
      if (!OpenClipboard(NULL))
        BailOut("Can't open clipboard");
      
      h = GetClipboardData(CF_TEXT);
    
      printf("%s\n", (char *)h);
    
      CloseClipboard();
      return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    10
    You are correct - doh!

    Thanks for the rapid solution!

    Code:
     HANDLE clip;
    
        if (OpenClipboard(NULL)) {
          clip = GetClipboardData(CF_TEXT);
          CloseClipboard();
        }
    
    
        string text;
        text = (char*)clip;
        cout << (char*)clip << " same as " << text << endl;
    
        //=== - O R - ====
    
        unsigned int i = 0;
        text = "";
    
        while (((char *)clip)[i] != 0) {
          text += ((char *)clip)[i];
          i++;
        }
    
        cout << text << endl;;
    
        //=== - O R - ====
    
        text = "";
        char* pntchr = (char*)clip;
    
        while (*pntchr != 0) {
          text += *pntchr;
          pntchr++;
        }
    
        cout << text << endl;;
    all the above versions of the code output the same text. Thanks again!

    p.s. i like the bailout terminology, going to have to start using that if you don't mind.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must not forget to close the clipboard later. So long as it's open, all other applications are locked out of it and that's really annoying. That means none can use the clipboard but your app (you wouldn't be able to copy and paste, for example).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You must not forget to close the clipboard later. So long as it's open, all other applications are locked out of it and that's really annoying. That means none can use the clipboard but your app (you wouldn't be able to copy and paste, for example).
    The code posted does close the clipboard - immediately after copying the content, in fact.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Good to note anyway.
    It reminds me of a bug in Microsoft's remote desktop for XP which never closed the clipboard on occasions. I designed a program to find and identify the culprit which stole the clipboard all the time and found it to be Microsoft's remote desktop. I had to kill it to restore the clipboard. Let's not make the same mistake...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM