Copy to clipboard problem.
-
I need to copy some string to the clipboard. This is the code I use:
handle = GlobalAlloc(GMEM\_MOVEABLE,text.GetLength()+1); if (handle) { if (OpenClipboard(hWnd) && EmptyClipboard() ) { out = (char\*)GlobalLock(handle); // this fails sometimes (out=NULL)
My code sometimes fail at out = (char*)GlobalLock(handle); GlobalLock returns NULL and GetLastError() = 6 (invalid handle). Any ideas why ? Is there more reliable code for copying to clipboard ? Thank you!
rrrado
-
I need to copy some string to the clipboard. This is the code I use:
handle = GlobalAlloc(GMEM\_MOVEABLE,text.GetLength()+1); if (handle) { if (OpenClipboard(hWnd) && EmptyClipboard() ) { out = (char\*)GlobalLock(handle); // this fails sometimes (out=NULL)
My code sometimes fail at out = (char*)GlobalLock(handle); GlobalLock returns NULL and GetLastError() = 6 (invalid handle). Any ideas why ? Is there more reliable code for copying to clipboard ? Thank you!
rrrado
I ran across the following code in one of our projects - not sure if it will help you or not
HGLOBAL hData = 0; // open memory file CSharedFile clipb (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); clipb.Write(cstrString, cstrString.GetLength()*sizeof(TCHAR)); clipb.Write(_T("\0"), sizeof(TCHAR)); hData = clipb.Detach(); // Now, hand over memory block to clipboard if (hData) { VERIFY(OpenClipboard()); SetClipboardData(CF_TEXT, hData); VERIFY(CloseClipboard()); }
cje
-
I ran across the following code in one of our projects - not sure if it will help you or not
HGLOBAL hData = 0; // open memory file CSharedFile clipb (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); clipb.Write(cstrString, cstrString.GetLength()*sizeof(TCHAR)); clipb.Write(_T("\0"), sizeof(TCHAR)); hData = clipb.Detach(); // Now, hand over memory block to clipboard if (hData) { VERIFY(OpenClipboard()); SetClipboardData(CF_TEXT, hData); VERIFY(CloseClipboard()); }
cje