Solved

Text from clipboard to char array

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

778 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