FIX: Clipboard Assistant - Paste Fails or Causes Hard BreakLast reviewed: September 19, 1997Article ID: Q148687 |
The information in this article applies to:
SYMPTOMSCode generated by the Clipboard Assistant for a Custom Clipboard Format may cause the following problems:
CAUSEThe first three problems can occur because of the OnEditPaste function generated by the Clipboard Assistant. The code is not correct because it frees the HGLOBAL that it retrieves from the clipboard when doing a paste operation. This memory should not be freed because the Clipboard will free it when necessary. The code also fails to call CloseClipboard for all possible calls to OpenClipboard. The fourth problem is caused by a separate bug in the CSharedFile::Detach() function. For more details on this problem, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q148455 TITLE : BUG: CSharedFile::Detach() Does Not Call GlobalUnlock() RESOLUTIONThe sample code in this article shows new versions of the OnEditCopy, OnEditCut, and OnEditPaste functions. You should use this code to replace the versions previously generated by the Clipboard Assistant.
STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual C++ version 5.0.
MORE INFORMATION
Sample Code to Resolve Problem
/* Compile options needed: Default AppWizard options */ void CMyView::OnEditCopy() { // CG: This block was added by the Clipboard Assistant component { CSharedFile memFile; CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete); // serialize data to archive object DoCutCopyPaste(ar, FALSE); ar.Flush(); HGLOBAL hData = memFile.Detach(); //Resolution for CSharedFile::Detach() problem //Check Q148455 for current status. // _MFC_VER might need to be updated. #if _MFC_VER <= 0x0420 ::GlobalUnlock(hData); #endif if (OpenClipboard()) { ::SetClipboardData(m_nClipboardFormat, hData); CloseClipboard(); } else AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD); } } void CMyView::OnEditCut() { // CG: This block was added by the Clipboard Assistant component { CSharedFile memFile; CArchive ar(&memFile, CArchive::store|CArchive::bNoFlushOnDelete); // Serialize data to archive object DoCutCopyPaste(ar, TRUE); ar.Flush(); HGLOBAL hData = memFile.Detach(); //Resolution for CSharedFile::Detach() problem //Check Q148455 for current status. // _MFC_VER might need to be updated. #if _MFC_VER <= 0x0420 ::GlobalUnlock(hData); #endif if (OpenClipboard()) { ::SetClipboardData(m_nClipboardFormat, hData); CloseClipboard(); } else AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD); } } void CMyView::OnEditPaste() { // CG: This block was added by the Clipboard Assistant component { if (OpenClipboard()) { HANDLE hData = ::GetClipboardData(m_nClipboardFormat); if (hData != NULL) { CSharedFile memFile; memFile.SetHandle(hData,FALSE); CArchive ar(&memFile, CArchive::load); // Serialize data to document DoCutCopyPaste(ar, FALSE); ar.Close(); //Resolution for CSharedFile::Detach() problem //Check Q148455 for current status. // _MFC_VER might need to be updated. #if _MFC_VER <= 0x0420 ::GlobalUnlock(memFile.Detach()); #else memFile.Detach(); #endif } else AfxMessageBox(CG_IDS_CANNOT_GET_CLIPBOARD_DATA); CloseClipboard(); } else AfxMessageBox(CG_IDS_CANNOT_OPEN_CLIPBOARD); } } Keywords : VwbIss kbcode kbprg kbtool kbbuglist kbfixlist Technology : kbMfc Version : 4.0 4.1 4.2 Platform : NT WINDOWS Issue type : kbbug Solution Type : kbfix |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |