Click here to Skip to main content
Licence CPOL
First Posted 15 Mar 2007
Views 36,149
Bookmarked 33 times

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

By Tydia-kun | 15 Mar 2007
Ever had a problem when the clipboard won't work? Like you can't copy or paste anything?
4 votes, 66.7%
1
1 vote, 16.7%
2

3
1 vote, 16.7%
4

5
1.48/5 - 6 votes
μ 1.48, σa 0.78 [?]

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 will 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 application 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 has 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 the code below, with comments of course, to show you how to solve this situation.

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
::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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tydia-kun



Sweden Sweden

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks a lot PinmemberMember 160006422:04 24 Aug '10  
QuestionCan you write a small tool ? Pinmembertikkana15:29 23 Jul '10  
GeneralNice Pinmembersivakarthiki21:27 23 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120206.1 | Last Updated 15 Mar 2007
Article Copyright 2007 by Tydia-kun
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid