Pasting data to clipboard

I am working on a C++ program that allows you to paste URLs into YouTube comments/posts on forums where URLs are forbidden. It works by pasting the modified (post-safe!) URL onto your clipboard. I ran into an invalid handle error (error code 6) upon trying to SetClipboardData(CF_TEXT, (void*)modifiedText.c_str()); Has anyone else on here run into the same/similar issues, and how did they solve it? //I was using a plain old HANDLE to store modifiedText.c_str(). Will I have to upgrade to HGLOBAL, or something?
Figured it out. I ended up using OpenClipboard(0) to open the clipboard, which is a HUGE no: http://msdn.microsoft.com/en-us/library/windows/desktop/ms649048%28v=vs.85%29.aspx . My problem stems from the fact that I don't know what argument OpenClipboard() should receive for moving text to the clipboard //and my GCC compiler is throwing a fit about my use of OpenClipboard() with no arguments

I also know that OpenClipboard(HWND); takes, as its parameter, a handle to the window that is using the clipboard, but I wish to place the data on the clipboard for the browser (whatever they are running), and possibly this program that I am making, to use the clipboard. I was thinking of implementing some version of the code snippet found here: http://www.cplusplus.com/forum/beginner/33250/ , but have no idea as to how hWnd is defined. //I try typing that into my Code::Blocks IDE and it doesn't know what the hell I am talking about. I know that typeid(hWnd).name() == "HWND"; (or some child of it), but is it being initialized to the current window (hopefully, not 0....) upon declaration?
Last edited on
I understand the code snippet now, and have used it. This one that is: http://www.cplusplus.com/forum/beginner/14349/ . Thank you, helios for writing that; although I have improved it by exploiting the fact that some of those functions are boolean, or return 0 upon failure. I also made my function boolean. I would like to learn about the multithreading that went on there and why it was even necessary.
Please look this code: what of this lines generate error?

BOOL SetClipboardText(LPCTSTR pszText)
{
BOOL ok = FALSE;
if(OpenClipboard(NULL)) {
EmptyClipboard();

LCID lc = GetUserDefaultLCID();
HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(lc) );
LCID* pLc = (LCID*)GlobalLock(hData);
*pLc = lc;
GlobalUnlock(hData);
SetClipboardData(CF_LOCALE, hData);

HGLOBAL hMem = GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE,
(lstrlen(pszText)+1)*sizeof(pszText[0]) );
LPTSTR ptxt = (LPTSTR)GlobalLock(hMem);
lstrcpy(ptxt, pszText);
GlobalUnlock(hMem);
ok = (SetClipboardData(CF_TEXT, hMem)!=NULL);

CloseClipboard(); // relinquish it for other windows
}
return ok;
}
None of them. //I have tried it on my computer
Topic archived. No new replies allowed.