Thread: copying to clipboard

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    copying to clipboard

    i was given this example to paste from the clipboard and it works fine.

    Code:
    #include <stdio.h> 
    #include <windows.h> 
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <cstdio> 
    #include <conio.h>
    
    int GetClipboardSize() 
    { 
      OpenClipboard(NULL) ;                          // Obtain clipboard 
      HGLOBAL handle = GetClipboardData (CF_TEXT) ;  // get handle to data 
      CloseClipboard() ;                             // Close clipboard 
      return GlobalSize( handle ) ;                  // return size of data 
    } 
    
    void GetClipboardText( TCHAR * buffer ) 
    { 
      OpenClipboard(NULL) ;                          // Obtain clipboard  
      HGLOBAL handle = GetClipboardData (CF_TEXT) ;  // get handle to data  
      char* szClipboard = (char*)GlobalLock(handle); // lock data 
      lstrcpy( buffer, szClipboard ) ;               // copy clipboard text 
      GlobalUnlock (handle) ;                        // unlock data 
      CloseClipboard () ;                            // close data 
    } 
    
    int main() 
    {
      // Allocate enough memory to save text in clipboard. 
      char * szClipboardText = new char[ GetClipboardSize() + 1 ] ; 
    
      // Get the clipboard text. 
      GetClipboardText( szClipboardText ) ; 
    
      // Print clipboard text. 
      printf( "Clipboard Text: \"%s\"", szClipboardText ); 
    cout << endl;
    
      // Delete the memory. 
      delete[] szClipboardText ; 
    
      getchar(); 
    }

    is there similar code that will copy to the clipboard? i would like to use these two together.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    BOOL GetClipboardText(LPTSTR buff, UINT iBuffSize)
    {
    	HANDLE hClip;
    	char* pszClipboard;
    
    	if(OpenClipboard(NULL))	// Open the clipboard so we can read its contents
    	{
    		hClip = GetClipboardData(CF_TEXT);	// Get a handle to the clipboad contents
    		if(hClip == NULL)
    		{
    			CloseClipboard();
    			return FALSE;
    		}
    		pszClipboard = (char*)GlobalLock(hClip);	// Point pClipboard to the clipboard contents
    		if(!pszClipboard)
    		{
    			GlobalUnlock(hClip);
    			return FALSE;
    		}
    		strncpy(buff,pszClipboard,iBuffSize);		// Copy the contents to a local variable
    		buff[iBuffSize-1] = 0;
    		GlobalUnlock(hClip);	// Release the lock on the clipboard contents
    		CloseClipboard();	// Close the clipboard.
    	}
    	else
    	{
    		return FALSE;
    	}
    
    	return TRUE;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Oops, I read your question too fast and thought you asked for a way to get text from the clipboard

    Here we go:

    Code:
    BOOL SetClipboardText(LPCTSTR lpszText, UINT uBuff)
    {
    	HGLOBAL hgText = NULL;
    	LPTSTR	lpszCopy = NULL;
    
    	// First open the clipboard
    	if(!::OpenClipboard(NULL))
    		return FALSE;
    	// Empty the clipboard, and make us the owner
    	if(!::EmptyClipboard())
    	{
    		::CloseClipboard();
    		return FALSE;
    	}
    
    	hgText = GlobalAlloc(GMEM_DDESHARE, (uBuff+1) * sizeof(TCHAR));
    
        if (hgText != NULL)
        {
    		// Lock the handle and copy the text to the buffer.
            lpszCopy = (char*)GlobalLock(hgText);
            memcpy(lpszCopy, lpszText, uBuff * sizeof(TCHAR));
            lpszCopy[uBuff] = (TCHAR) 0;
            GlobalUnlock(hgText);
    
    		// Set the clipboard text
    		if(!::SetClipboardData(CF_TEXT,hgText))
    		{
    			::CloseClipboard();
    			return FALSE;
    		}
    	}
    	::CloseClipboard();
    	return TRUE;
    }

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Code:
    void SetClipboardText( HWND hwnd, TCHAR * szText )
    {
      HGLOBAL globalHandle = GlobalAlloc( GHND|GMEM_SHARE,
                                         (lstrlen(szText)+1) * sizeof(TCHAR)) ;
    
      TCHAR * szClipboard = (TCHAR*)GlobalLock(globalHandle) ;
      lstrcpy( szClipboard, szText ) ;
      GlobalUnlock (globalHandle) ;
      OpenClipboard(hwnd) ;
      EmptyClipboard () ;
      SetClipboardData (CF_TEXT, globalHandle) ;
      CloseClipboard() ;
    }
    By the way bballzone, did you get your code from here?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    yea i did. i don't like those forums as much because they do not respond as fast.

    is there also a way that i can just put the contents of a variable into the clipboard. for example if i prompt the user to enter their name and i store it in a variable called fname, can i put fname into the clipboard?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    of course.

    Code:
    char szName[] = "this is my name";
    SetClipboardText(szName,strlen(szName));

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    Code:
    Error: need explicit cast for function parameter 2 to get
    from: unsigned
    to: char *
    at this line

    SetClipboardText(szName,strlen(szName));

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's because you used Dante Shamest's code instead of mine.

    You should be using mine since Dante's doesnt accept a buffer length as a function parameter, and his also ignores return values which is a very bad idea in this case.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    thanks a lot bithub

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    since this is related to this topic i'll just put this here also. why isn't this allowed?

    Code:
    	keybd_event(VK_V,0,0,0); 
    	keybd_event(VK_V,0,KEYEVENTF_KEYUP,0);
    i can use VK_CONTROL but i can't have it type V.

  11. #11
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Try 'V' ?

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    and now... thank you dante

  13. #13
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by bballzone
    i was given this example to paste from the clipboard and it works fine.

    Code:
    #include <stdio.h> 
    #include <windows.h> 
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <cstdio> 
    #include <conio.h>
    
    int GetClipboardSize() 
    { 
      OpenClipboard(NULL) ;                          // Obtain clipboard 
      HGLOBAL handle = GetClipboardData (CF_TEXT) ;  // get handle to data 
      CloseClipboard() ;                             // Close clipboard 
      return GlobalSize( handle ) ;                  // return size of data 
    } 
    
    void GetClipboardText( TCHAR * buffer ) 
    { 
      OpenClipboard(NULL) ;                          // Obtain clipboard  
      HGLOBAL handle = GetClipboardData (CF_TEXT) ;  // get handle to data  
      char* szClipboard = (char*)GlobalLock(handle); // lock data 
      lstrcpy( buffer, szClipboard ) ;               // copy clipboard text 
      GlobalUnlock (handle) ;                        // unlock data 
      CloseClipboard () ;                            // close data 
    } 
    
    int main() 
    {
      // Allocate enough memory to save text in clipboard. 
      char * szClipboardText = new char[ GetClipboardSize() + 1 ] ; 
    
      // Get the clipboard text. 
      GetClipboardText( szClipboardText ) ; 
    
      // Print clipboard text. 
      printf( "Clipboard Text: \"%s\"", szClipboardText ); 
    cout << endl;
    
      // Delete the memory. 
      delete[] szClipboardText ; 
    
      getchar(); 
    }

    is there similar code that will copy to the clipboard? i would like to use these two together.
    What a terrible mess; never use wherever you got that from again.
    Using depreciated old C headers in a C++ program (string.h, stdlib.h, stdio.h)
    Using depreciated old C++ headers in a C++ program (fstream.h, iostream.h)
    Yet all this along-side cstdio?
    C-style casts in a C++ program.
    Mixing C-stdio (printf) and C++ iostreams (cout)
    conio.h is nasty... and you don't even use any of it's nasty functions! why include it?
    windows.h included twice?

    In GetClipboardText you take a TCHAR * and use lstrcpy(), why? in main() you create szClipboardText as a char *, and if this thing so happened to be compiled in the mythical land of wide characters, I guarantee you at least one function in the program as written wouldn't know what to do with it's data.
    hello, internet!

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    it was part of a whole program that did use those headers.

    i use the Digital Mars compiler that requires all headers to end in a '.h' extension.

  15. #15
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by bballzone
    it was part of a whole program that did use those headers.

    i use the Digital Mars compiler that requires all headers to end in a '.h' extension.
    Where does <cstdio> fit in then? I don't know what digital mars is, but if it doesnt support standard C++ and modern headers, chuck it for something useful.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clipboard hijack
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-08-2008, 06:04 AM
  2. Copying a Directory to the Clipboard
    By Buiden in forum C++ Programming
    Replies: 0
    Last Post: 07-15-2005, 12:21 PM
  3. Clipboard and Custom Types
    By McClamm in forum C# Programming
    Replies: 1
    Last Post: 09-16-2004, 04:43 PM
  4. Clipboard Modifier
    By Korhedron in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 02:32 PM
  5. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM