Skip to content

Commit

Permalink
The 'ClipboardFiles' property will now be cleared upon copying files.
Browse files Browse the repository at this point in the history
Prevented crashing when performing a clipboard operation in Remote Desktop environments.
  • Loading branch information
Willy-Kimura committed Jun 25, 2020
1 parent bd5d023 commit 1555dcc
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 50 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion SharpClipboard.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.30002.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpClipboard", "SharpClipboard\SharpClipboard.csproj", "{005C3326-D638-4113-AEBB-A83433CC015E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpClipboard.Tests.WinForms", "SharpCliboard.Tests.WinForms\SharpClipboard.Tests.WinForms.csproj", "{86F8EAC7-259D-4A2F-842D-AA93163293C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpClipboard.Tests.WinForms", "SharpClipboard.Tests.WinForms\SharpClipboard.Tests.WinForms.csproj", "{86F8EAC7-259D-4A2F-842D-AA93163293C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpClipboard.Tests.NetCoreWinForms", "SharpClipboard.Tests.NetCoreWinForms\SharpClipboard.Tests.NetCoreWinForms.csproj", "{D73E5EB1-DC2F-46E2-A30D-D76ECF2C4A07}"
EndProject
Expand Down
4 changes: 2 additions & 2 deletions SharpClipboard/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.5.1.0")]
[assembly: AssemblyFileVersion("3.5.1.0")]
[assembly: AssemblyVersion("3.5.2.0")]
[assembly: AssemblyFileVersion("3.5.2.0")]
3 changes: 2 additions & 1 deletion SharpClipboard/SharpClipboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<PackageProjectUrl>https://github.com/Willy-Kimura/SharpClipboard</PackageProjectUrl>
<PackageIconUrl></PackageIconUrl>
<Description>SharpClipboard is a clipboard-monitoring library for .NET that listens to the system's clipboard entries, allowing developers to tap into the rich capabilities of determining the clipboard's contents at runtime.</Description>
<PackageReleaseNotes>Added the static property 'HandleCaption'.</PackageReleaseNotes>
<PackageReleaseNotes>- The 'ClipboardFiles' property will now be cleared upon copying files.
- Prevented crashing when performing a clipboard operation in Remote Desktop environments.</PackageReleaseNotes>
<Copyright>© 2020 Willy Kimura</Copyright>
<PackageTags>c# vb clipboard monitor clipboard-monitor clipboard-manager clipboard-history</PackageTags>
<PackageIcon>clipboard.png</PackageIcon>
Expand Down
105 changes: 59 additions & 46 deletions SharpClipboard/Views/ClipboardHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,68 +174,81 @@ protected override void WndProc(ref Message m)
}
}

// Determines whether a file/files have been cut/copied.
if ((SharpClipboardInstance.ObservableFormats.Files == true) &&
(dataObj.GetDataPresent(DataFormats.FileDrop)))
try
{
string[] capturedFiles = (string[])dataObj.GetData(DataFormats.FileDrop);
// Determines whether a file/files have been cut/copied.
if ((SharpClipboardInstance.ObservableFormats.Files == true) &&
(dataObj.GetDataPresent(DataFormats.FileDrop)))
{
string[] capturedFiles = (string[])dataObj.GetData(DataFormats.FileDrop);

// If the 'capturedFiles' string array persists as null, then this means
// that the copied content is of a complex object type since the file-drop
// format is able to capture more-than-just-file content in the clipboard.
// Therefore assign the content its rightful type.
if (capturedFiles == null)
{
SharpClipboardInstance.ClipboardObject = dataObj;
SharpClipboardInstance.ClipboardText = dataObj.GetData(DataFormats.UnicodeText).ToString();

SharpClipboardInstance.Invoke(dataObj, SharpClipboard.ContentTypes.Other,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
else
{
// Clear all existing files before update.
SharpClipboardInstance.ClipboardFiles.Clear();

SharpClipboardInstance.ClipboardFiles.AddRange(capturedFiles);
SharpClipboardInstance.ClipboardFile = capturedFiles[0];

SharpClipboardInstance.Invoke(capturedFiles, SharpClipboard.ContentTypes.Files,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
}

// If the 'capturedFiles' string array persists as null, then it means
// that the copied content is of a complex object-type since the file-drop
// format is able to capture more-than-just-file content in the clipboard.
// Assign the content its rightful content-type.
if (capturedFiles == null)
// Determines whether text has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Texts == true) &&
(dataObj.GetDataPresent(DataFormats.Text) || dataObj.GetDataPresent(DataFormats.UnicodeText)))
{
SharpClipboardInstance.ClipboardObject = dataObj;
SharpClipboardInstance.ClipboardText = dataObj.GetData(DataFormats.UnicodeText).ToString();
string capturedText = dataObj.GetData(DataFormats.UnicodeText).ToString();
SharpClipboardInstance.ClipboardText = capturedText;

SharpClipboardInstance.Invoke(dataObj, SharpClipboard.ContentTypes.Other,
SharpClipboardInstance.Invoke(capturedText, SharpClipboard.ContentTypes.Text,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
else

// Determines whether an image has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Images == true) &&
(dataObj.GetDataPresent(DataFormats.Bitmap)))
{
SharpClipboardInstance.ClipboardFiles.AddRange(capturedFiles);
SharpClipboardInstance.ClipboardFile = capturedFiles[0];
Image capturedImage = dataObj.GetData(DataFormats.Bitmap) as Image;
SharpClipboardInstance.ClipboardImage = capturedImage;

SharpClipboardInstance.Invoke(capturedFiles, SharpClipboard.ContentTypes.Files,
SharpClipboardInstance.Invoke(capturedImage, SharpClipboard.ContentTypes.Image,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
}

// Determines whether text has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Texts == true) &&
(dataObj.GetDataPresent(DataFormats.Text) || dataObj.GetDataPresent(DataFormats.UnicodeText)))
{
string capturedText = dataObj.GetData(DataFormats.UnicodeText).ToString();
SharpClipboardInstance.ClipboardText = capturedText;

SharpClipboardInstance.Invoke(capturedText, SharpClipboard.ContentTypes.Text,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}

// Determines whether an image has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Images == true) &&
(dataObj.GetDataPresent(DataFormats.Bitmap)))
{
Image capturedImage = dataObj.GetData(DataFormats.Bitmap) as Image;
SharpClipboardInstance.ClipboardImage = capturedImage;

SharpClipboardInstance.Invoke(capturedImage, SharpClipboard.ContentTypes.Image,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
// Determines whether a complex object has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Others == true) &&
!(dataObj.GetDataPresent(DataFormats.FileDrop)))
{
SharpClipboardInstance.Invoke(dataObj, SharpClipboard.ContentTypes.Other,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
}
}

// Determines whether a complex object has been cut/copied.
else if ((SharpClipboardInstance.ObservableFormats.Others == true) &&
!(dataObj.GetDataPresent(DataFormats.FileDrop)))
catch (AccessViolationException)
{
SharpClipboardInstance.Invoke(dataObj, SharpClipboard.ContentTypes.Other,
new SourceApplication(GetForegroundWindow(), SharpClipboardInstance.ForegroundWindowHandle(),
GetApplicationName(), GetActiveWindowTitle(), GetApplicationPath()));
// Use-cases such as Remote Desktop usage might throw this exception.
// Applications with Administrative privileges can however override
// this exception when run in a production environment.
}
catch (NullReferenceException) { }
}

// Provides support for multi-instance clipboard monitoring.
Expand Down

0 comments on commit 1555dcc

Please sign in to comment.