Clipboard Access

This is a discussion on Clipboard Access within the C++ Programming forums, part of the General Programming Boards category; I want to place pieces of text onto the clipboard from within a Win32 console app. How can this be ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    Clipboard Access

    I want to place pieces of text onto the clipboard from within a Win32 console app. How can this be done?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Code:
    OpenClipboard(hWnd); // Or null, if you have no HWND defined.
    EmptyClipboard();
    
    SetClipboardData(CF_TEXT, "Some_String_of_Data");
    
    CloseClipboard();

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by Lithorien
    Code:
    OpenClipboard(hWnd); // Or null, if you have no HWND defined.
    EmptyClipboard();
    
    SetClipboardData(CF_TEXT, "Some_String_of_Data");
    
    CloseClipboard();
    That empties it, but the new text doesn't go there.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by samGwilliam
    That empties it, but the new text doesn't go there.
    Are you sure that you're reading back from the clipboard correctly?

    Here's a more verbose example that I know works correctly, however.

    Code:
    OpenClipboard(hWnd);
    EmptyClipboard();
    
    int start, end, length = GetWindowTextLength(GetDlgItem(hWnd, ID_EDIT));
    
    SendMessage(hEdit, EM_GETSEL,(WPARAM)&start, (LPARAM)&end);
    				
    if ((length > 0) && (start != end))
    {
    	char *temp = new char[length+1];
    	char *temp2 = new char [(end-start)+1];
    	
    	GetDlgItemText(hWnd, ID_EDIT, temp, length+1);
    	
    	int i = 0;
    	
    	for (int x = start; x < end; x++)
    	{
    		temp2[0 + i] = temp[x];
    		i++;
    	}
    	
    	SetClipboardData(CF_TEXT, temp2);
    	
    	delete temp;
    }
    
    CloseClipboard();
    This would read in from an edit box (ID_EDIT), and then set the clipboard data equal to whatever's hilighted in the edit box.

    I know this works correctly, and hope you can figure out what to do from it.

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I am using the NULL argument as I have no hWnds defined. Could this be the problem?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by samGwilliam
    I am using the NULL argument as I have no hWnds defined. Could this be the problem?
    It doesn't appear to be the problem (I just tested with NULL myself, didn't fail).

    Can I see your current code?

  7. #7
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Code:
    char outString [10000];
    .
    .
    .
    OpenClipboard (NULL); // Or null, if you have no HWND defined.
    EmptyClipboard ();
    
    SetClipboardData (CF_TEXT, outString);
    
    CloseClipboard ();

  8. #8
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Actually, the NULL is the problem (oddly enough - why'd it work for me?)

    OpenClipboard()
    SetClipboardData()

    If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.
    My apoligies. :/

  9. #9
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    So - excuse my ignorance - as this is a console app, and I've never done Windows code before, how should I include a hWnd in my code and use/assign it correctly?

  10. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    You can declare a HWND by #include <windows.h> 'ing, and declaring HWND hWnd; (or whatever).

    At that point, you can try it in your clipboard code.

    Code:
    #include <windows.h>
    
    int main()
    {
        HWND hWnd;
        // ...
    }

  11. #11
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    I tried it. It doesn't even empty it now. Trust MS to come up with some stupid, roundabout way of doing it. Why should it be so important who "owns" the clipboard? Why can't it just put data up there?

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by samGwilliam
    I tried it. It doesn't even empty it now. Trust MS to come up with some stupid, roundabout way of doing it. Why should it be so important who "owns" the clipboard? Why can't it just put data up there?
    Good question.

    Is it possible to convert your project to a Win32 GUI project, and see if you can get your window recognized then? Because as it stands, your declared hWnd still == NULL.

    And the only way I know of to get hWnd != NULL is to actually CreateWindow() and set hWnd equal to the result of that call. :/

    Sorry, sam..

  13. #13
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by Lithorien
    Good question.

    Is it possible to convert your project to a Win32 GUI project, and see if you can get your window recognized then? Because as it stands, your declared hWnd still == NULL.

    And the only way I know of to get hWnd != NULL is to actually CreateWindow() and set hWnd equal to the result of that call. :/

    Sorry, sam..
    That's a bit crap isn't it? Bloody MS....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ww hosting and SSH access
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-07-2005, 08:49 AM
  2. copying to clipboard
    By bballzone in forum Windows Programming
    Replies: 24
    Last Post: 09-30-2004, 03:24 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, 01:32 PM
  5. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 01:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21