forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataObject.cs
More file actions
581 lines (497 loc) · 18.3 KB
/
Copy pathDataObject.cs
File metadata and controls
581 lines (497 loc) · 18.3 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
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* vspython@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
* ***************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
namespace Microsoft.VisualStudioTools.Project
{
internal enum tagDVASPECT
{
DVASPECT_CONTENT = 1,
DVASPECT_THUMBNAIL = 2,
DVASPECT_ICON = 4,
DVASPECT_DOCPRINT = 8
}
internal enum tagTYMED
{
TYMED_HGLOBAL = 1,
TYMED_FILE = 2,
TYMED_ISTREAM = 4,
TYMED_ISTORAGE = 8,
TYMED_GDI = 16,
TYMED_MFPICT = 32,
TYMED_ENHMF = 64,
TYMED_NULL = 0
}
internal sealed class DataCacheEntry : IDisposable
{
#region fields
/// <summary>
/// Defines an object that will be a mutex for this object for synchronizing thread calls.
/// </summary>
private static volatile object Mutex = new object();
private FORMATETC format;
private long data;
private DATADIR dataDir;
private bool isDisposed;
#endregion
#region properties
internal FORMATETC Format
{
get
{
return this.format;
}
}
internal long Data
{
get
{
return this.data;
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal DATADIR DataDir
{
get
{
return this.dataDir;
}
}
#endregion
/// <summary>
/// The IntPtr is data allocated that should be removed. It is allocated by the ProcessSelectionData method.
/// </summary>
internal DataCacheEntry(FORMATETC fmt, IntPtr data, DATADIR dir)
{
this.format = fmt;
this.data = (long)data;
this.dataDir = dir;
}
#region Dispose
~DataCacheEntry()
{
Dispose(false);
}
/// <summary>
/// The IDispose interface Dispose method for disposing the object determinastically.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// The method that does the cleanup.
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
// Everybody can go here.
if (!this.isDisposed)
{
// Synchronize calls to the Dispose simulteniously.
lock (Mutex)
{
if (disposing && this.data != 0)
{
Marshal.FreeHGlobal((IntPtr)this.data);
this.data = 0;
}
this.isDisposed = true;
}
}
}
#endregion
}
/// <summary>
/// Unfortunately System.Windows.Forms.IDataObject and
/// Microsoft.VisualStudio.OLE.Interop.IDataObject are different...
/// </summary>
internal sealed class DataObject : IDataObject
{
#region fields
internal const int DATA_S_SAMEFORMATETC = 0x00040130;
EventSinkCollection map;
ArrayList entries;
#endregion
internal DataObject()
{
this.map = new EventSinkCollection();
this.entries = new ArrayList();
}
internal void SetData(FORMATETC format, IntPtr data)
{
this.entries.Add(new DataCacheEntry(format, data, DATADIR.DATADIR_SET));
}
#region IDataObject methods
int IDataObject.DAdvise(FORMATETC[] e, uint adv, IAdviseSink sink, out uint cookie)
{
Utilities.ArgumentNotNull("e", e);
STATDATA sdata = new STATDATA();
sdata.ADVF = adv;
sdata.FORMATETC = e[0];
sdata.pAdvSink = sink;
cookie = this.map.Add(sdata);
sdata.dwConnection = cookie;
return 0;
}
void IDataObject.DUnadvise(uint cookie)
{
this.map.RemoveAt(cookie);
}
int IDataObject.EnumDAdvise(out IEnumSTATDATA e)
{
e = new EnumSTATDATA((IEnumerable)this.map);
return 0; //??
}
int IDataObject.EnumFormatEtc(uint direction, out IEnumFORMATETC penum)
{
penum = new EnumFORMATETC((DATADIR)direction, (IEnumerable)this.entries);
return 0;
}
int IDataObject.GetCanonicalFormatEtc(FORMATETC[] format, FORMATETC[] fmt)
{
throw new System.Runtime.InteropServices.COMException("", DATA_S_SAMEFORMATETC);
}
void IDataObject.GetData(FORMATETC[] fmt, STGMEDIUM[] m)
{
STGMEDIUM retMedium = new STGMEDIUM();
if (fmt == null || fmt.Length < 1)
return;
foreach (DataCacheEntry e in this.entries)
{
if (e.Format.cfFormat == fmt[0].cfFormat /*|| fmt[0].cfFormat == InternalNativeMethods.CF_HDROP*/)
{
retMedium.tymed = e.Format.tymed;
// Caller must delete the memory.
retMedium.unionmember = DragDropHelper.CopyHGlobal(new IntPtr(e.Data));
break;
}
}
if (m != null && m.Length > 0)
m[0] = retMedium;
}
void IDataObject.GetDataHere(FORMATETC[] fmt, STGMEDIUM[] m)
{
}
int IDataObject.QueryGetData(FORMATETC[] fmt)
{
if (fmt == null || fmt.Length < 1)
return VSConstants.S_FALSE;
foreach (DataCacheEntry e in this.entries)
{
if (e.Format.cfFormat == fmt[0].cfFormat /*|| fmt[0].cfFormat == InternalNativeMethods.CF_HDROP*/)
return VSConstants.S_OK;
}
return VSConstants.S_FALSE;
}
void IDataObject.SetData(FORMATETC[] fmt, STGMEDIUM[] m, int fRelease)
{
}
#endregion
}
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
internal static class DragDropHelper
{
#pragma warning disable 414
internal static readonly ushort CF_VSREFPROJECTITEMS;
internal static readonly ushort CF_VSSTGPROJECTITEMS;
internal static readonly ushort CF_VSPROJECTCLIPDESCRIPTOR;
#pragma warning restore 414
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
static DragDropHelper()
{
CF_VSREFPROJECTITEMS = UnsafeNativeMethods.RegisterClipboardFormat("CF_VSREFPROJECTITEMS");
CF_VSSTGPROJECTITEMS = UnsafeNativeMethods.RegisterClipboardFormat("CF_VSSTGPROJECTITEMS");
CF_VSPROJECTCLIPDESCRIPTOR = UnsafeNativeMethods.RegisterClipboardFormat("CF_PROJECTCLIPBOARDDESCRIPTOR");
}
public static FORMATETC CreateFormatEtc(ushort iFormat)
{
FORMATETC fmt = new FORMATETC();
fmt.cfFormat = iFormat;
fmt.ptd = IntPtr.Zero;
fmt.dwAspect = (uint)DVASPECT.DVASPECT_CONTENT;
fmt.lindex = -1;
fmt.tymed = (uint)TYMED.TYMED_HGLOBAL;
return fmt;
}
public static int QueryGetData(Microsoft.VisualStudio.OLE.Interop.IDataObject pDataObject, ref FORMATETC fmtetc)
{
FORMATETC[] af = new FORMATETC[1];
af[0] = fmtetc;
int result = pDataObject.QueryGetData(af);
if (result == VSConstants.S_OK)
{
fmtetc = af[0];
return VSConstants.S_OK;
}
return result;
}
public static STGMEDIUM GetData(Microsoft.VisualStudio.OLE.Interop.IDataObject pDataObject, ref FORMATETC fmtetc)
{
FORMATETC[] af = new FORMATETC[1];
af[0] = fmtetc;
STGMEDIUM[] sm = new STGMEDIUM[1];
pDataObject.GetData(af, sm);
fmtetc = af[0];
return sm[0];
}
/// <summary>
/// Retrives data from a VS format.
/// </summary>
public static List<string> GetDroppedFiles(ushort format, Microsoft.VisualStudio.OLE.Interop.IDataObject dataObject, out DropDataType ddt)
{
ddt = DropDataType.None;
List<string> droppedFiles = new List<string>();
// try HDROP
FORMATETC fmtetc = CreateFormatEtc(format);
if (QueryGetData(dataObject, ref fmtetc) == VSConstants.S_OK)
{
STGMEDIUM stgmedium = DragDropHelper.GetData(dataObject, ref fmtetc);
if (stgmedium.tymed == (uint)TYMED.TYMED_HGLOBAL)
{
// We are releasing the cloned hglobal here.
IntPtr dropInfoHandle = stgmedium.unionmember;
if (dropInfoHandle != IntPtr.Zero)
{
ddt = DropDataType.Shell;
try
{
uint numFiles = UnsafeNativeMethods.DragQueryFile(dropInfoHandle, 0xFFFFFFFF, null, 0);
// We are a directory based project thus a projref string is placed on the clipboard.
// We assign the maximum length of a projref string.
// The format of a projref is : <Proj Guid>|<project rel path>|<file path>
uint lenght = (uint)Guid.Empty.ToString().Length + 2 * NativeMethods.MAX_PATH + 2;
char[] moniker = new char[lenght + 1];
for (uint fileIndex = 0; fileIndex < numFiles; fileIndex++)
{
uint queryFileLength = UnsafeNativeMethods.DragQueryFile(dropInfoHandle, fileIndex, moniker, lenght);
string filename = new String(moniker, 0, (int)queryFileLength);
droppedFiles.Add(filename);
}
}
finally
{
Marshal.FreeHGlobal(dropInfoHandle);
}
}
}
}
return droppedFiles;
}
public static string GetSourceProjectPath(Microsoft.VisualStudio.OLE.Interop.IDataObject dataObject)
{
string projectPath = null;
FORMATETC fmtetc = CreateFormatEtc(CF_VSPROJECTCLIPDESCRIPTOR);
if (QueryGetData(dataObject, ref fmtetc) == VSConstants.S_OK)
{
STGMEDIUM stgmedium = DragDropHelper.GetData(dataObject, ref fmtetc);
if (stgmedium.tymed == (uint)TYMED.TYMED_HGLOBAL)
{
// We are releasing the cloned hglobal here.
IntPtr dropInfoHandle = stgmedium.unionmember;
if (dropInfoHandle != IntPtr.Zero)
{
try
{
string path = GetData(dropInfoHandle);
// Clone the path that we can release our memory.
if (!String.IsNullOrEmpty(path))
{
projectPath = String.Copy(path);
}
}
finally
{
Marshal.FreeHGlobal(dropInfoHandle);
}
}
}
}
return projectPath;
}
/// <summary>
/// Returns the data packed after the DROPFILES structure.
/// </summary>
/// <param name="dropHandle"></param>
/// <returns></returns>
internal static string GetData(IntPtr dropHandle)
{
IntPtr data = UnsafeNativeMethods.GlobalLock(dropHandle);
try
{
_DROPFILES df = (_DROPFILES)Marshal.PtrToStructure(data, typeof(_DROPFILES));
if (df.fWide != 0)
{
IntPtr pdata = new IntPtr((long)data + df.pFiles);
return Marshal.PtrToStringUni(pdata);
}
}
finally
{
// data is an IntPtr struct; no need for nullcheck.
UnsafeNativeMethods.GlobalUnLock(data);
}
return null;
}
internal static IntPtr CopyHGlobal(IntPtr data)
{
IntPtr src = UnsafeNativeMethods.GlobalLock(data);
int size = UnsafeNativeMethods.GlobalSize(data);
IntPtr ptr = Marshal.AllocHGlobal(size);
IntPtr buffer = UnsafeNativeMethods.GlobalLock(ptr);
try
{
for (int i = 0; i < size; i++)
{
byte val = Marshal.ReadByte(new IntPtr((long)src + i));
Marshal.WriteByte(new IntPtr((long)buffer + i), val);
}
}
finally
{
if (buffer != IntPtr.Zero)
{
UnsafeNativeMethods.GlobalUnLock(buffer);
}
if (src != IntPtr.Zero)
{
UnsafeNativeMethods.GlobalUnLock(src);
}
}
return ptr;
}
internal static void CopyStringToHGlobal(string s, IntPtr data, int bufferSize)
{
Int16 nullTerminator = 0;
int dwSize = Marshal.SizeOf(nullTerminator);
if ((s.Length + 1) * Marshal.SizeOf(s[0]) > bufferSize)
throw new System.IO.InternalBufferOverflowException();
// IntPtr memory already locked...
for (int i = 0, len = s.Length; i < len; i++)
{
Marshal.WriteInt16(data, i * dwSize, s[i]);
}
// NULL terminate it
Marshal.WriteInt16(new IntPtr((long)data + (s.Length * dwSize)), nullTerminator);
}
} // end of dragdrophelper
internal class EnumSTATDATA : IEnumSTATDATA
{
IEnumerable i;
IEnumerator e;
public EnumSTATDATA(IEnumerable i)
{
this.i = i;
this.e = i.GetEnumerator();
}
void IEnumSTATDATA.Clone(out IEnumSTATDATA clone)
{
clone = new EnumSTATDATA(i);
}
int IEnumSTATDATA.Next(uint celt, STATDATA[] d, out uint fetched)
{
uint rc = 0;
//uint size = (fetched != null) ? fetched[0] : 0;
for (uint i = 0; i < celt; i++)
{
if (e.MoveNext())
{
STATDATA sdata = (STATDATA)e.Current;
rc++;
if (d != null && d.Length > i)
{
d[i] = sdata;
}
}
}
fetched = rc;
return 0;
}
int IEnumSTATDATA.Reset()
{
e.Reset();
return 0;
}
int IEnumSTATDATA.Skip(uint celt)
{
for (uint i = 0; i < celt; i++)
{
e.MoveNext();
}
return 0;
}
}
internal class EnumFORMATETC : IEnumFORMATETC
{
IEnumerable cache; // of DataCacheEntrys.
DATADIR dir;
IEnumerator e;
public EnumFORMATETC(DATADIR dir, IEnumerable cache)
{
this.cache = cache;
this.dir = dir;
e = cache.GetEnumerator();
}
void IEnumFORMATETC.Clone(out IEnumFORMATETC clone)
{
clone = new EnumFORMATETC(dir, cache);
}
int IEnumFORMATETC.Next(uint celt, FORMATETC[] d, uint[] fetched)
{
uint rc = 0;
//uint size = (fetched != null) ? fetched[0] : 0;
for (uint i = 0; i < celt; i++)
{
if (e.MoveNext())
{
DataCacheEntry entry = (DataCacheEntry)e.Current;
rc++;
if (d != null && d.Length > i)
{
d[i] = entry.Format;
}
}
else
{
return VSConstants.S_FALSE;
}
}
if (fetched != null && fetched.Length > 0)
fetched[0] = rc;
return VSConstants.S_OK;
}
int IEnumFORMATETC.Reset()
{
e.Reset();
return 0;
}
int IEnumFORMATETC.Skip(uint celt)
{
for (uint i = 0; i < celt; i++)
{
e.MoveNext();
}
return 0;
}
}
}