This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Clipboard from Cygwin?


I dug around in my old code archives, and found the following (after a
little cleanup). It may help someone add write capability to the
/dev/clipboard.

--Chuck

#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define CLIPCHUNK 1024
int main()
{
   HANDLE hData;  /* handle to clip data */
   LPSTR lpData;  /* pointer to clip data */
   LPSTR lpszText;
   char chunk[CLIPCHUNK+1];
   char *buf = NULL;
   char *buf_old = NULL;
   char *ptr;
   int len = 0;
   int numread;

   /* Since we have to copy to the clipboard all in one step,
      read entire stream into memory. However, let's try to 
      do this using the minimum amount of memory, but keep 
      allocating more if needed.
   */
   do
   {
      numread = fread(chunk,sizeof(char),CLIPCHUNK,stdin);
      if(numread > 0)
      {
	 if (buf)
	 {
	    if ((buf_old = malloc (len * sizeof(char))) == NULL)
	    {
	       fprintf(stderr,"internal error - out of memory");
	       return(FALSE);
	    }
	    memcpy(buf_old, buf, len);
	    free(buf);
	 }
         if ((buf = malloc ((len+numread) * sizeof(char))) == NULL)
	 {
	    fprintf(stderr,"internal error - out of memory");
	    return(FALSE);
	 }
	 if (buf_old)
	 {
	    memcpy(buf,buf_old,len);
	    free(buf_old);
	 }
	 ptr = &(buf[len]);
	 memcpy(ptr,chunk,numread);
	 len += numread;
      }
   }
   while(!feof(stdin) && !ferror(stdin));

   /* Allocate memory and copy the string to it */
   if (!(hData 
	 = GlobalAlloc(GHND, len+1))) {
      return (TRUE);
   }
   if (!(lpData = GlobalLock(hData))) {
      return (TRUE);
   }

   strcpy((char *)lpData, buf);
   GlobalUnlock(hData);
    
   /* Clear the current contents of the clipboard, and set
    * the data handle to the new string.
    */
   
   if (OpenClipboard(0)) {
      EmptyClipboard();
      SetClipboardData(CF_TEXT, hData);
      CloseClipboard();
   }
   hData = NULL;
   return (TRUE);
}

begin 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36$PZ*T3YVJG?HQVE;*^8O!V=UD^',2PSH<("2Z
8PM?^CN9YX+==SZ_(7/7^`&.H&'3=!@``
`
end

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]