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
| Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Imports System.Text
Imports System.Reflection
Imports System.Windows
Namespace WpfApplication1
Public Class OutlookDataObject
Implements System.Windows.IDataObject
#Region "NativeMethods"
Private Class NativeMethods
<DllImport("kernel32.dll")>
Private Shared Function GlobalLock(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("ole32.dll", PreserveSig:=False)>
Public Shared Function CreateILockBytesOnHGlobal(ByVal hGlobal As IntPtr, ByVal fDeleteOnRelease As Boolean) As ILockBytes
End Function
<DllImport("OLE32.DLL", CharSet:=CharSet.Auto, PreserveSig:=False)>
Public Shared Function GetHGlobalFromILockBytes(ByVal pLockBytes As ILockBytes) As IntPtr
End Function
<DllImport("OLE32.DLL", CharSet:=CharSet.Unicode, PreserveSig:=False)>
Public Shared Function StgCreateDocfileOnILockBytes(ByVal plkbyt As ILockBytes, ByVal grfMode As UInteger, ByVal reserved As UInteger) As IStorage
End Function
<ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000000B-0000-0000-C000-000000000046")>
Public Interface IStorage
'<MarshalAs(UnmanagedType.Interface)>
Function CreateStream(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfMode As Integer,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved1 As Integer,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved2 As Integer) As IStream
'<MarshalAs(UnmanagedType.Interface)>
Function OpenStream(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String, ByVal reserved1 As IntPtr,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfMode As Integer,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved2 As Integer) As IStream
'<MarshalAs(UnmanagedType.Interface)>
Function CreateStorage(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfMode As Integer,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved1 As Integer,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved2 As Integer) As IStorage
'<MarshalAs(UnmanagedType.Interface)>
Function OpenStorage(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String, ByVal pstgPriority As IntPtr,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfMode As Integer, ByVal snbExclude As IntPtr,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved As Integer) As IStorage
Sub CopyTo(ByVal ciidExclude As Integer,
<[In], MarshalAs(UnmanagedType.LPArray)> ByVal pIIDExclude As Guid(), ByVal snbExclude As IntPtr,
<[In], MarshalAs(UnmanagedType.Interface)> ByVal stgDest As IStorage)
Sub MoveElementTo(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String,
<[In], MarshalAs(UnmanagedType.Interface)> ByVal stgDest As IStorage,
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsNewName As String,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfFlags As Integer)
Sub Commit(ByVal grfCommitFlags As Integer)
Sub Revert()
Sub EnumElements(
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved1 As Integer, ByVal reserved2 As IntPtr,
<[In], MarshalAs(UnmanagedType.U4)> ByVal reserved3 As Integer, <Out>
<MarshalAs(UnmanagedType.Interface)> ByRef ppVal As Object)
Sub DestroyElement(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String)
Sub RenameElement(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsOldName As String,
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsNewName As String)
Sub SetElementTimes(
<[In], MarshalAs(UnmanagedType.BStr)> ByVal pwcsName As String,
<[In]> ByVal pctime As System.Runtime.InteropServices.ComTypes.FILETIME,
<[In]> ByVal patime As System.Runtime.InteropServices.ComTypes.FILETIME,
<[In]> ByVal pmtime As System.Runtime.InteropServices.ComTypes.FILETIME)
Sub SetClass(
<[In]> ByRef clsid As Guid)
Sub SetStateBits(ByVal grfStateBits As Integer, ByVal grfMask As Integer)
Sub Stat(
<Out> ByRef pStatStg As System.Runtime.InteropServices.ComTypes.STATSTG, ByVal grfStatFlag As Integer)
End Interface
<ComImport, Guid("0000000A-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface ILockBytes
Sub ReadAt(
<[In], MarshalAs(UnmanagedType.U8)> ByVal ulOffset As Long,
<Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=1)> ByVal pv As Byte(),
<[In], MarshalAs(UnmanagedType.U4)> ByVal cb As Integer,
<Out, MarshalAs(UnmanagedType.LPArray)> ByVal pcbRead As Integer())
Sub WriteAt(
<[In], MarshalAs(UnmanagedType.U8)> ByVal ulOffset As Long, ByVal pv As IntPtr,
<[In], MarshalAs(UnmanagedType.U4)> ByVal cb As Integer,
<Out, MarshalAs(UnmanagedType.LPArray)> ByVal pcbWritten As Integer())
Sub Flush()
Sub SetSize(
<[In], MarshalAs(UnmanagedType.U8)> ByVal cb As Long)
Sub LockRegion(
<[In], MarshalAs(UnmanagedType.U8)> ByVal libOffset As Long,
<[In], MarshalAs(UnmanagedType.U8)> ByVal cb As Long,
<[In], MarshalAs(UnmanagedType.U4)> ByVal dwLockType As Integer)
Sub UnlockRegion(
<[In], MarshalAs(UnmanagedType.U8)> ByVal libOffset As Long,
<[In], MarshalAs(UnmanagedType.U8)> ByVal cb As Long,
<[In], MarshalAs(UnmanagedType.U4)> ByVal dwLockType As Integer)
Sub Stat(
<Out> ByRef pstatstg As System.Runtime.InteropServices.ComTypes.STATSTG,
<[In], MarshalAs(UnmanagedType.U4)> ByVal grfStatFlag As Integer)
End Interface
<StructLayout(LayoutKind.Sequential)>
Public NotInheritable Class POINTL
Public x As Integer
Public y As Integer
End Class
<StructLayout(LayoutKind.Sequential)>
Public NotInheritable Class SIZEL
Public cx As Integer
Public cy As Integer
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public NotInheritable Class FILEGROUPDESCRIPTORA
Public cItems As UInteger
Public fgd As FILEDESCRIPTORA
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public NotInheritable Class FILEDESCRIPTORA
Public dwFlags As UInteger
Public clsid As Guid
Public sizel As SIZEL
Public pointl As POINTL
Public dwFileAttributes As UInteger
Public ftCreationTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastAccessTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastWriteTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public nFileSizeHigh As UInteger
Public nFileSizeLow As UInteger
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
Public cFileName As String
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public NotInheritable Class FILEGROUPDESCRIPTORW
Public cItems As UInteger
Public fgd As FILEDESCRIPTORW
End Class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public NotInheritable Class FILEDESCRIPTORW
Public dwFlags As UInteger
Public clsid As Guid
Public sizel As SIZEL
Public pointl As POINTL
Public dwFileAttributes As UInteger
Public ftCreationTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastAccessTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public ftLastWriteTime As System.Runtime.InteropServices.ComTypes.FILETIME
Public nFileSizeHigh As UInteger
Public nFileSizeLow As UInteger
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)>
Public cFileName As String
End Class
End Class
#End Region
#Region "Property(s)"
''' <summary>
''' Holds the <seecref="System.Windows.IDataObject"/> that this class is wrapping
''' </summary>
Private underlyingDataObject As System.Windows.IDataObject
''' <summary>
''' Holds the <seecref="System.Runtime.InteropServices.ComTypes.IDataObject"/> interface to the <seecref="System.Windows.IDataObject"/> that this class is wrapping.
''' </summary>
Private comUnderlyingDataObject As System.Runtime.InteropServices.ComTypes.IDataObject
''' <summary>
''' Holds the internal ole <seecref="System.Windows.IDataObject"/> to the <seecref="System.Windows.IDataObject"/> that this class is wrapping.
''' </summary>
Private oleUnderlyingDataObject As System.Windows.IDataObject
''' <summary>
''' Holds the <seecref="MethodInfo"/> of the "GetDataFromHGLOBAL" method of the internal ole <seecref="System.Windows.IDataObject"/>.
''' </summary>
Private getDataFromHGLOBALMethod As MethodInfo
#End Region
#Region "Constructor(s)"
''' <summary>
''' Initializes a new instance of the <seecref="OutlookDataObject"/> class.
''' </summary>
''' <paramname="underlyingDataObject">The underlying data object to wrap.</param>
Public Sub New(ByVal underlyingDataObject As System.Windows.IDataObject)
'get the underlying dataobject and its ComType IDataObject interface to it
Me.underlyingDataObject = underlyingDataObject
comUnderlyingDataObject = CType(Me.underlyingDataObject, System.Runtime.InteropServices.ComTypes.IDataObject)
'get the internal ole dataobject and its GetDataFromHGLOBAL so it can be called later
Dim innerDataField As FieldInfo = Me.underlyingDataObject.[GetType]().GetField("_innerData", BindingFlags.NonPublic Or BindingFlags.Instance)
oleUnderlyingDataObject = CType(innerDataField.GetValue(Me.underlyingDataObject), System.Windows.IDataObject)
getDataFromHGLOBALMethod = oleUnderlyingDataObject.[GetType]().GetMethod("GetDataFromHGLOBAL", BindingFlags.NonPublic Or BindingFlags.Instance)
End Sub
#End Region
#Region "IDataObject Members"
''' <summary>
''' Retrieves the data associated with the specified class type format.
''' </summary>
''' <paramname="format">A <seecref="T:System.Type"></see> representing the format of the data to retrieve. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <returns>
''' The data associated with the specified format, or null.
''' </returns>
Public Function GetData(ByVal format As Type) As Object
Return GetData(format.FullName)
End Function
''' <summary>
''' Retrieves the data associated with the specified data format.
''' </summary>
''' <paramname="format">The format of the data to retrieve. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <returns>
''' The data associated with the specified format, or null.
''' </returns>
Public Function GetData(ByVal format As String) As Object
Return GetData(format, True)
End Function
''' <summary>
''' Retrieves the data associated with the specified data format, using a Boolean to determine whether to convert the data to the format.
''' </summary>
''' <paramname="format">The format of the data to retrieve. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="autoConvert">true to convert the data to the specified format; otherwise, false.</param>
''' <returns>
''' The data associated with the specified format, or null.
''' </returns>
Public Function GetData(ByVal format As String, ByVal autoConvert As Boolean) As Object
'handle the "FileGroupDescriptor" and "FileContents" format request in this class otherwise pass through to underlying IDataObject
Select Case format
Case "FileGroupDescriptor"
'override the default handling of FileGroupDescriptor which returns a
'MemoryStream and instead return a string array of file names
Dim fileGroupDescriptorAPointer As IntPtr = IntPtr.Zero
Try
'use the underlying IDataObject to get the FileGroupDescriptor as a MemoryStream
Dim fileGroupDescriptorStream As MemoryStream = CType(underlyingDataObject.GetData("FileGroupDescriptor", autoConvert), MemoryStream)
Dim fileGroupDescriptorBytes As Byte() = New Byte(fileGroupDescriptorStream.Length - 1) {}
fileGroupDescriptorStream.Read(fileGroupDescriptorBytes, 0, fileGroupDescriptorBytes.Length)
fileGroupDescriptorStream.Close()
'copy the file group descriptor into unmanaged memory
fileGroupDescriptorAPointer = Marshal.AllocHGlobal(fileGroupDescriptorBytes.Length)
Marshal.Copy(fileGroupDescriptorBytes, 0, fileGroupDescriptorAPointer, fileGroupDescriptorBytes.Length)
'''marshal the unmanaged memory to to FILEGROUPDESCRIPTORA struct
'FIX FROM - https://stackoverflow.com/questions/27173844/accessviolationexception-after-copying-a-file-from-inside-a-zip-archive-to-the-c
Dim ITEMCOUNT As Integer = Marshal.ReadInt32(fileGroupDescriptorAPointer)
'create a new array to store file names in of the number of items in the file group descriptor
Dim fileNames As String() = New String(ITEMCOUNT - 1) {}
'get the pointer to the first file descriptor
Dim fileDescriptorPointer As IntPtr = CType(CLng(fileGroupDescriptorAPointer) + Marshal.SizeOf(ITEMCOUNT), IntPtr)
'loop for the number of files acording to the file group descriptor
For fileDescriptorIndex As Integer = 0 To ITEMCOUNT - 1
'marshal the pointer top the file descriptor as a FILEDESCRIPTORA struct and get the file name
Dim fileDescriptor As NativeMethods.FILEDESCRIPTORA = CType(Marshal.PtrToStructure(fileDescriptorPointer, GetType(NativeMethods.FILEDESCRIPTORA)), NativeMethods.FILEDESCRIPTORA)
fileNames(fileDescriptorIndex) = fileDescriptor.cFileName
'move the file descriptor pointer to the next file descriptor
fileDescriptorPointer = CType(CLng(fileDescriptorPointer) + Marshal.SizeOf(fileDescriptor), IntPtr)
Next
'return the array of filenames
Return fileNames
Finally
'free unmanaged memory pointer
Marshal.FreeHGlobal(fileGroupDescriptorAPointer)
End Try
Case "FileGroupDescriptorW"
'override the default handling of FileGroupDescriptorW which returns a
'MemoryStream and instead return a string array of file names
Dim fileGroupDescriptorWPointer As IntPtr = IntPtr.Zero
Try
'use the underlying IDataObject to get the FileGroupDescriptorW as a MemoryStream
Dim fileGroupDescriptorStream As MemoryStream = CType(underlyingDataObject.GetData("FileGroupDescriptorW"), MemoryStream)
Dim fileGroupDescriptorBytes As Byte() = New Byte(fileGroupDescriptorStream.Length - 1) {}
fileGroupDescriptorStream.Read(fileGroupDescriptorBytes, 0, fileGroupDescriptorBytes.Length)
fileGroupDescriptorStream.Close()
'copy the file group descriptor into unmanaged memory
fileGroupDescriptorWPointer = Marshal.AllocHGlobal(fileGroupDescriptorBytes.Length)
Marshal.Copy(fileGroupDescriptorBytes, 0, fileGroupDescriptorWPointer, fileGroupDescriptorBytes.Length)
'marshal the unmanaged memory to to FILEGROUPDESCRIPTORW struct
'FIX FROM - https://stackoverflow.com/questions/27173844/accessviolationexception-after-copying-a-file-from-inside-a-zip-archive-to-the-c
Dim ITEMCOUNT As Integer = Marshal.ReadInt32(fileGroupDescriptorWPointer)
'create a new array to store file names in of the number of items in the file group descriptor
Dim fileNames As String() = New String(ITEMCOUNT - 1) {}
'get the pointer to the first file descriptor
Dim fileDescriptorPointer As IntPtr = CType(CLng(fileGroupDescriptorWPointer) + Marshal.SizeOf(ITEMCOUNT), IntPtr)
'loop for the number of files acording to the file group descriptor
For fileDescriptorIndex As Integer = 0 To ITEMCOUNT - 1
'marshal the pointer top the file descriptor as a FILEDESCRIPTORW struct and get the file name
Dim fileDescriptor As NativeMethods.FILEDESCRIPTORW = CType(Marshal.PtrToStructure(fileDescriptorPointer, GetType(NativeMethods.FILEDESCRIPTORW)), NativeMethods.FILEDESCRIPTORW)
fileNames(fileDescriptorIndex) = fileDescriptor.cFileName
'move the file descriptor pointer to the next file descriptor
fileDescriptorPointer = CType(CLng(fileDescriptorPointer) + Marshal.SizeOf(fileDescriptor), IntPtr)
Next
'return the array of filenames
Return fileNames
Finally
'free unmanaged memory pointer
Marshal.FreeHGlobal(fileGroupDescriptorWPointer)
End Try
Case "FileContents"
'override the default handling of FileContents which returns the
'contents of the first file as a memory stream and instead return
'a array of MemoryStreams containing the data to each file dropped
'
' FILECONTENTS requires a companion FILEGROUPDESCRIPTOR to be
' available so we bail out if we don't find one in the data object.
Dim fgdFormatName As String
If GetDataPresent("FileGroupDescriptorW") Then
fgdFormatName = "FileGroupDescriptorW"
ElseIf GetDataPresent("FileGroupDescriptor") Then
fgdFormatName = "FileGroupDescriptor"
Else
Return Nothing
End If
'get the array of filenames which lets us know how many file contents exist
Dim fileContentNames As String() = CType(GetData(fgdFormatName), String())
'create a MemoryStream array to store the file contents
Dim fileContents As MemoryStream() = New MemoryStream(fileContentNames.Length - 1) {}
'loop for the number of files acording to the file names
For fileIndex As Integer = 0 To fileContentNames.Length - 1
'get the data at the file index and store in array
fileContents(fileIndex) = GetData(format, fileIndex)
Next
'return array of MemoryStreams containing file contents
Return fileContents
End Select
'use underlying IDataObject to handle getting of data
Return underlyingDataObject.GetData(format, autoConvert)
End Function
''' <summary>
''' Retrieves the data associated with the specified data format at the specified index.
''' </summary>
''' <paramname="format">The format of the data to retrieve. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="index">The index of the data to retrieve.</param>
''' <returns>
''' A <seecref="MemoryStream"/> containing the raw data for the specified data format at the specified index.
''' </returns>
Public Function GetData(ByVal format As String, ByVal index As Integer) As MemoryStream
'create a FORMATETC struct to request the data with
Dim formatetc As FORMATETC = New FORMATETC()
formatetc.cfFormat = CShort(DataFormats.GetDataFormat(format).Id)
formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT
formatetc.lindex = index
formatetc.ptd = New IntPtr(0)
formatetc.tymed = TYMED.TYMED_ISTREAM Or TYMED.TYMED_ISTORAGE Or TYMED.TYMED_HGLOBAL
'create STGMEDIUM to output request results into
Dim medium As STGMEDIUM = New STGMEDIUM()
'using the Com IDataObject interface get the data using the defined FORMATETC
comUnderlyingDataObject.GetData(formatetc, medium)
'retrieve the data depending on the returned store type
Select Case medium.tymed
Case TYMED.TYMED_ISTORAGE
'to handle a IStorage it needs to be written into a second unmanaged
'memory mapped storage and then the data can be read from memory into
'a managed byte and returned as a MemoryStream
Dim iStorage As NativeMethods.IStorage = Nothing
Dim iStorage2 As NativeMethods.IStorage = Nothing
Dim iLockBytes As NativeMethods.ILockBytes = Nothing
Dim iLockBytesStat As System.Runtime.InteropServices.ComTypes.STATSTG
Try
'marshal the returned pointer to a IStorage object
iStorage = CType(Marshal.GetObjectForIUnknown(medium.unionmember), NativeMethods.IStorage)
Marshal.Release(medium.unionmember)
'create a ILockBytes (unmanaged byte array) and then create a IStorage using the byte array as a backing store
iLockBytes = NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, True)
iStorage2 = NativeMethods.StgCreateDocfileOnILockBytes(iLockBytes, &H1012, 0)
'copy the returned IStorage into the new IStorage
iStorage.CopyTo(0, Nothing, IntPtr.Zero, iStorage2)
iLockBytes.Flush()
iStorage2.Commit(0)
'get the STATSTG of the ILockBytes to determine how many bytes were written to it
iLockBytesStat = New System.Runtime.InteropServices.ComTypes.STATSTG()
iLockBytes.Stat(iLockBytesStat, 1)
Dim iLockBytesSize As Integer = iLockBytesStat.cbSize
'read the data from the ILockBytes (unmanaged byte array) into a managed byte array
Dim iLockBytesContent As Byte() = New Byte(iLockBytesSize - 1) {}
iLockBytes.ReadAt(0, iLockBytesContent, iLockBytesContent.Length, Nothing)
'wrapped the managed byte array into a memory stream and return it
Return New MemoryStream(iLockBytesContent)
Finally
'release all unmanaged objects
Marshal.ReleaseComObject(iStorage2)
Marshal.ReleaseComObject(iLockBytes)
Marshal.ReleaseComObject(iStorage)
End Try
Case TYMED.TYMED_ISTREAM
'to handle a IStream it needs to be read into a managed byte and
'returned as a MemoryStream
Dim iStream As IStream = Nothing
Dim iStreamStat As System.Runtime.InteropServices.ComTypes.STATSTG
Try
'marshal the returned pointer to a IStream object
iStream = CType(Marshal.GetObjectForIUnknown(medium.unionmember), IStream)
Marshal.Release(medium.unionmember)
'get the STATSTG of the IStream to determine how many bytes are in it
iStreamStat = New System.Runtime.InteropServices.ComTypes.STATSTG()
iStream.Stat(iStreamStat, 0)
Dim iStreamSize As Integer = iStreamStat.cbSize
'read the data from the IStream into a managed byte array
Dim iStreamContent As Byte() = New Byte(iStreamSize - 1) {}
iStream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero)
'wrapped the managed byte array into a memory stream and return it
Return New MemoryStream(iStreamContent)
Finally
'release all unmanaged objects
Marshal.ReleaseComObject(iStream)
End Try
Case TYMED.TYMED_HGLOBAL
'to handle a HGlobal the exisitng "GetDataFromHGLOBAL" method is invoked via
'reflection
Return CType(getDataFromHGLOBALMethod.Invoke(oleUnderlyingDataObject, New Object() {DataFormats.GetDataFormat(CShort(formatetc.cfFormat)).Name, medium.unionmember}), MemoryStream)
End Select
Return Nothing
End Function
''' <summary>
''' Determines whether data stored in this instance is associated with, or can be converted to, the specified format.
''' </summary>
''' <paramname="format">A <seecref="T:System.Type"></see> representing the format for which to check. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <returns>
''' true if data stored in this instance is associated with, or can be converted to, the specified format; otherwise, false.
''' </returns>
Public Function GetDataPresent(ByVal format As Type) As Boolean
Return underlyingDataObject.GetDataPresent(format)
End Function
''' <summary>
''' Determines whether data stored in this instance is associated with, or can be converted to, the specified format.
''' </summary>
''' <paramname="format">The format for which to check. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <returns>
''' true if data stored in this instance is associated with, or can be converted to, the specified format; otherwise false.
''' </returns>
Public Function GetDataPresent(ByVal format As String) As Boolean
Return underlyingDataObject.GetDataPresent(format)
End Function
''' <summary>
''' Determines whether data stored in this instance is associated with the specified format, using a Boolean value to determine whether to convert the data to the format.
''' </summary>
''' <paramname="format">The format for which to check. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="autoConvert">true to determine whether data stored in this instance can be converted to the specified format; false to check whether the data is in the specified format.</param>
''' <returns>
''' true if the data is in, or can be converted to, the specified format; otherwise, false.
''' </returns>
Public Function GetDataPresent(ByVal format As String, ByVal autoConvert As Boolean) As Boolean
Return underlyingDataObject.GetDataPresent(format, autoConvert)
End Function
''' <summary>
''' Returns a list of all formats that data stored in this instance is associated with or can be converted to.
''' </summary>
''' <returns>
''' An array of the names that represents a list of all formats that are supported by the data stored in this object.
''' </returns>
Public Function GetFormats() As String()
Return underlyingDataObject.GetFormats()
End Function
''' <summary>
''' Gets a list of all formats that data stored in this instance is associated with or can be converted to, using a Boolean value to determine whether to retrieve all formats that the data can be converted to or only native data formats.
''' </summary>
''' <paramname="autoConvert">true to retrieve all formats that data stored in this instance is associated with or can be converted to; false to retrieve only native data formats.</param>
''' <returns>
''' An array of the names that represents a list of all formats that are supported by the data stored in this object.
''' </returns>
Public Function GetFormats(ByVal autoConvert As Boolean) As String()
Return underlyingDataObject.GetFormats(autoConvert)
End Function
''' <summary>
''' Stores the specified data in this instance, using the class of the data for the format.
''' </summary>
''' <paramname="data">The data to store.</param>
Public Sub SetData(ByVal data As Object)
underlyingDataObject.SetData(data)
End Sub
''' <summary>
''' Stores the specified data and its associated class type in this instance.
''' </summary>
''' <paramname="format">A <seecref="T:System.Type"></see> representing the format associated with the data. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="data">The data to store.</param>
Public Sub SetData(ByVal format As Type, ByVal data As Object)
underlyingDataObject.SetData(format, data)
End Sub
''' <summary>
''' Stores the specified data and its associated format in this instance.
''' </summary>
''' <paramname="format">The format associated with the data. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="data">The data to store.</param>
Public Sub SetData(ByVal format As String, ByVal data As Object)
underlyingDataObject.SetData(format, data)
End Sub
''' <summary>
''' Stores the specified data and its associated format in this instance, using a Boolean value to specify whether the data can be converted to another format.
''' </summary>
''' <paramname="format">The format associated with the data. See <seecref="T:System.Windows.DataFormats"></see> for predefined formats.</param>
''' <paramname="autoConvert">true to allow the data to be converted to another format; otherwise, false.</param>
''' <paramname="data">The data to store.</param>
Public Sub SetData(ByVal format As String, ByVal data As Object, ByVal autoConvert As Boolean)
underlyingDataObject.SetData(format, data, autoConvert)
End Sub
Private Function IDataObject_GetData(format As String) As Object Implements Windows.IDataObject.GetData
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetData1(format As Type) As Object Implements Windows.IDataObject.GetData
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetData2(format As String, autoConvert As Boolean) As Object Implements Windows.IDataObject.GetData
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetDataPresent(format As String) As Boolean Implements Windows.IDataObject.GetDataPresent
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetDataPresent1(format As Type) As Boolean Implements Windows.IDataObject.GetDataPresent
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetDataPresent2(format As String, autoConvert As Boolean) As Boolean Implements Windows.IDataObject.GetDataPresent
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetFormats() As String() Implements Windows.IDataObject.GetFormats
Throw New NotImplementedException()
End Function
Private Function IDataObject_GetFormats1(autoConvert As Boolean) As String() Implements Windows.IDataObject.GetFormats
Throw New NotImplementedException()
End Function
Private Sub IDataObject_SetData(data As Object) Implements Windows.IDataObject.SetData
Throw New NotImplementedException()
End Sub
Private Sub IDataObject_SetData1(format As String, data As Object) Implements Windows.IDataObject.SetData
Throw New NotImplementedException()
End Sub
Private Sub IDataObject_SetData2(format As Type, data As Object) Implements Windows.IDataObject.SetData
Throw New NotImplementedException()
End Sub
Private Sub IDataObject_SetData3(format As String, data As Object, autoConvert As Boolean) Implements Windows.IDataObject.SetData
Throw New NotImplementedException()
End Sub
#End Region
End Class
End Namespace |
Partager