Page 1 of 1

How to unlock the Clipboard when another app misbehaves

Posted: Fri Sep 22, 2006 9:28 pm
by Luis Krause
Hi all

I'm having a problem. Something in my PC (and I know it's not FWH) is not releasing the Windows Clipboard, forcing me to reboot my PC in order to be able to do the everyday Cut & Paste stuff!

Researching in msdn, I found a couple of API functions that I thought would allow me to forcibly kill the other apps stronghold on the clipboard, but I'm missing something.

Code: Select all

DLL32 FUNCTION ClipboardOwner() ;
   AS LONG PASCAL FROM "GetClipboardOwner" Lib "user32"

DLL32 FUNCTION ClipboardChain( ;
   hPrevWnd AS LONG, ;
   hNewWnd  AS LONG ) ;
   AS LONG PASCAL FROM "ChangeClipboardChain" Lib "user32"
doing something like

Code: Select all

#define WM_DESTROYCLIPBOARD 775

Function Test()
   Local oClp
   Local hOwner := 0
   Local hNewOwner := 0

   MsgInfo( ClipBoardOwner() )
   oClp := TClipboard():New()
   //MsgInfo( "App handle: " + cValToChar( oClp:oWnd:hWnd ) )
   oClp:Open()

   // this doesn't forcibly release ownership
   hOwner := ClipBoardOwner()
   MsgGet( "New Clipboard Owner Handle", "New ::hWnd:", @hNewOwner )
   ClipboardChain( hOwner, hNewOwner )
   SendMessage( hNewOwner, WM_DESTROYCLIPBOARD, 0, 0 )

   oClp:Close()  // this works as expected, of course

   hOwner := ClipBoardOwner()
   MsgInfo( "Clipboard Owner: " + cValToChar( hOwner ) )

return nil
Has no efect.
If anyone has already created a routine that can release the clipboard when a misbehaved app doesn't release it, I'd appreciate your help.

Regard,
Luis

Posted: Thu Sep 28, 2006 10:51 am
by Badara Thiam
Luis,

Please look at SetClipboardViewer() API (w98 and 2000) if you don't know.

This is a part of "SAMS Windows 2000 API SuperBible" :

"SetClipBoardViewer places the given windows at the lead of the chain
of the clipboard viewers. The value returned, the handle of the next clipboard viewer on the chain, must be saved by the caller and kept up-to-date by processing the WM_CHANGECBCHAIN message. The handle of the next clipboard viewer is needed to process the WM_DRAWCLIPBOARD, WM_CHANGECBCHAIN, and WM_DESTROY messages.


Syntax : HWND SetClipboardViewer( HWND hWnd )"


Hope this help, 8)