Reputation: 1825
I have developed an application that catches all Clipboard text:
protected override void WndProc(ref Message m)
{
try
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
{
if (Clipboard.GetText() != string.Empty)
//I analyze the data then
if (ClipboardObject.CheckNewData(Clipboard.GetText()))
ClipboardObject.UpdateClipboardData(Clipboard.GetText());
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
break;
}
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardViewer)
nextClipboardViewer = m.LParam;
else
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
catch (Exception ex)
{
}
}
When the event is risen, i fill the data in an object that is shared to my whole application then in timer that ticks every half second i keep checking if there is new data in this object.
On every pc i have installed this application on, strange things start happening to windows even when my application isn't running:
I have been struggling with these issues for a long time and i really can't find any solution to my problem, any help is appreciated.
Upvotes: 1
Views: 893
Reputation: 63732
"Each window that receives the WM_DRAWCLIPBOARD
message must call the SendMessage function to pass the message on to the next window in the clipboard viewer chain."
Are you sure that always happens? You should have a try ... finally block there (in fact, GetText probably throws if the data isn't textual).
The WM_CHANGECBCHAIN
could also be very tricky - do you clean up after yourself on application exit? You have to remove yourself again using ChangeClipboardChain
, otherwise you leave a dangling pointer in the clipboard chain!
Basically, when your form is closing (and it's handle is getting disposed - but before it really is disposed), you need to call something like this:
ChangeClipboardChain(this.Handle, nextClipboardViewer);
The clipboard change is extremely brittle - a crashing application can leave you with broken clipboard. In .NET, at least try to use a finalizer to dispose of this (even then, it's going to be tricky) - in fact, it may not be a bad idea to bind the clipboard to something else rather than your actual window (so that you can implement the dispose-finalize pattern properly), but that's up to you. Also, see this question: Can aborting a process without resetting the clipboard chain cause trouble?
Note that even then, someone can just process kill your application (that's one of the reasons Task Manager warns you not to kill processes - in this case, it really will make your application unstable). Triple-check everything to make sure you dispose of the CB chain in every possible scenario (except for the unavoidable process kill - there are ways to fix even this in a way, but hey, if the user is being in ass, let him), otherwise you're going to leave your PC very unstable.
Upvotes: 3