Beispiel #1
0
            /// <include file='doc\DataObject.uex' path='docs/doc[@for="DataObject.OleConverter.GetDataFromOleIStream"]/*' />
            /// <devdoc>
            ///     Uses IStream and retrieves the specified format from the bound IComDataObject.
            /// </devdoc>
            /// <internalonly/>
            private Object GetDataFromOleIStream(string format) {

                FORMATETC formatetc = new FORMATETC();
                STGMEDIUM medium = new STGMEDIUM();

                formatetc.cfFormat = unchecked((short)(ushort)(DataFormats.GetFormat(format).Id));
                formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                formatetc.lindex = -1;
                formatetc.tymed = TYMED.TYMED_ISTREAM;

                medium.tymed = TYMED.TYMED_ISTREAM;

                // Limit the # of exceptions we may throw below.
                if (NativeMethods.S_OK != QueryGetDataUnsafe(ref formatetc)){
                    return null;
                }

                try {
                    IntSecurity.UnmanagedCode.Assert();
                    try {
                         innerData.GetData(ref formatetc, out medium);
                    }
                    finally {
                        CodeAccessPermission.RevertAssert();
                    }
                }
                catch {
                    return null;
                }

                if (medium.unionmember != IntPtr.Zero) {
                    UnsafeNativeMethods.IStream pStream = (UnsafeNativeMethods.IStream)Marshal.GetObjectForIUnknown(medium.unionmember);
                    Marshal.Release(medium.unionmember);
                    NativeMethods.STATSTG sstg = new NativeMethods.STATSTG();
                    pStream.Stat(sstg, NativeMethods.STATFLAG_DEFAULT );
                    int size = (int)sstg.cbSize;

                    IntPtr hglobal = UnsafeNativeMethods.GlobalAlloc(NativeMethods.GMEM_MOVEABLE
                                                      | NativeMethods.GMEM_DDESHARE
                                                      | NativeMethods.GMEM_ZEROINIT,
                                                      size);
                    IntPtr ptr = UnsafeNativeMethods.GlobalLock(new HandleRef(innerData, hglobal));
                    pStream.Read(ptr, size);
                    UnsafeNativeMethods.GlobalUnlock(new HandleRef(innerData, hglobal));

                    return GetDataFromHGLOBLAL(format, hglobal);
                }

                return null;
            }
Beispiel #2
0
 internal State RefreshStorage(UnsafeNativeMethods.IPersistStorage iPersistStorage) {
     Debug.Assert(storage != null, "how can we not have a storage object?");
     Debug.Assert(iLockBytes != null, "how can we have a storage w/o ILockBytes?");
     if (storage == null || iLockBytes == null) return null;
     iPersistStorage.Save(storage, true);
     storage.Commit(0);
     iPersistStorage.HandsOffStorage();
     try {
         buffer = null;
         ms = null;
         NativeMethods.STATSTG stat = new NativeMethods.STATSTG();
         iLockBytes.Stat(stat, NativeMethods.Ole.STATFLAG_NONAME);
         length = (int) stat.cbSize;
         buffer = new byte[length];
         IntPtr hglobal = UnsafeNativeMethods.GetHGlobalFromILockBytes(iLockBytes);
         IntPtr pointer = UnsafeNativeMethods.GlobalLock(new HandleRef(null, hglobal));
         try {
             if (pointer != IntPtr.Zero) {
                 Marshal.Copy(pointer, buffer, 0, length);
             }
             else {
                 length = 0;
                 buffer = null;
             }
         }
         finally {
             UnsafeNativeMethods.GlobalUnlock(new HandleRef(null, hglobal));
         }
     }
     finally {
         iPersistStorage.SaveCompleted(storage);
     }
     return this;
 }