5,702,921 members and growing! (14,450 online)
Email Password   helpLost your password?
Desktop Development » Clipboard » General     Beginner

How to fix up the clipboard when it won't work

By Tydia-kun

Ever had a problem when the clipboard won't work? Like you can't copy or paste anything?
VC8.0, C++, Windows, Dev

Posted: 15 Mar 2007
Updated: 15 Mar 2007
Views: 10,708
Bookmarked: 20 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 0.95 Rating: 1.36 out of 5
4 votes, 80.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Ever had the annoying problem that you can't use the clipboard? When you can't copy or paste anything? If yes, then this is for you.

Background

I present this solution to you now, as I worked out a solution to fix this. There seems to be a bug in a Windows component that causes this I've noticed. This shouldn't be there, of course, and to remove it, I shall present a solution.

Using the code

First, let me explain how the clipboard works in short. As you know, the clipboard can only take one snippet of information from one program at a time, which is why I suppose each application has to lock the clipboard before writing any data to it.
A program opens the clipboard using the Windows API OpenClipboard. Later, it closes it using CloseClipboard. While the clipboard is open, no other program can access the clipboard. Now imagine what problems would arise if a program did not close the clipboard after it was done!
Unfortunately, Windows does not allow any function (to my knowledge) that can force the clipboard to be closed, regardless of which program calls it. The clipboard that opens the clipboard needs to close it.

As you may understand, the problem that is presented in this article is when a program does not close the clipboard. To fix this, we need to find the window that is holding the clipboard open, and close it. Failing that, destroy it or terminate its owning process.

First, let's find the window that has opened the clipboard (and which have not closed it). Windows provides an API for this.
This function returns the handle to the window. And as we know the handle, we can do all sorts of crazy stuff! I'll present code below, with comments of course, to show you how to solve this situation.

<code><code><code><code><code><code>CHAR Buffer[1000]; // A local buffer to hold data
HWND h = GetOpenClipboardWindow(); // Returns the HWND of the window that is currently having access to the clipboard

::GetWindowText(h, Buffer, 1000); // Get the titlebar text of the window
GetWindowModuleFileName(h, Buffer, 1000); // Get the path to the module which owns the window (the EXE of the DLL where the owning window was created)

dwThreadId = GetWindowThreadProcessId(h, &dwProcessId); // Get the thread ID and process ID that owns this window
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId); // Open the process with full access; gives us a handle to the process

GetModuleFileNameEx(hProcess, NULL, Buffer, 1000); // Get the path to the program (EXE file) that owns the window (aka the process)
//TerminateProcess(hProcess, 0); // We can call this to terminate the process whose window is jamming the clipboard
CloseHandle(hProcess); // We no longer need the handle to the process, so let's close the handle
<code>::CloseWindow(h); // Close the offending window
::DestroyWindow(h); // Destroy the offending window. Usually called after closing the window.

That's it! Don't let those programs steal your clipboard! It's anyone's!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tydia-kun



Location: Sweden Sweden

Other popular Clipboard articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2007
Editor:
Copyright 2007 by Tydia-kun
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project