Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Text from clipboard to char array

Posted on 2004-09-14
2
Medium Priority
?
221 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

886 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