Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Clipboard In C

Posted on 2003-03-23
18
Medium Priority
?
369 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
  • 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

Industry Leaders: 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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

580 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