Solved

Text from clipboard to char array

Posted on 2004-09-14
2
213 Views
Last Modified: 2010-04-15
I want to grab a string from the clipboard in Windows and copy it to a char array for further processing.  If some other data is on the clipboard, I wouldn't want that returned (such as a picture, file, etc)...  just a simple "if there is text on the clipboard, copy it to my variable".

I'm going to have users copy text to the clipboard from a travel website and then process that data in my application.

Does anyone have any example code for this simple task of moving text from the clipboard to a variable?  I'm using Visual C++ and no MFC.
0
Comment
Question by:jimstar
2 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 12061634
Please check the MSDN pages about the Clipboard functions what you want
is OpenClipboard and GetClipboardData for checking the content type of the clipboard
you want to check the CF_XXXX constants.

you'll find examples about the Clipboard e.g here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/dataexchange/clipboard.asp

Regard
Friedrich
0
 
LVL 7

Accepted Solution

by:
aib_42 earned 250 total points
ID: 12061914
It's easy:
OpenClipboard(NULL);
h = GetClipboardData(CF_TEXT);
CloseClipboard();

If h is != NULL, you have text in the clipboard and h is pointing to it. I guess you would do something like this afterwards:

char *mystorage;
if (h != NULL) {
  mystorage = malloc(strlen(h)+1);
  if (mystorage != NULL) {
    strcpy(mystorage, h);
  } else {
    /* error allocating memory */
  }
} else {
  /* clipboard doesn't contain text, or is empty */
}
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now