Solved

Text from clipboard to char array

Posted on 2004-09-14
2
217 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

792 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