Thread: How to Get Clipboard Data as a String in C

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Post How to Get Clipboard Data as a String in C

    Hello, I need to get data from the clipboard as a string, so that I can traverse the string and do work on possibly each character of the string.

    Here is the code I have:

    Code:
    int main()
    {
        OpenClipboard(NULL);
        char *str;
        HANDLE hData;
        hData = GetClipboardData(CF_TEXT);
        str = (char*)GlobalLock(hData);
        CloseClipboard();
    }
    The problem is, when I later try to access the character at str[0], the program crashes.

    Thanks for your help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    I guess the first thing to do is add some error checking and diagnostic messages to find out exactly where it's failing and for what reason.
    GlobalLock function (Windows)

    Blindly stumbling from one function to another without any regard for success or failure is just going to crash and burn at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying data to clipboard, linux, c
    By krisinho in forum C Programming
    Replies: 4
    Last Post: 06-06-2014, 04:27 PM
  2. help "Copy String to Clipboard"
    By haibuithe in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2012, 10:25 PM
  3. Get Clipboard Text as String
    By n00b3 in forum Windows Programming
    Replies: 5
    Last Post: 06-10-2008, 05:09 AM
  4. Pasting data from clipboard
    By dean11 in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 05:48 PM
  5. Clipboard data in MDI
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-19-2004, 10:24 PM

Tags for this Thread