Skip to content

Commit 51fd252

Browse files
committed
Fix COMException(CLIPBRD_E_CANT_OPEN) thrown by Clipboard.SetText
Use Clipboard.SetDataObject(text, true) and try multiple times within a loop and delay in between.
1 parent ab6bdae commit 51fd252

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Runtime.InteropServices;
2+
using System.Threading.Tasks;
3+
using System.Windows;
4+
5+
using HandyControl.Controls;
6+
7+
namespace IPConfig.Helpers;
8+
9+
public static class ClipboardHelper
10+
{
11+
private const int CLIPBRD_E_CANT_OPEN = unchecked((int)0x800401D0);
12+
13+
public static async Task SetTextAsync(string text)
14+
{
15+
for (int i = 0; i < 10; i++)
16+
{
17+
try
18+
{
19+
Clipboard.SetDataObject(text, true);
20+
Growl.Error(i.ToString());
21+
22+
return;
23+
}
24+
catch (COMException ex) when (ex.ErrorCode == CLIPBRD_E_CANT_OPEN)
25+
{ }
26+
27+
await Task.Delay(100);
28+
}
29+
}
30+
}

IPConfig/ViewModels/IPConfigDetailViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ public void RejectChanges()
372372
#region RelayCommands
373373

374374
[RelayCommand]
375-
private static void CopyDns(string dns)
375+
private static async Task CopyDnsAsync(string dns)
376376
{
377-
Clipboard.SetText(dns);
377+
await ClipboardHelper.SetTextAsync(dns);
378378
}
379379

380380
[RelayCommand(AllowConcurrentExecutions = true)]

IPConfig/ViewModels/IPConfigListViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text;
99
using System.Text.RegularExpressions;
10+
using System.Threading.Tasks;
1011
using System.Windows;
1112
using System.Windows.Data;
1213

@@ -201,7 +202,7 @@ where match.Success
201202
}
202203

203204
[RelayCommand]
204-
private void CopySelectedIPConfigsAsText()
205+
private async Task CopySelectedIPConfigsAsTextAsync()
205206
{
206207
string sep = new('=', 64);
207208
var sb = new StringBuilder();
@@ -213,7 +214,7 @@ private void CopySelectedIPConfigsAsText()
213214
}
214215

215216
string text = sb.ToString().Trim()[..^64];
216-
Clipboard.SetText(text);
217+
await ClipboardHelper.SetTextAsync(text);
217218
}
218219

219220
[RelayCommand(CanExecute = nameof(CanDeleteIPConfig))]

IPConfig/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ private void Closing(CancelEventArgs e)
295295
}
296296

297297
[RelayCommand(CanExecute = nameof(IsSelectedNicNotNull))]
298-
private void CopySelectedNicIPConfigAsText()
298+
private async Task CopySelectedNicIPConfigAsTextAsync()
299299
{
300300
string text = $"{SelectedNic}\n\n{SelectedNicIPConfig}".Trim();
301-
Clipboard.SetText(text);
301+
await ClipboardHelper.SetTextAsync(text);
302302
}
303303

304304
[RelayCommand]

IPConfig/ViewModels/NicConfigDetailViewModel.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Text.RegularExpressions;
66
using System.Threading.Tasks;
77
using System.Timers;
8-
using System.Windows;
98

109
using CommunityToolkit.Mvvm.ComponentModel;
1110
using CommunityToolkit.Mvvm.Input;
@@ -69,7 +68,7 @@ public void Receive(PropertyChangedMessage<Nic> message)
6968
#region Relay Commands
7069

7170
[RelayCommand]
72-
private void CopyAllContentsAsText()
71+
private async Task CopyAllContentsAsTextAsync()
7372
{
7473
string iPv4Header = Nic.SupportsIPv4 ? Lang.TcpIPv4_Header : $"{Lang.AdapterNotSupported} {Lang.TcpIPv4_Header}";
7574
string iPv6Header = Nic.SupportsIPv6 ? Lang.TcpIPv6_Header : $"{Lang.AdapterNotSupported} {Lang.TcpIPv6_Header}";
@@ -104,51 +103,51 @@ private void CopyAllContentsAsText()
104103
{Lang.NotSupported}
105104
""";
106105

107-
Clipboard.SetText(text);
106+
await ClipboardHelper.SetTextAsync(text);
108107
}
109108

110109
[RelayCommand]
111-
private void CopyContentsAsText(string tag)
110+
private async Task CopyContentsAsTextAsync(string tag)
112111
{
113112
if (tag == Lang.AdapterProperties_Header)
114113
{
115-
Clipboard.SetText(Nic.ToString());
114+
await ClipboardHelper.SetTextAsync(Nic.ToString());
116115
}
117116
else if (tag == Lang.TcpIPv4_Header)
118117
{
119-
Clipboard.SetText(GetIPv4PropertyContents());
118+
await ClipboardHelper.SetTextAsync(GetIPv4PropertyContents());
120119
}
121120
else if (tag == Lang.IPv4GeneralProperties_Header)
122121
{
123-
Clipboard.SetText(IPv4AdvancedCofnig.FormatGeneralProperties());
122+
await ClipboardHelper.SetTextAsync(IPv4AdvancedCofnig.FormatGeneralProperties());
124123
}
125124
else if (tag == Lang.IPv4Lifetimes_Header)
126125
{
127-
Clipboard.SetText(IPv4AdvancedCofnig.FormatLifetimes());
126+
await ClipboardHelper.SetTextAsync(IPv4AdvancedCofnig.FormatLifetimes());
128127
}
129128
else if (tag == Lang.IPv4AdvancedProperties_Header)
130129
{
131-
Clipboard.SetText(FormatIPv4InterfaceProperties());
130+
await ClipboardHelper.SetTextAsync(FormatIPv4InterfaceProperties());
132131
}
133132
else if (tag == Lang.IPv4Statistics_Header)
134133
{
135-
Clipboard.SetText(FormatIPv4InterfaceStatistics());
134+
await ClipboardHelper.SetTextAsync(FormatIPv4InterfaceStatistics());
136135
}
137136
else if (tag == Lang.TcpIPv6_Header)
138137
{
139-
Clipboard.SetText(GetIPv6PropertyContents());
138+
await ClipboardHelper.SetTextAsync(GetIPv6PropertyContents());
140139
}
141140
else if (tag == Lang.IPv6GeneralProperties_Header)
142141
{
143-
Clipboard.SetText(IPv6AdvancedCofnig.FormatGeneralProperties());
142+
await ClipboardHelper.SetTextAsync(IPv6AdvancedCofnig.FormatGeneralProperties());
144143
}
145144
else if (tag == Lang.IPv6Lifetimes_Header)
146145
{
147-
Clipboard.SetText(IPv6AdvancedCofnig.FormatLifetimes());
146+
await ClipboardHelper.SetTextAsync(IPv6AdvancedCofnig.FormatLifetimes());
148147
}
149148
else if (tag == Lang.IPv6AdvancedProperties_Header)
150149
{
151-
Clipboard.SetText(FormatIPv6InterfaceProperties());
150+
await ClipboardHelper.SetTextAsync(FormatIPv6InterfaceProperties());
152151
}
153152
else if (tag == Lang.IPv6Statistics_Header)
154153
{

0 commit comments

Comments
 (0)