Willy-Kimura / SharpClipboard

A library for anonymously monitoring clipboard entries.
188 stars 36 forks source link

2 type of Exceptions occur using SharpClipboard in a production environment #13

Closed karstennilsen closed 4 years ago

karstennilsen commented 4 years ago

To make the library workable I had to add the following exception handling in ClipboardHandle.cs protected override void WndProc:

    try
    {

        // Determines whether a file/files have been cut/copied.
        if ((SharpClipboardInstance.ObservableFormats.Files == true) &&
            (dataObj.GetDataPresent(DataFormats.FileDrop)))
        {
        ...
        }
    } catch (System.AccessViolationException)
    {
        // Do nothing, we can't access data we need (can e.g. happen in Remote Desktop environment)
    } catch (System.NullReferenceException)
    {
        // Do nothing, between determing clipboard format and fetching the data the clipboard object has already changed
        // happens e.g. in string capturedText = dataObj.GetData(DataFormats.UnicodeText).ToString(); when the UnicodeText property is not there anymore
    }

There might be better ways to handle these problems, but this is in each case better than crashing the application :) I would for the second exception suggest to first make a copy of the object before analysing so it can't be changed after determing the clipboard format.

Willy-Kimura commented 4 years ago

Great job! Will do.

C1rdec commented 4 years ago

Is there any release candidate for this?

Willy-Kimura commented 4 years ago

Oh yeah, on or before tomorrow will do.

Willy-Kimura commented 4 years ago

Hi @C1rdec we'll have to reschedule the release to tomorrow. Apologies for the inconvenience.

Willy-Kimura commented 4 years ago

Update to v3.5.2 now available.