I want to place pieces of text onto the clipboard from within a Win32 console app. How can this be done?
I want to place pieces of text onto the clipboard from within a Win32 console app. How can this be done?
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.Originally Posted by Lithorien
Are you sure that you're reading back from the clipboard correctly?Originally Posted by samGwilliam
Here's a more verbose example that I know works correctly, however.
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.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();
I know this works correctly, and hope you can figure out what to do from it.
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).Originally Posted by samGwilliam
Can I see your current code?
Code:char outString [10000]; . . . OpenClipboard (NULL); // Or null, if you have no HWND defined. EmptyClipboard (); SetClipboardData (CF_TEXT, outString); CloseClipboard ();
Actually, the NULL is the problem (oddly enough - why'd it work for me?)
OpenClipboard()
SetClipboardData()
My apoligies. :/If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.
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?
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; // ... }
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.Originally Posted by samGwilliam
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....Originally Posted by Lithorien