Solved

Text from clipboard to char array

Posted on 2004-09-14
2
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and reading files in the C programming language.

728 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