-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileDataObject.cs
More file actions
986 lines (904 loc) · 45.6 KB
/
VirtualFileDataObject.cs
File metadata and controls
986 lines (904 loc) · 45.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
// This code taken from http://blogs.msdn.com/b/delay/archive/2009/11/16/creating-something-from-nothing-and-knowing-it-developer-friendly-virtual-file-implementation-for-net-refined.aspx
// Then adapted for WinForms instead of WPF
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
namespace Delay
{
/// <summary>
/// Class implementing drag/drop and clipboard support for virtual files.
/// Also offers an alternate interface to the IDataObject interface.
/// </summary>
public sealed class VirtualFileDataObject : System.Runtime.InteropServices.ComTypes.IDataObject, IAsyncOperation
{
/// <summary>
/// Gets or sets a value indicating whether the data object can be used asynchronously.
/// </summary>
public bool IsAsynchronous { get; set; }
/// <summary>
/// Identifier for CFSTR_FILECONTENTS.
/// </summary>
private static short FILECONTENTS = (short)(DataFormats.GetFormat(NativeMethods.CFSTR_FILECONTENTS).Id);
/// <summary>
/// Identifier for CFSTR_FILEDESCRIPTORW.
/// </summary>
private static short FILEDESCRIPTORW = (short)(DataFormats.GetFormat(NativeMethods.CFSTR_FILEDESCRIPTORW).Id);
/// <summary>
/// Identifier for CFSTR_PASTESUCCEEDED.
/// </summary>
private static short PASTESUCCEEDED = (short)(DataFormats.GetFormat(NativeMethods.CFSTR_PASTESUCCEEDED).Id);
/// <summary>
/// Identifier for CFSTR_PERFORMEDDROPEFFECT.
/// </summary>
private static short PERFORMEDDROPEFFECT = (short)(DataFormats.GetFormat(NativeMethods.CFSTR_PERFORMEDDROPEFFECT).Id);
/// <summary>
/// Identifier for CFSTR_PREFERREDDROPEFFECT.
/// </summary>
private static short PREFERREDDROPEFFECT = (short)(DataFormats.GetFormat(NativeMethods.CFSTR_PREFERREDDROPEFFECT).Id);
/// <summary>
/// In-order list of registered data objects.
/// </summary>
private List<DataObject> _dataObjects = new List<DataObject>();
/// <summary>
/// Tracks whether an asynchronous operation is ongoing.
/// </summary>
private bool _inOperation;
/// <summary>
/// Stores the user-specified start action.
/// </summary>
private Action<VirtualFileDataObject> _startAction;
/// <summary>
/// Stores the user-specified end action.
/// </summary>
private Action<VirtualFileDataObject> _endAction;
/// <summary>
/// Initializes a new instance of the VirtualFileDataObject class.
/// </summary>
public VirtualFileDataObject()
{
IsAsynchronous = true;
}
/// <summary>
/// Initializes a new instance of the VirtualFileDataObject class.
/// </summary>
/// <param name="startAction">Optional action to run at the start of the data transfer.</param>
/// <param name="endAction">Optional action to run at the end of the data transfer.</param>
public VirtualFileDataObject(Action<VirtualFileDataObject> startAction, Action<VirtualFileDataObject> endAction)
: this()
{
_startAction = startAction;
_endAction = endAction;
}
#region IDataObject Members
// Explicit interface implementation hides the technical details from users of VirtualFileDataObject.
/// <summary>
/// Creates a connection between a data object and an advisory sink.
/// </summary>
/// <param name="pFormatetc">A FORMATETC structure that defines the format, target device, aspect, and medium that will be used for future notifications.</param>
/// <param name="advf">One of the ADVF values that specifies a group of flags for controlling the advisory connection.</param>
/// <param name="adviseSink">A pointer to the IAdviseSink interface on the advisory sink that will receive the change notification.</param>
/// <param name="connection">When this method returns, contains a pointer to a DWORD token that identifies this connection.</param>
/// <returns>HRESULT success code.</returns>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
int System.Runtime.InteropServices.ComTypes.IDataObject.DAdvise(ref FORMATETC pFormatetc, ADVF advf, IAdviseSink adviseSink, out int connection)
{
Marshal.ThrowExceptionForHR(NativeMethods.OLE_E_ADVISENOTSUPPORTED);
throw new NotImplementedException();
}
/// <summary>
/// Destroys a notification connection that had been previously established.
/// </summary>
/// <param name="connection">A DWORD token that specifies the connection to remove.</param>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
void System.Runtime.InteropServices.ComTypes.IDataObject.DUnadvise(int connection)
{
Marshal.ThrowExceptionForHR(NativeMethods.OLE_E_ADVISENOTSUPPORTED);
throw new NotImplementedException();
}
/// <summary>
/// Creates an object that can be used to enumerate the current advisory connections.
/// </summary>
/// <param name="enumAdvise">When this method returns, contains an IEnumSTATDATA that receives the interface pointer to the new enumerator object.</param>
/// <returns>HRESULT success code.</returns>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
int System.Runtime.InteropServices.ComTypes.IDataObject.EnumDAdvise(out IEnumSTATDATA enumAdvise)
{
Marshal.ThrowExceptionForHR(NativeMethods.OLE_E_ADVISENOTSUPPORTED);
throw new NotImplementedException();
}
/// <summary>
/// Creates an object for enumerating the FORMATETC structures for a data object.
/// </summary>
/// <param name="direction">One of the DATADIR values that specifies the direction of the data.</param>
/// <returns>IEnumFORMATETC interface.</returns>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
IEnumFORMATETC System.Runtime.InteropServices.ComTypes.IDataObject.EnumFormatEtc(DATADIR direction)
{
if (direction == DATADIR.DATADIR_GET)
{
if (0 == _dataObjects.Count)
{
// Note: SHCreateStdEnumFmtEtc fails for a count of 0; throw helpful exception
throw new InvalidOperationException("VirtualFileDataObject requires at least one data object to enumerate.");
}
// Create enumerator and return it
IEnumFORMATETC enumerator;
if (NativeMethods.SUCCEEDED(NativeMethods.SHCreateStdEnumFmtEtc((uint)(_dataObjects.Count), _dataObjects.Select(d => d.FORMATETC).ToArray(), out enumerator)))
{
return enumerator;
}
// Returning null here can cause an AV in the caller; throw instead
Marshal.ThrowExceptionForHR(NativeMethods.E_FAIL);
}
throw new NotImplementedException();
}
/// <summary>
/// Provides a standard FORMATETC structure that is logically equivalent to a more complex structure.
/// </summary>
/// <param name="formatIn">A pointer to a FORMATETC structure that defines the format, medium, and target device that the caller would like to use to retrieve data in a subsequent call such as GetData.</param>
/// <param name="formatOut">When this method returns, contains a pointer to a FORMATETC structure that contains the most general information possible for a specific rendering, making it canonically equivalent to formatetIn.</param>
/// <returns>HRESULT success code.</returns>
int System.Runtime.InteropServices.ComTypes.IDataObject.GetCanonicalFormatEtc(ref FORMATETC formatIn, out FORMATETC formatOut)
{
throw new NotImplementedException();
}
/// <summary>
/// Obtains data from a source data object.
/// </summary>
/// <param name="format">A pointer to a FORMATETC structure that defines the format, medium, and target device to use when passing the data.</param>
/// <param name="medium">When this method returns, contains a pointer to the STGMEDIUM structure that indicates the storage medium containing the returned data through its tymed member, and the responsibility for releasing the medium through the value of its pUnkForRelease member.</param>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
void System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref FORMATETC format, out STGMEDIUM medium)
{
medium = new STGMEDIUM();
var hr = ((System.Runtime.InteropServices.ComTypes.IDataObject)this).QueryGetData(ref format);
if (NativeMethods.SUCCEEDED(hr))
{
// Find the best match
var formatCopy = format; // Cannot use ref or out parameter inside an anonymous method, lambda expression, or query expression
var dataObject = _dataObjects
.Where(d =>
(d.FORMATETC.cfFormat == formatCopy.cfFormat) &&
(d.FORMATETC.dwAspect == formatCopy.dwAspect) &&
(0 != (d.FORMATETC.tymed & formatCopy.tymed) &&
(d.FORMATETC.lindex == formatCopy.lindex)))
.FirstOrDefault();
if (dataObject != null)
{
if (!IsAsynchronous && (FILEDESCRIPTORW == dataObject.FORMATETC.cfFormat) && !_inOperation)
{
// Enter the operation and call the start action
_inOperation = true;
if (null != _startAction)
{
_startAction(this);
}
}
// Populate the STGMEDIUM
medium.tymed = dataObject.FORMATETC.tymed;
var result = dataObject.GetData(); // Possible call to user code
hr = result.Item2;
if (NativeMethods.SUCCEEDED(hr))
{
medium.unionmember = result.Item1;
}
}
else
{
// Couldn't find a match
hr = NativeMethods.DV_E_FORMATETC;
}
}
if (!NativeMethods.SUCCEEDED(hr)) // Not redundant; hr gets updated in the block above
{
Marshal.ThrowExceptionForHR(hr); // If you are breaking into the Visual Studio debugger here, then go to Debug menu, Exceptions, find System.Runtime.InteropServices.COMException and uncheck "when unhandled". It's supposed to be thrown here, and is handled by the drop target.
}
}
/// <summary>
/// Obtains data from a source data object.
/// </summary>
/// <param name="format">A pointer to a FORMATETC structure that defines the format, medium, and target device to use when passing the data.</param>
/// <param name="medium">A STGMEDIUM that defines the storage medium containing the data being transferred.</param>
void System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere(ref FORMATETC format, ref STGMEDIUM medium)
{
throw new NotImplementedException();
}
/// <summary>
/// Determines whether the data object is capable of rendering the data described in the FORMATETC structure.
/// </summary>
/// <param name="format">A pointer to a FORMATETC structure that defines the format, medium, and target device to use for the query.</param>
/// <returns>HRESULT success code.</returns>
int System.Runtime.InteropServices.ComTypes.IDataObject.QueryGetData(ref FORMATETC format)
{
var formatCopy = format; // Cannot use ref or out parameter inside an anonymous method, lambda expression, or query expression
var formatMatches = _dataObjects.Where(d => d.FORMATETC.cfFormat == formatCopy.cfFormat);
if (!formatMatches.Any())
{
return NativeMethods.DV_E_FORMATETC;
}
var tymedMatches = formatMatches.Where(d => 0 != (d.FORMATETC.tymed & formatCopy.tymed));
if (!tymedMatches.Any())
{
return NativeMethods.DV_E_TYMED;
}
var aspectMatches = tymedMatches.Where(d => d.FORMATETC.dwAspect == formatCopy.dwAspect);
if (!aspectMatches.Any())
{
return NativeMethods.DV_E_DVASPECT;
}
return NativeMethods.S_OK;
}
/// <summary>
/// Transfers data to the object that implements this method.
/// </summary>
/// <param name="formatIn">A FORMATETC structure that defines the format used by the data object when interpreting the data contained in the storage medium.</param>
/// <param name="medium">A STGMEDIUM structure that defines the storage medium in which the data is being passed.</param>
/// <param name="release">true to specify that the data object called, which implements SetData, owns the storage medium after the call returns.</param>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
void System.Runtime.InteropServices.ComTypes.IDataObject.SetData(ref FORMATETC formatIn, ref STGMEDIUM medium, bool release)
{
var handled = false;
if ((formatIn.dwAspect == DVASPECT.DVASPECT_CONTENT) &&
(formatIn.tymed == TYMED.TYMED_HGLOBAL) &&
(medium.tymed == formatIn.tymed))
{
// Supported format; capture the data
var ptr = NativeMethods.GlobalLock(medium.unionmember);
if (IntPtr.Zero != ptr)
{
try
{
var length = NativeMethods.GlobalSize(ptr).ToInt32();
var data = new byte[length];
Marshal.Copy(ptr, data, 0, length);
// Store it in our own format
SetData(formatIn.cfFormat, data);
handled = true;
}
finally
{
NativeMethods.GlobalUnlock(medium.unionmember);
}
}
// Release memory if we now own it
if (release)
{
Marshal.FreeHGlobal(medium.unionmember);
}
}
// Handle synchronous mode
if (!IsAsynchronous && (PERFORMEDDROPEFFECT == formatIn.cfFormat) && _inOperation)
{
// Call the end action and exit the operation
if (null != _endAction)
{
_endAction(this);
}
_inOperation = false;
}
// Throw if unhandled
if (!handled)
{
throw new NotImplementedException();
}
}
#endregion
/// <summary>
/// Provides data for the specified data format (HGLOBAL).
/// </summary>
/// <param name="dataFormat">Data format.</param>
/// <param name="data">Sequence of data.</param>
public void SetData(short dataFormat, IEnumerable<byte> data)
{
_dataObjects.Add(
new DataObject
{
FORMATETC = new FORMATETC
{
cfFormat = dataFormat,
ptd = IntPtr.Zero,
dwAspect = DVASPECT.DVASPECT_CONTENT,
lindex = -1,
tymed = TYMED.TYMED_HGLOBAL
},
GetData = () =>
{
var dataArray = data.ToArray();
var ptr = Marshal.AllocHGlobal(dataArray.Length);
Marshal.Copy(dataArray, 0, ptr, dataArray.Length);
return new Tuple<IntPtr, int>(ptr, NativeMethods.S_OK);
},
});
}
/// <summary>
/// Provides data for the specified data format and index (ISTREAM).
/// </summary>
/// <param name="dataFormat">Data format.</param>
/// <param name="index">Index of data.</param>
/// <param name="streamData">Action generating the data.</param>
/// <remarks>
/// Uses Stream instead of IEnumerable(T) because Stream is more likely
/// to be natural for the expected scenarios.
/// </remarks>
public void SetData(short dataFormat, int index, Action<Stream> streamData)
{
_dataObjects.Add(
new DataObject
{
FORMATETC = new FORMATETC
{
cfFormat = dataFormat,
ptd = IntPtr.Zero,
dwAspect = DVASPECT.DVASPECT_CONTENT,
lindex = index,
tymed = TYMED.TYMED_ISTREAM
},
GetData = () =>
{
// Create IStream for data
var ptr = IntPtr.Zero;
var iStream = NativeMethods.CreateStreamOnHGlobal(IntPtr.Zero, true);
if (streamData != null)
{
// Wrap in a .NET-friendly Stream and call provided code to fill it
using (var stream = new IStreamWrapper(iStream))
{
streamData(stream);
}
}
// Return an IntPtr for the IStream
ptr = Marshal.GetComInterfaceForObject(iStream, typeof(IStream));
Marshal.ReleaseComObject(iStream);
return new Tuple<IntPtr, int>(ptr, NativeMethods.S_OK);
},
});
}
/// <summary>
/// Provides data for the specified data format (FILEGROUPDESCRIPTOR/FILEDESCRIPTOR)
/// </summary>
/// <param name="fileDescriptors">Collection of virtual files.</param>
public void SetData(IEnumerable<FileDescriptor> fileDescriptors)
{
// Prepare buffer
var bytes = new List<byte>();
// Add FILEGROUPDESCRIPTOR header
bytes.AddRange(StructureBytes(new NativeMethods.FILEGROUPDESCRIPTOR { cItems = (uint)(fileDescriptors.Count()) }));
// Add n FILEDESCRIPTORs
foreach (var fileDescriptor in fileDescriptors)
{
// Set required fields
var FILEDESCRIPTOR = new NativeMethods.FILEDESCRIPTOR
{
cFileName = fileDescriptor.Name,
};
// Set optional timestamp
if (fileDescriptor.ChangeTimeUtc.HasValue)
{
FILEDESCRIPTOR.dwFlags |= NativeMethods.FD_CREATETIME | NativeMethods.FD_WRITESTIME;
var changeTime = fileDescriptor.ChangeTimeUtc.Value.ToLocalTime().ToFileTime();
var changeTimeFileTime = new System.Runtime.InteropServices.ComTypes.FILETIME
{
dwLowDateTime = (int)(changeTime & 0xffffffff),
dwHighDateTime = (int)(changeTime >> 32),
};
FILEDESCRIPTOR.ftLastWriteTime = changeTimeFileTime;
FILEDESCRIPTOR.ftCreationTime = changeTimeFileTime;
}
// Set optional length
if (fileDescriptor.Length.HasValue)
{
FILEDESCRIPTOR.dwFlags |= NativeMethods.FD_FILESIZE;
FILEDESCRIPTOR.nFileSizeLow = (uint)(fileDescriptor.Length & 0xffffffff);
FILEDESCRIPTOR.nFileSizeHigh = (uint)(fileDescriptor.Length >> 32);
}
// Add structure to buffer
bytes.AddRange(StructureBytes(FILEDESCRIPTOR));
}
// Set CFSTR_FILEDESCRIPTORW
SetData(FILEDESCRIPTORW, bytes);
// Set n CFSTR_FILECONTENTS
var index = 0;
foreach (var fileDescriptor in fileDescriptors)
{
SetData(FILECONTENTS, index, fileDescriptor.StreamContents);
index++;
}
}
/// <summary>
/// Gets or sets the CFSTR_PASTESUCCEEDED value for the object.
/// </summary>
public DragDropEffects? PasteSucceeded
{
get { return GetDropEffect(PASTESUCCEEDED); }
set { SetData(PASTESUCCEEDED, BitConverter.GetBytes((UInt32)value)); }
}
/// <summary>
/// Gets or sets the CFSTR_PERFORMEDDROPEFFECT value for the object.
/// </summary>
public DragDropEffects? PerformedDropEffect
{
get { return GetDropEffect(PERFORMEDDROPEFFECT); }
set { SetData(PERFORMEDDROPEFFECT, BitConverter.GetBytes((UInt32)value)); }
}
/// <summary>
/// Gets or sets the CFSTR_PREFERREDDROPEFFECT value for the object.
/// </summary>
public DragDropEffects? PreferredDropEffect
{
get { return GetDropEffect(PREFERREDDROPEFFECT); }
set { SetData(PREFERREDDROPEFFECT, BitConverter.GetBytes((UInt32)value)); }
}
/// <summary>
/// Gets the DragDropEffects value (if any) previously set on the object.
/// </summary>
/// <param name="format">Clipboard format.</param>
/// <returns>DragDropEffects value or null.</returns>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
private DragDropEffects? GetDropEffect(short format)
{
// Get the most recent setting
var dataObject = _dataObjects
.Where(d =>
(format == d.FORMATETC.cfFormat) &&
(DVASPECT.DVASPECT_CONTENT == d.FORMATETC.dwAspect) &&
(TYMED.TYMED_HGLOBAL == d.FORMATETC.tymed))
.LastOrDefault();
if (null != dataObject)
{
// Read the value and return it
var result = dataObject.GetData();
if (NativeMethods.SUCCEEDED(result.Item2))
{
var ptr = NativeMethods.GlobalLock(result.Item1);
if (IntPtr.Zero != ptr)
{
try
{
var length = NativeMethods.GlobalSize(ptr).ToInt32();
if (4 == length)
{
var data = new byte[length];
Marshal.Copy(ptr, data, 0, length);
return (DragDropEffects)(BitConverter.ToUInt32(data, 0));
}
}
finally
{
NativeMethods.GlobalUnlock(result.Item1);
}
}
}
}
return null;
}
#region IAsyncOperation Members
// Explicit interface implementation hides the technical details from users of VirtualFileDataObject.
/// <summary>
/// Called by a drop source to specify whether the data object supports asynchronous data extraction.
/// </summary>
/// <param name="fDoOpAsync">A Boolean value that is set to VARIANT_TRUE to indicate that an asynchronous operation is supported, or VARIANT_FALSE otherwise.</param>
void IAsyncOperation.SetAsyncMode(int fDoOpAsync)
{
IsAsynchronous = !(NativeMethods.VARIANT_FALSE == fDoOpAsync);
}
/// <summary>
/// Called by a drop target to determine whether the data object supports asynchronous data extraction.
/// </summary>
/// <param name="pfIsOpAsync">A Boolean value that is set to VARIANT_TRUE to indicate that an asynchronous operation is supported, or VARIANT_FALSE otherwise.</param>
void IAsyncOperation.GetAsyncMode(out int pfIsOpAsync)
{
pfIsOpAsync = IsAsynchronous ? NativeMethods.VARIANT_TRUE : NativeMethods.VARIANT_FALSE;
}
/// <summary>
/// Called by a drop target to indicate that asynchronous data extraction is starting.
/// </summary>
/// <param name="pbcReserved">Reserved. Set this value to NULL.</param>
void IAsyncOperation.StartOperation(IBindCtx pbcReserved)
{
_inOperation = true;
if (null != _startAction)
{
_startAction(this);
}
}
/// <summary>
/// Called by the drop source to determine whether the target is extracting data asynchronously.
/// </summary>
/// <param name="pfInAsyncOp">Set to VARIANT_TRUE if data extraction is being handled asynchronously, or VARIANT_FALSE otherwise.</param>
void IAsyncOperation.InOperation(out int pfInAsyncOp)
{
pfInAsyncOp = _inOperation ? NativeMethods.VARIANT_TRUE : NativeMethods.VARIANT_FALSE;
}
/// <summary>
/// Notifies the data object that that asynchronous data extraction has ended.
/// </summary>
/// <param name="hResult">An HRESULT value that indicates the outcome of the data extraction. Set to S_OK if successful, or a COM error code otherwise.</param>
/// <param name="pbcReserved">Reserved. Set to NULL.</param>
/// <param name="dwEffects">A DROPEFFECT value that indicates the result of an optimized move. This should be the same value that would be passed to the data object as a CFSTR_PERFORMEDDROPEFFECT format with a normal data extraction operation.</param>
void IAsyncOperation.EndOperation(int hResult, IBindCtx pbcReserved, uint dwEffects)
{
if (null != _endAction)
{
_endAction(this);
}
_inOperation = false;
}
#endregion
/// <summary>
/// Returns the in-memory representation of an interop structure.
/// </summary>
/// <param name="source">Structure to return.</param>
/// <returns>In-memory representation of structure.</returns>
[SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Method doesn't decrease security.")]
private static IEnumerable<byte> StructureBytes(object source)
{
// Set up for call to StructureToPtr
var size = Marshal.SizeOf(source.GetType());
var ptr = Marshal.AllocHGlobal(size);
var bytes = new byte[size];
try
{
Marshal.StructureToPtr(source, ptr, false);
// Copy marshalled bytes to buffer
Marshal.Copy(ptr, bytes, 0, size);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
return bytes;
}
/// <summary>
/// Class representing a virtual file for use by drag/drop or the clipboard.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible", Justification = "Deliberate to provide obvious coupling.")]
public class FileDescriptor
{
/// <summary>
/// Gets or sets the name of the file.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the (optional) length of the file.
/// </summary>
public Int64? Length { get; set; }
/// <summary>
/// Gets or sets the (optional) change time of the file.
/// </summary>
public DateTime? ChangeTimeUtc { get; set; }
/// <summary>
/// Gets or sets an Action that returns the contents of the file.
/// </summary>
public Action<Stream> StreamContents { get; set; }
}
/// <summary>
/// Class representing the result of a SetData call.
/// </summary>
private class DataObject
{
/// <summary>
/// FORMATETC structure for the data.
/// </summary>
public FORMATETC FORMATETC { get; set; }
/// <summary>
/// Func returning the data as an IntPtr and an HRESULT success code.
/// </summary>
public Func<Tuple<IntPtr, int>> GetData { get; set; }
}
/// <summary>
/// Represents a 2-tuple, or pair.
/// </summary>
/// <remarks>
/// Minimal implementation of the .NET 4 Tuple class; remove if running on .NET 4.
/// </remarks>
/// <typeparam name="T1">The type of the tuple's first component.</typeparam>
/// <typeparam name="T2">The type of the tuple's second component.</typeparam>
private class Tuple<T1, T2>
{
/// <summary>
/// Gets the value of the current Tuple(T1, T2) object's first component.
/// </summary>
public T1 Item1 { get; private set; }
/// <summary>
/// Gets the value of the current Tuple(T1, T2) object's second component.
/// </summary>
public T2 Item2 { get; private set; }
/// <summary>
/// Initializes a new instance of the Tuple(T1, T2) class.
/// </summary>
/// <param name="item1">The value of the tuple's first component.</param>
/// <param name="item2">The value of the tuple's second component.</param>
public Tuple(T1 item1, T2 item2)
{
Item1 = item1;
Item2 = item2;
}
}
/// <summary>
/// Simple class that exposes a write-only IStream as a Stream.
/// </summary>
private class IStreamWrapper : Stream
{
/// <summary>
/// IStream instance being wrapped.
/// </summary>
private IStream _iStream;
/// <summary>
/// Initializes a new instance of the IStreamWrapper class.
/// </summary>
/// <param name="iStream">IStream instance to wrap.</param>
public IStreamWrapper(IStream iStream)
{
_iStream = iStream;
}
/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// </summary>
public override bool CanRead
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// </summary>
public override bool CanSeek
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// </summary>
public override bool CanWrite
{
get { return true; }
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the origin parameter.</param>
/// <param name="origin">A value of type SeekOrigin indicating the reference point used to obtain the new position.</param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <summary>
/// Sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
/// <param name="count">The number of bytes to be written to the current stream.</param>
public override void Write(byte[] buffer, int offset, int count)
{
if (offset == 0)
{
// Optimize common case to avoid creating extra buffers
_iStream.Write(buffer, count, IntPtr.Zero);
}
else
{
// Easy way to provide the relevant byte[]
_iStream.Write(buffer.Skip(offset).ToArray(), count, IntPtr.Zero);
}
}
}
/// <summary>
/// Initiates a drag-and-drop operation.
/// </summary>
/// <param name="dragSource">A reference to the dependency object that is the source of the data being dragged.</param>
/// <param name="dataObject">A data object that contains the data being dragged.</param>
/// <param name="allowedEffects">One of the DragDropEffects values that specifies permitted effects of the drag-and-drop operation.</param>
/// <returns>One of the DragDropEffects values that specifies the final effect that was performed during the drag-and-drop operation.</returns>
/// <remarks>
/// Call this method instead of System.Windows.DragDrop.DoDragDrop because this method handles IDataObject better.
/// </remarks>
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "dragSource", Justification = "Parameter is present so the signature matches that of System.Windows.DragDrop.DoDragDrop.")]
public static DragDropEffects DoDragDrop(/*DependencyObject dragSource, */System.Runtime.InteropServices.ComTypes.IDataObject dataObject, DragDropEffects allowedEffects)
{
int[] finalEffect = new int[1];
try
{
NativeMethods.DoDragDrop(dataObject, new DropSource(), (int)allowedEffects, finalEffect);
}
finally
{
var virtualFileDataObject = dataObject as VirtualFileDataObject;
if ((null != virtualFileDataObject) && !virtualFileDataObject.IsAsynchronous && virtualFileDataObject._inOperation)
{
// Call the end action and exit the operation
if (null != virtualFileDataObject._endAction)
{
virtualFileDataObject._endAction(virtualFileDataObject);
}
virtualFileDataObject._inOperation = false;
}
}
return (DragDropEffects)(finalEffect[0]);
}
/// <summary>
/// Contains the methods for generating visual feedback to the end user and for canceling or completing the drag-and-drop operation.
/// </summary>
private class DropSource : NativeMethods.IDropSource
{
/// <summary>
/// Determines whether a drag-and-drop operation should continue.
/// </summary>
/// <param name="fEscapePressed">Indicates whether the Esc key has been pressed since the previous call to QueryContinueDrag or to DoDragDrop if this is the first call to QueryContinueDrag. A TRUE value indicates the end user has pressed the escape key; a FALSE value indicates it has not been pressed.</param>
/// <param name="grfKeyState">The current state of the keyboard modifier keys on the keyboard. Possible values can be a combination of any of the flags MK_CONTROL, MK_SHIFT, MK_ALT, MK_BUTTON, MK_LBUTTON, MK_MBUTTON, and MK_RBUTTON.</param>
/// <returns>This method returns S_OK/DRAGDROP_S_DROP/DRAGDROP_S_CANCEL on success.</returns>
public int QueryContinueDrag(int fEscapePressed, uint grfKeyState)
{
var escapePressed = (0 != fEscapePressed);
/*var keyStates = (DragDropKeyStates)grfKeyState;*/
if (escapePressed)
{
return NativeMethods.DRAGDROP_S_CANCEL;
}
else if (grfKeyState == 1 // 1 is left button.
/*DragDropKeyStates.None == (keyStates & DragDropKeyStates.LeftMouseButton)*/)
{
return NativeMethods.DRAGDROP_S_DROP;
}
return NativeMethods.S_OK;
}
/// <summary>
/// Gives visual feedback to an end user during a drag-and-drop operation.
/// </summary>
/// <param name="dwEffect">The DROPEFFECT value returned by the most recent call to IDropTarget::DragEnter, IDropTarget::DragOver, or IDropTarget::DragLeave. </param>
/// <returns>This method returns S_OK on success.</returns>
public int GiveFeedback(uint dwEffect)
{
return NativeMethods.DRAGDROP_S_USEDEFAULTCURSORS;
}
}
/// <summary>
/// Provides access to Win32-level constants, structures, and functions.
/// </summary>
private static class NativeMethods
{
public const int DRAGDROP_S_DROP = 0x00040100;
public const int DRAGDROP_S_CANCEL = 0x00040101;
public const int DRAGDROP_S_USEDEFAULTCURSORS = 0x00040102;
public const int DV_E_DVASPECT = -2147221397;
public const int DV_E_FORMATETC = -2147221404;
public const int DV_E_TYMED = -2147221399;
public const int E_FAIL = -2147467259;
public const uint FD_CREATETIME = 0x00000008;
public const uint FD_WRITESTIME = 0x00000020;
public const uint FD_FILESIZE = 0x00000040;
public const int OLE_E_ADVISENOTSUPPORTED = -2147221501;
public const int S_OK = 0;
public const int S_FALSE = 1;
public const int VARIANT_FALSE = 0;
public const int VARIANT_TRUE = -1;
public const string CFSTR_FILECONTENTS = "FileContents";
public const string CFSTR_FILEDESCRIPTORW = "FileGroupDescriptorW";
public const string CFSTR_PASTESUCCEEDED = "Paste Succeeded";
public const string CFSTR_PERFORMEDDROPEFFECT = "Performed DropEffect";
public const string CFSTR_PREFERREDDROPEFFECT = "Preferred DropEffect";
[SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "Structure exists for interop.")]
[StructLayout(LayoutKind.Sequential)]
public struct FILEGROUPDESCRIPTOR
{
public UInt32 cItems;
// Followed by 0 or more FILEDESCRIPTORs
}
[SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "Structure exists for interop.")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct FILEDESCRIPTOR
{
public UInt32 dwFlags;
public Guid clsid;
public Int32 sizelcx;
public Int32 sizelcy;
public Int32 pointlx;
public Int32 pointly;
public UInt32 dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public UInt32 nFileSizeHigh;
public UInt32 nFileSizeLow;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
}
[ComImport]
[Guid("00000121-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDropSource
{
[PreserveSig]
int QueryContinueDrag(int fEscapePressed, uint grfKeyState);
[PreserveSig]
int GiveFeedback(uint dwEffect);
}
[SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "2#", Justification = "Win32 API.")]
[DllImport("shell32.dll")]
public static extern int SHCreateStdEnumFmtEtc(uint cfmt, FORMATETC[] afmt, out IEnumFORMATETC ppenumFormatEtc);
[return: MarshalAs(UnmanagedType.Interface)]
[DllImport("ole32.dll", PreserveSig = false)]
public static extern IStream CreateStreamOnHGlobal(IntPtr hGlobal, [MarshalAs(UnmanagedType.Bool)] bool fDeleteOnRelease);
[DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true, PreserveSig = false)]
public static extern void DoDragDrop(System.Runtime.InteropServices.ComTypes.IDataObject dataObject, IDropSource dropSource, int allowedEffects, int[] finalEffect);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr hMem);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll")]
public static extern bool GlobalUnlock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalSize(IntPtr handle);
/// <summary>
/// Returns true iff the HRESULT is a success code.
/// </summary>
/// <param name="hr">HRESULT to check.</param>
/// <returns>True iff a success code.</returns>
public static bool SUCCEEDED(int hr)
{
return (0 <= hr);
}
}
}
/// <summary>
/// Definition of the IAsyncOperation COM interface.
/// </summary>
/// <remarks>
/// Pseudo-public because VirtualFileDataObject implements it.
/// </remarks>
[ComImport]
[Guid("3D8B0590-F691-11d2-8EA9-006097DF5BD4")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IAsyncOperation
{
void SetAsyncMode([In] Int32 fDoOpAsync);
void GetAsyncMode([Out] out Int32 pfIsOpAsync);
void StartOperation([In] IBindCtx pbcReserved);
void InOperation([Out] out Int32 pfInAsyncOp);
void EndOperation([In] Int32 hResult, [In] IBindCtx pbcReserved, [In] UInt32 dwEffects);
}
}