Microsoft KB Archive/148687

= FIX: Clipboard Assistant - Paste Fails or Causes Hard Break =

Article ID: 148687

Article Last Modified on 12/2/2003

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition

-



This article was previously published under Q148687



SYMPTOMS
Code generated by the Clipboard Assistant for a Custom Clipboard Format may cause the following problems:

 Copying to the clipboard fails to replace what was previously placed on the clipboard. An access violation occurs with a message similar to the following:

Exception: access violation (0xc0000005),

Address: 0x5f82cf37

 An unexpected memory overwrite occurs after several clipboard operations. On the Checked build of Windows NT, a Cut, Copy, or Paste operation causes a hard-coded breakpoint to be encountered and a trace message similar to the following to be generated:

BASE: GlobalFree called with a locked object.





CAUSE
The 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:

148455 CSharedFile::Detach Does Not Call GlobalUnlock



RESOLUTION
The 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.



STATUS
Microsoft 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.



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 <WWLINK TYPE="ARTICLE" VALUE="Q148455">Q148455</WWLINK> 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 <WWLINK TYPE="ARTICLE" VALUE="Q148455">Q148455</WWLINK> 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 <WWLINK TYPE="ARTICLE" VALUE="Q148455">Q148455</WWLINK> 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: kbbug kbfix kbide kbprogramming kbcode KB148687

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.