?
Solved

Clipboard In C

Posted on 2003-03-23
18
Medium Priority
?
368 Views
Last Modified: 2010-04-15
Hey I was wondering if there was a library/API/command for the clipboard editing: copying, pasting etc.
0
Comment
Question by:supertw2000
[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
  • 9
  • 7
  • 2
18 Comments
 

Author Comment

by:supertw2000
ID: 8193423
Could you please give an example
0
 

Expert Comment

by:loontech
ID: 8194023
0
 
LVL 10

Expert Comment

by:makerp
ID: 8194758
here is a snippet of code showing you cut and paste  

case IDM_CUT:
case IDM_COPY:
if (hText != NULL) {
   /* Allocate memory and copy the string to it */
   if (!(hData = GlobalAlloc(GMEM_DDESHARE, GlobalSize (hText)))) {
       OutOfMemory();
       return (TRUE);
   }
   if (!(lpData = GlobalLock(hData))) {
       OutOfMemory();
       return (TRUE);
   }
   if (!(lpszText = GlobalLock (hText))) {
       OutOfMemory();
       return (TRUE);
   }
   strcpy(lpData, lpszText);
   GlobalUnlock(hData);
   GlobalUnlock (hText);
/* Clear the current contents of the clipboard, and set
 * the data handle to the new string. */

   if (OpenClipboard(hWnd)) {
        EmptyClipboard();
        SetClipboardData(CF_TEXT, hData);
        CloseClipboard();
   }
   hData = NULL;
case IDM_PASTE:
   if (OpenClipboard(hWnd)) {
/* get text from the clipboard */
      if (!(hClipData = GetClipboardData(CF_TEXT))) {
          CloseClipboard();
          break;
      }
      if (hText != NULL) {
          GlobalFree(hText);
      }
      if (!(hText = GlobalAlloc(GMEM_MOVEABLE,GlobalSize(hClipData)))) {
          OutOfMemory();
          CloseClipboard();
          break;
      }
      if (!(lpClipData = GlobalLock(hClipData))) {
          OutOfMemory();
          CloseClipboard();
          break;
       }
       if (!(lpszText = GlobalLock(hText))) {
                            OutOfMemory();
                            CloseClipboard();
                            break;
       }
       strcpy(lpszText, lpClipData);
       GlobalUnlock(hClipData);
       CloseClipboard();
       GlobalUnlock(hText);
       break;
}
0
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!

 

Author Comment

by:supertw2000
ID: 8200556
USING C not C++
0
 

Expert Comment

by:loontech
ID: 8200813
the api calls that are in the link I posted are all in C.
I know just C. I haven't learned C++.
0
 

Expert Comment

by:loontech
ID: 8200843
makerp is Completely correct. and Its all in C. Not C++.
0
 

Author Comment

by:supertw2000
ID: 8207265
Could you make a program that inputs a string and copies it to the keyboard?
0
 

Expert Comment

by:loontech
ID: 8207277
keyboard?
do you mena standard in?
0
 

Author Comment

by:supertw2000
ID: 8207485
i meant clipboard
0
 

Author Comment

by:supertw2000
ID: 8207721
i meant clipboard
0
 
LVL 10

Expert Comment

by:makerp
ID: 8212609
my code shows you exactly how to do this.... whats wrong with it
0
 

Author Comment

by:supertw2000
ID: 8214487
i don't get it yours just shows a case and has no like main() or #include etc. you really have to spell it out for me. i'm kinda new and learn staright from the book so these things are hard, could you refer me to some websites that explain how you use things that don't spell everything out? thanks
0
 

Expert Comment

by:loontech
ID: 8214717
so you aren't asking for help.
you are asking us to write FOR you.
0
 

Author Comment

by:supertw2000
ID: 8216045
no i just don't get it makerp just show me some cases? but how's that gonna help me? i'm not trying to make you do work
0
 

Accepted Solution

by:
loontech earned 80 total points
ID: 8216073

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     
     
     switch (message)
     {case IDM_CUT:
case IDM_COPY:
if (hText != NULL) {
  /* Allocate memory and copy the string to it */
  if (!(hData = GlobalAlloc(GMEM_DDESHARE, GlobalSize (hText)))) {
      OutOfMemory();
      return (TRUE);
  }
  if (!(lpData = GlobalLock(hData))) {
      OutOfMemory();
      return (TRUE);
  }
  if (!(lpszText = GlobalLock (hText))) {
      OutOfMemory();
      return (TRUE);
  }
  strcpy(lpData, lpszText);
  GlobalUnlock(hData);
  GlobalUnlock (hText);
/* Clear the current contents of the clipboard, and set
* the data handle to the new string. */

  if (OpenClipboard(hWnd)) {
       EmptyClipboard();
       SetClipboardData(CF_TEXT, hData);
       CloseClipboard();
  }
  hData = NULL;
case IDM_PASTE:
  if (OpenClipboard(hWnd)) {
/* get text from the clipboard */
     if (!(hClipData = GetClipboardData(CF_TEXT))) {
         CloseClipboard();
         break;
     }
     if (hText != NULL) {
         GlobalFree(hText);
     }
     if (!(hText = GlobalAlloc(GMEM_MOVEABLE,GlobalSize(hClipData)))) {
         OutOfMemory();
         CloseClipboard();
         break;
     }
     if (!(lpClipData = GlobalLock(hClipData))) {
         OutOfMemory();
         CloseClipboard();
         break;
      }
      if (!(lpszText = GlobalLock(hText))) {
                           OutOfMemory();
                           CloseClipboard();
                           break;
      }
      strcpy(lpszText, lpClipData);
      GlobalUnlock(hClipData);
      CloseClipboard();
      GlobalUnlock(hText);
      break;
}
0
 

Author Comment

by:supertw2000
ID: 8222152
ok yea i'm just going to give you the points and i still have no idea, i mean there's no main() (you must think i'm really stupid); could you please still make a program that prompt a string and copy it to the clipboard??
0
 

Author Comment

by:supertw2000
ID: 8222186
like in a simple dos window
0
 

Expert Comment

by:loontech
ID: 8237380
//this might be more what you are looking for
//It worked on my system as a console app in straight C
//you might need to tweak it so feel free
#include <windows.h>
#include <stdio.h>
#define MAX 250
void main(void)
{
HANDLE CosoleIn ;
char input[MAX];


scanf("%s",input);
CosoleIn=input;  

     if (OpenClipboard(NULL))  //open clipboard for recieving
          {
               EmptyClipboard(); //clear all data and fomats out
           SetClipboardData(CF_TEXT,CosoleIn); //put the clipboard data
                     //GlobalLock(CosoleIn);
                    //GlobalUnlock(CosoleIn);
          CloseClipboard(); // close the clipboard
          }
          }

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

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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

800 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