private static MemoryStream GetIStream(STGMEDIUM medium) { //marshal the returned pointer to a IStream object IStream iStream = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember); Marshal.Release(medium.unionmember); //get the STATSTG of the IStream to determine how many bytes are in it var iStreamStat = new System.Runtime.InteropServices.ComTypes.STATSTG(); iStream.Stat(out iStreamStat, 0); int iStreamSize = (int)iStreamStat.cbSize; //prevent memoryexception if (iStreamSize > 100000000) { return(null); } //read the data from the IStream into a managed byte array byte[] iStreamContent = new byte[iStreamSize]; iStream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero); //wrapped the managed byte array into a memory stream return(new MemoryStream(iStreamContent)); }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG statStg, int grfFlags) { statStg = new System.Runtime.InteropServices.ComTypes.STATSTG { cbSize = m_stream.Length }; }
public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG { type = STGTY_STREAM, cbSize = BaseStream.Length, grfMode = 0 }; if (BaseStream.CanRead && BaseStream.CanWrite) { pstatstg.grfMode |= STGM_READWRITE; } else if (BaseStream.CanRead) { pstatstg.grfMode |= STGM_READ; } else if (BaseStream.CanWrite) { pstatstg.grfMode |= STGM_WRITE; } else { throw new IOException("Stream is closed"); } }
public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG() { cbSize = _stream.Length }; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { const int STGTY_STREAM = 2; pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.type = STGTY_STREAM; pstatstg.cbSize = this._stream.Length; pstatstg.grfMode = 0; if (this._stream.CanRead && this._stream.CanWrite) { const int STGM_READWRITE = 0x00000002; pstatstg.grfMode |= STGM_READWRITE; return; } if (this._stream.CanRead) { const int STGM_READ = 0x00000000; pstatstg.grfMode |= STGM_READ; return; } if (this._stream.CanWrite) { const int STGM_WRITE = 0x00000001; pstatstg.grfMode |= STGM_WRITE; return; } throw new IOException(); }
//No problem cant be made worse with recursion! private List <string> OpenSubStorage(ole32.IStorage Is, string pwcsName, List <string> data) { ole32.IStorage ppstg; Is.OpenStorage(pwcsName, null, (uint)(ole32.STGM.READ | ole32.STGM.SHARE_EXCLUSIVE), IntPtr.Zero, 0, out ppstg); //set up to fetch one item on each call to next ole32.IEnumSTATSTG SSenum; ppstg.EnumElements(0, IntPtr.Zero, 0, out SSenum); var SSstruct = new System.Runtime.InteropServices.ComTypes.STATSTG[1]; //do the loop until not more items uint NumReturned; do { SSenum.Next(1, SSstruct, out NumReturned); if (NumReturned != 0) { if (SSstruct[0].type == 1) { OpenSubStorage(ppstg, SSstruct[0].pwcsName, data); } else if (SSstruct[0].type == 2 && SSstruct[0].pwcsName == "3") { data.Add(readStream(ref ppstg, SSstruct[0].pwcsName)); } } } while (NumReturned > 0); return(data); }
public IEnumerable <string> GetNotes(string path) { var data = new List <string>(); var isOLE = ole32.StgIsStorageFile(path); if (isOLE == 0) { //open the storage ole32.IStorage Is; int result = ole32.StgOpenStorage(path, null, ole32.STGM.READ | ole32.STGM.SHARE_DENY_WRITE, IntPtr.Zero, 0, out Is); //set up to fetch one item on each call to next ole32.IEnumSTATSTG SSenum; Is.EnumElements(0, IntPtr.Zero, 0, out SSenum); var SSstruct = new System.Runtime.InteropServices.ComTypes.STATSTG[1]; //do the loop until not more items uint NumReturned; do { SSenum.Next(1, SSstruct, out NumReturned); if (NumReturned != 0) { if (SSstruct[0].type == 1) { OpenSubStorage(Is, SSstruct[0].pwcsName, data); } } } while (NumReturned > 0); } return(data); }
public void Stat(out ComTypes.STATSTG pstatstg, STATFLAG grfStatFlag) { pstatstg = new ComTypes.STATSTG(); pstatstg.type = (int)STGTY.Stream; pstatstg.cbSize = this.stream.Length; pstatstg.grfLocksSupported = (int)LOCKTYPE.Exclusive; }
public void Stat(out com.STATSTG pstatstg, int grfStatFlag) { pstatstg = new com.STATSTG() { cbSize = this.source.Length }; }
public void Stat( out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.cbSize = base.Length; }
/// <summary> /// Returns details about the stream, including its length, type and name. /// </summary> /// <param name="pstatstg"></param> /// <param name="grfStatFlag"></param> public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.cbSize = mInner.Length; pstatstg.type = 2; // stream type pstatstg.pwcsName = (mInner is FileStream) ? ((FileStream)mInner).Name : String.Empty; }
/// <summary> /// Obtain stream stats. /// </summary> /// <remarks> /// STATSG has to be qualified because it is defined both in System.Runtime.InteropServices and /// System.Runtime.InteropServices.ComTypes. /// The STATSTG structure is shared by streams, storages and byte arrays. Members irrelevant to streams /// or not available from System.IO.Stream are not returned, which leaves only cbSize and grfMode as /// meaningful and available pieces of information. /// grfStatFlag is used to indicate whether the stream name should be returned and is ignored because /// this information is unavailable. /// </remarks> void IStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG streamStats, int grfStatFlag) { streamStats = new System.Runtime.InteropServices.ComTypes.STATSTG(); streamStats.type = NativeMethods.STGTY_STREAM; streamStats.cbSize = _ioStream.Length; // Return access information in grfMode. streamStats.grfMode = 0; // default value for each flag will be false if (_ioStream.CanRead && _ioStream.CanWrite) { streamStats.grfMode |= NativeMethods.STGM_READWRITE; } else if (_ioStream.CanRead) { streamStats.grfMode |= NativeMethods.STGM_READ; } else if (_ioStream.CanWrite) { streamStats.grfMode |= NativeMethods.STGM_WRITE; } else { // A stream that is neither readable nor writable is a closed stream. // Note the use of an exception that is known to the interop marshaller // (unlike ObjectDisposedException). throw new IOException(SR.Get(SRID.StreamObjectDisposed)); } }
public void Stat(out com.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG() { cbSize = this.source.Length }; }
internal static SolidEdgeDocument FromIStorage(IStorage storage) { if (storage == null) { throw new ArgumentNullException("storage"); } SolidEdgeDocument document = null; System.Runtime.InteropServices.ComTypes.STATSTG statstg = statstg = storage.GetStatistics(); // For now there is a 1 - 1 mapping. Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetSolidEdgeDocumentTypes(statstg.clsid); if (types.Length == 0) { Marshal.ThrowExceptionForHR(HRESULT.CO_E_CANTDETERMINECLASS); } else if (types.Length == 1) { document = InvokeConstructor(types[0], storage, statstg); } else // Future possibility for multiple types. i.e Guid.Empty { Marshal.ThrowExceptionForHR(HRESULT.CO_E_CANTDETERMINECLASS); } return(document); }
HResult IStream.Stat(out comtypes.STATSTG pstatstg, int grfStatFlag) { const int STGM_READ = 0x00000000; const int STGM_WRITE = 0x00000001; const int STGM_READWRITE = 0x00000002; var tmp = new comtypes.STATSTG(); tmp.type = 2; //STGTY_STREAM tmp.cbSize = Length; if (CanWrite && CanRead) { tmp.grfMode = STGM_READWRITE; } else if (CanRead) { tmp.grfMode = STGM_READ; } else if (CanWrite) { tmp.grfMode = STGM_WRITE; } else { throw new ObjectDisposedException("Stream"); } pstatstg = tmp; return(HResult.S_OK); }
void IStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { System.Runtime.InteropServices.ComTypes.STATSTG sTATSTG = new System.Runtime.InteropServices.ComTypes.STATSTG { type = 2, cbSize = this.Length, grfMode = 0 }; if (this.CanWrite && this.CanRead) { sTATSTG.grfMode |= 2; } else if (this.CanRead) { sTATSTG.grfMode |= 0; } else { if (!this.CanWrite) { throw new ObjectDisposedException("Stream"); } sTATSTG.grfMode |= 1; } pstatstg = sTATSTG; }
public IEnumerable <STATSTGWrapper> EnumElements(bool read_stream_data) { List <STATSTGWrapper> ret = new List <STATSTGWrapper>(); _stg.EnumElements(0, IntPtr.Zero, 0, out IEnumSTATSTG enum_stg); try { ComTypes.STATSTG[] stat = new ComTypes.STATSTG[1]; uint fetched; while (enum_stg.Next(1, stat, out fetched) == 0) { STGTY type = (STGTY)stat[0].type; byte[] bytes = new byte[0]; if (read_stream_data && type == STGTY.Stream) { bytes = ReadStream(stat[0].pwcsName); } ret.Add(new STATSTGWrapper(stat[0].pwcsName, stat[0], bytes)); } } finally { Marshal.ReleaseComObject(enum_stg); } return(ret); }
void IStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { const int STGM_READ = 0x00000000; const int STGM_WRITE = 0x00000001; const int STGM_READWRITE = 0x00000002; var tmp = new System.Runtime.InteropServices.ComTypes.STATSTG { type = 2, cbSize = Length, grfMode = 0 }; if (CanWrite && CanRead) { tmp.grfMode |= STGM_READWRITE; } else if (CanRead) { tmp.grfMode |= STGM_READ; } else if (CanWrite) { tmp.grfMode |= STGM_WRITE; } else { throw new ObjectDisposedException("Stream"); } pstatstg = tmp; }
// Returns details about the stream, including its length, type and name. public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG(); pstatstg.cbSize = m_inner.Length; pstatstg.type = 2; // stream type pstatstg.pwcsName = (m_inner is FileStream) ? ((FileStream)m_inner).Name : String.Empty; }
/// <summary> /// Obtain stream stats. /// </summary> /// <remarks> /// STATSG has to be qualified because it is defined both in System.Runtime.InteropServices and /// System.Runtime.InteropServices.ComTypes. /// The STATSTG structure is shared by streams, storages and byte arrays. Members irrelevant to streams /// or not available from System.IO.Stream are not returned, which leaves only cbSize and grfMode as /// meaningful and available pieces of information. /// grfStatFlag is used to indicate whether the stream name should be returned and is ignored because /// this information is unavailable. /// </remarks> void IStream.Stat(out STATSTG streamStats, int grfStatFlag) { streamStats = new STATSTG { type = STGTY_STREAM, cbSize = stream.Length, // Return access information in grfMode. grfMode = 0 // default value for each flag will be false }; if (stream.CanRead && stream.CanWrite) { streamStats.grfMode |= STGM_READWRITE; } else if (stream.CanRead) { streamStats.grfMode |= STGM_READ; } else if (stream.CanWrite) { streamStats.grfMode |= STGM_WRITE; } else { // A stream that is neither readable nor writable is a closed stream. // Note the use of an exception that is known to the interop marshaller // (unlike ObjectDisposedException). throw new IOException("Current stream object was closed and disposed. Cannot access a closed stream."); } }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG stat, int statFlag) { stat = new System.Runtime.InteropServices.ComTypes.STATSTG { cbSize = _base.Length }; }
void IUnsafeComStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG() { cbSize = _length }; }
public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG { cbSize = _stream.Length }; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG streamStats, int grfStatFlag) { streamStats = new System.Runtime.InteropServices.ComTypes.STATSTG(); streamStats.type = 2; streamStats.cbSize = stream.Length; // Return access information in grfMode. streamStats.grfMode = 0; // default value for each flag will be false if (stream.CanRead && stream.CanWrite) { streamStats.grfMode |= 22; } else if (stream.CanRead) { streamStats.grfMode |= 0; } else if (stream.CanWrite) { streamStats.grfMode |= 1; } else { throw new IOException(); } }
void IUnsafeComStream.Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG() { cbSize = _length }; }
private static void ReadIStorageIntoStream(IntPtr handle, Stream stream) { //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 NativeMethods.ILockBytes iLockBytes = null; NativeMethods.IStorage iStorageNew = null; IntPtr ptrRead = IntPtr.Zero; try { //Marshal pointer to an IStorage object NativeMethods.IStorage iStorage = (NativeMethods.IStorage)Marshal.GetObjectForIUnknown(handle); //Create an ILockBytes object on a HGlobal, then create a IStorage object on top of the ILockBytes object iLockBytes = NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, true); iStorageNew = NativeMethods.StgCreateDocfileOnILockBytes(iLockBytes, 0x00001012, 0); //Copy the IStorage object into the new IStorage object iStorage.CopyTo(0, null, IntPtr.Zero, iStorageNew); iLockBytes.Flush(); iStorageNew.Commit(0); //Get length of ILockBytes byte array System.Runtime.InteropServices.ComTypes.STATSTG stat = new System.Runtime.InteropServices.ComTypes.STATSTG(); iLockBytes.Stat(out stat, 1); long length = stat.cbSize; //Read bytes into stream ptrRead = Marshal.AllocCoTaskMem(sizeof(int)); byte[] buffer = new byte[4096]; //4 KB buffer int offset = 0; int bytesRead; while (true) { iLockBytes.ReadAt(offset, buffer, buffer.Length, ptrRead); bytesRead = Marshal.ReadInt32(ptrRead); if (bytesRead == 0) { break; } stream.Write(buffer, 0, bytesRead); offset += bytesRead; } } finally { //Release all unmanaged objects Marshal.FreeCoTaskMem(ptrRead); if (iStorageNew != null) { Marshal.ReleaseComObject(iStorageNew); } if (iLockBytes != null) { Marshal.ReleaseComObject(iLockBytes); } } }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG { cbSize = this._stream.Length, grfLocksSupported = 0 }; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { //IStreamWrapper wants the length var tempSTATSTG = new System.Runtime.InteropServices.ComTypes.STATSTG(); tempSTATSTG.cbSize = stream.Length; pstatstg = tempSTATSTG; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = default(System.Runtime.InteropServices.ComTypes.STATSTG); pstatstg.type = 2; pstatstg.cbSize = this.managedStream.Length; pstatstg.grfMode = 2; pstatstg.grfLocksSupported = 2; }
int IStream.Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG(); pstatstg.type = 2; pstatstg.cbSize = stream.Length; pstatstg.grfLocksSupported = 2; return(S_OK); }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); this._Validate(); pstatstg.type = 2; pstatstg.cbSize = this._source.Length; pstatstg.grfMode = 2; pstatstg.grfLocksSupported = 2; }
public ReadOnlyILockBytes(Stream stream) { _stream = stream; _statstg = new STATSTG() { grfLocksSupported = 0, cbSize = _stream.Length, type = (int)STGTY.LOCKBYTES }; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.type = 2; pstatstg.cbSize = this.stream.Length; pstatstg.grfMode = 0; if (this.stream.CanRead && this.stream.CanWrite) { pstatstg.grfMode |= 2; } else if (!this.stream.CanRead) { if (this.stream.CanWrite) pstatstg.grfMode |= 1; } }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG streamStats, int grfStatFlag) { streamStats = new System.Runtime.InteropServices.ComTypes.STATSTG(); streamStats.type = 2; streamStats.cbSize = stream.Length; // Return access information in grfMode. streamStats.grfMode = 0; // default value for each flag will be false if (stream.CanRead && stream.CanWrite) streamStats.grfMode |= 22; else if (stream.CanRead) streamStats.grfMode |= 0; else if (stream.CanWrite) streamStats.grfMode |= 1; else throw new IOException(); }
// Get this clipboard format out of this data object public static byte[] GetData(IDataObject oData, string cfFormat) { // First, we try using the built-in functionality. Unfortunately, in the TYMED_ISTREAM case // they forget to seek the stream to zero, and so aren't successful. We'll take care of that // in a moment. // System.IO.Stream stm = (System.IO.Stream)oData.GetData(cfFormat, false); if (null != stm) { stm.Seek(0, System.IO.SeekOrigin.Begin); int cb = (int)stm.Length; byte[] rgb = new byte[cb]; int cbRead = stm.Read(rgb, 0, cb); if (cbRead == cb) { // The bug is that the data returned is entirely zero. Let's check. // for (int ib=0; ib < cb; ib++) { if (rgb[ib] != 0) return rgb; } } } // // Ok, try to see if we can get it on a stream ourselves // COMTypes.IDataObject ido = (COMTypes.IDataObject)oData; COMTypes.FORMATETC formatEtc = new COMTypes.FORMATETC(); COMTypes.STGMEDIUM medium = new COMTypes.STGMEDIUM(); formatEtc.cfFormat = (short)DataFormats.GetFormat(cfFormat).Id; formatEtc.dwAspect = COMTypes.DVASPECT.DVASPECT_CONTENT; formatEtc.lindex = -1; // REVIEW: is 0 better? formatEtc.tymed = COMTypes.TYMED.TYMED_ISTREAM; // ido.GetData(ref formatEtc, out medium); // if (medium.unionmember != IntPtr.Zero) { // Get at the IStream and release the ref in the medium COMTypes.IStream istm = (COMTypes.IStream)Marshal.GetObjectForIUnknown(medium.unionmember); Marshal.Release(medium.unionmember); // How big is the stream? COMTypes.STATSTG statstg = new COMTypes.STATSTG(); istm.Stat(out statstg, 1); int cb = (int)statstg.cbSize; byte[] rgb = new byte[cb]; // Seek the stream to the beginning and read the data IntPtr pcbRead = Marshal.AllocCoTaskMem(sizeof(int)); istm.Seek(0, 0, IntPtr.Zero); istm.Read(rgb, cb, pcbRead); int cbRead = Marshal.ReadInt32(pcbRead); Marshal.FreeCoTaskMem(pcbRead); if (cb==cbRead) return rgb; } // // Can't get the data // return null; }
private static int Stat(IntPtr @this, out STATSTG pstatstg, int grfStatFlag) { #if MAP_EX_TO_HR try { #endif GetObject(@this).managedInterface.Stat(out pstatstg, grfStatFlag); return S_OK; #if MAP_EX_TO_HR } catch (Exception e) { pstatstg = new STATSTG(); return GetHRForException(e); } #endif }
public void Stat(out STATSTG pstatstg, int grfStatFlag) { ThrowExceptionForHR(managedVtable.Stat(comInterface, out pstatstg, grfStatFlag)); }
public string GetSource(IWebBrowser2 thisBrowser) { if ((thisBrowser == null) || (thisBrowser.Document == null)) return string.Empty; //Declare vars int hr = Hresults.S_OK; IStream pStream = null; IPersistStreamInit pPersistStreamInit = null; // Query for IPersistStreamInit. pPersistStreamInit = thisBrowser.Document as IPersistStreamInit; if (pPersistStreamInit == null) return string.Empty; //Create stream, delete on release hr = WinApis.CreateStreamOnHGlobal(m_NullPointer, true, out pStream); if ((pStream == null) || (hr != Hresults.S_OK)) return string.Empty; //Save hr = pPersistStreamInit.Save(pStream, false); if (hr != Hresults.S_OK) return string.Empty; //Now read from stream.... //First get the size long ulSizeRequired = (long)0; //LARGE_INTEGER long liBeggining = (long)0; System.Runtime.InteropServices.ComTypes.STATSTG statstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pStream.Seek(liBeggining, (int)tagSTREAM_SEEK.STREAM_SEEK_SET, m_NullPointer); pStream.Stat(out statstg, (int)tagSTATFLAG.STATFLAG_NONAME); //Size ulSizeRequired = statstg.cbSize; if (ulSizeRequired == (long)0) return string.Empty; //Allocate buffer + read byte[] pSource = new byte[ulSizeRequired]; pStream.Read(pSource, (int)ulSizeRequired, m_NullPointer); //Added by schlaup to handle UTF8 and UNICODE pages //Convert //ASCIIEncoding asce = new ASCIIEncoding(); //return asce.GetString(pSource); Encoding enc = null; if (pSource.Length > 8) { // Check byte order mark if ((pSource[0] == 0xFF) && (pSource[1] == 0xFE)) // UTF16LE enc = Encoding.Unicode; if ((pSource[0] == 0xFE) && (pSource[1] == 0xFF)) // UTF16BE enc = Encoding.BigEndianUnicode; if ((pSource[0] == 0xEF) && (pSource[1] == 0xBB) && (pSource[2] == 0xBF)) //UTF8 enc = Encoding.UTF8; if (enc == null) { // Check for alternating zero bytes which might indicate Unicode if ((pSource[1] == 0) && (pSource[3] == 0) && (pSource[5] == 0) && (pSource[7] == 0)) enc = Encoding.Unicode; } } if (enc == null) enc = Encoding.Default; int bomLength = enc.GetPreamble().Length; return enc.GetString(pSource, bomLength, pSource.Length - bomLength); }
internal static unsafe int Stat(System.IntPtr pComThis, out ComTypes.STATSTG pstatstg, int grfStatFlag) { __com_IStream* pIStream = (__com_IStream*)pComThis; pstatstg = new ComTypes.STATSTG(); pstatstg.cbSize = pIStream->m_cbSize; return Interop.COM.S_OK; }
/// <summary> /// Processes sub streams and storages on the specified storage. /// </summary> /// <param name="storage">The storage to get sub streams and storages for.</param> protected virtual void LoadStorage(NativeMethods.IStorage storage) { this.storage = storage; //ensures memory is released ReferenceManager.AddItem(this.storage); NativeMethods.IEnumSTATSTG storageElementEnum = null; try { //enum all elements of the storage storage.EnumElements(0, IntPtr.Zero, 0, out storageElementEnum); //iterate elements while (true) { //get 1 element out of the com enumerator uint elementStatCount; ComTypes.STATSTG[] elementStats = new ComTypes.STATSTG[1]; storageElementEnum.Next(1, elementStats, out elementStatCount); //break loop if element not retrieved if (elementStatCount != 1) { break; } ComTypes.STATSTG elementStat = elementStats[0]; switch (elementStat.type) { case 1: //element is a storage. add its statistics object to the storage dictionary subStorageStatistics.Add(elementStat.pwcsName, elementStat); break; case 2: //element is a stream. add its statistics object to the stream dictionary streamStatistics.Add(elementStat.pwcsName, elementStat); break; } } } finally { //free memory if (storageElementEnum != null) { Marshal.ReleaseComObject(storageElementEnum); } } }
/// <summary> /// Returns details about the stream, including its length, type and name. /// </summary> /// <param name="pstatstg"></param> /// <param name="grfStatFlag"></param> public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG { cbSize = _inner.Length, type = 2, pwcsName = (_inner is FileStream) ? ((FileStream) _inner).Name : String.Empty }; // stream type }
internal byte[] Save() { ILockBytes lb; var iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb); IStorage storage = null; byte[] ret = null; //Create the document in-memory if (StgCreateDocfileOnILockBytes(lb, STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED, 0, out storage)==0) { foreach(var store in this.Storage.SubStorage) { CreateStore(store.Key, store.Value, storage); } CreateStreams(this.Storage, storage); lb.Flush(); //Now copy the unmanaged stream to a byte array --> memory stream var statstg = new comTypes.STATSTG(); lb.Stat(out statstg, 0); int size = (int)statstg.cbSize; IntPtr buffer = Marshal.AllocHGlobal(size); UIntPtr readSize; ret=new byte[size]; lb.ReadAt(0, buffer, size, out readSize); Marshal.Copy(buffer, ret, 0, size); Marshal.FreeHGlobal(buffer); } Marshal.ReleaseComObject(storage); Marshal.ReleaseComObject(lb); return ret; }
/// <inheritdoc/> public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { var s = new System.Runtime.InteropServices.ComTypes.STATSTG(); // s.atime = ???; s.cbSize = stream.Length; s.clsid = Guid.Empty; // s.ctime = ???; s.grfLocksSupported = 0; s.grfMode = 2; s.grfStateBits = 0; // s.mtime = ???; if ((grfStatFlag & (int)STATFLAG.NONAME) == 0) s.pwcsName = name; s.reserved = 0; s.type = (int)STGTY.STREAM; pstatstg = s; }
public void Stat(out COMTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new COMTypes.STATSTG(); pstatstg.cbSize = this.stm.Length; }
/// <summary> /// Retrieves the data associated with the specified data format at the specified index. /// </summary> /// <param name="format">The format of the data to retrieve. See <see cref="T:System.Windows.Forms.DataFormats"></see> for predefined formats.</param> /// <param name="index">The index of the data to retrieve.</param> /// <returns> /// A <see cref="MemoryStream"/> containing the raw data for the specified data format at the specified index. /// </returns> public MemoryStream GetData(string format, int index) { //create a FORMATETC struct to request the data with FORMATETC formatetc = new FORMATETC(); formatetc.cfFormat = (short)DataFormats.GetFormat(format).Id; formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT; formatetc.lindex = index; formatetc.ptd = new IntPtr(0); formatetc.tymed = TYMED.TYMED_ISTREAM | TYMED.TYMED_ISTORAGE | TYMED.TYMED_HGLOBAL; //create STGMEDIUM to output request results into STGMEDIUM medium = new STGMEDIUM(); //using the Com IDataObject interface get the data using the defined FORMATETC this.comUnderlyingDataObject.GetData(ref formatetc, out medium); //retrieve the data depending on the returned store type switch(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 NativeMethods.IStorage iStorage = null; NativeMethods.IStorage iStorage2 = null; NativeMethods.ILockBytes iLockBytes = null; System.Runtime.InteropServices.ComTypes.STATSTG iLockBytesStat; try { //marshal the returned pointer to a IStorage object iStorage = (NativeMethods.IStorage)Marshal.GetObjectForIUnknown(medium.unionmember); 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, 0x00001012, 0); //copy the returned IStorage into the new IStorage iStorage.CopyTo(0, null, 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(out iLockBytesStat, 1); int iLockBytesSize = (int)iLockBytesStat.cbSize; //read the data from the ILockBytes (unmanaged byte array) into a managed byte array byte[] iLockBytesContent = new byte[iLockBytesSize]; iLockBytes.ReadAt(0, iLockBytesContent, iLockBytesContent.Length, null); //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); } case TYMED.TYMED_ISTREAM: //to handle a IStream it needs to be read into a managed byte and //returned as a MemoryStream IStream iStream = null; System.Runtime.InteropServices.ComTypes.STATSTG iStreamStat; try { //marshal the returned pointer to a IStream object iStream = (IStream)Marshal.GetObjectForIUnknown(medium.unionmember); 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(out iStreamStat, 0); int iStreamSize = (int)iStreamStat.cbSize; //read the data from the IStream into a managed byte array byte[] iStreamContent = new byte[iStreamSize]; 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); } case TYMED.TYMED_HGLOBAL: //to handle a HGlobal the exisitng "GetDataFromHGLOBLAL" method is invoked via //reflection return (MemoryStream)this.getDataFromHGLOBLALMethod.Invoke(this.oleUnderlyingDataObject, new object[] { DataFormats.GetFormat((short)formatetc.cfFormat).Name, medium.unionmember }); } return null; }
void IStream.Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG { type = 2, cbSize = _stream.Length, }; if (_stream.CanRead && _stream.CanWrite) pstatstg.grfMode = Win32.STGM_READWRITE; else if (_stream.CanWrite) pstatstg.grfMode = Win32.STGM_WRITE; else if (_stream.CanRead) pstatstg.grfMode = Win32.STGM_READ; else throw new IOException(); }
public void Stat(out ComTypes.STATSTG pstatstg, STATFLAG grfStatFlag) { pstatstg = new ComTypes.STATSTG(); pstatstg.type = (int)STGTY.Stream; pstatstg.cbSize = this.stream.Length; pstatstg.grfLocksSupported = (int)LOCKTYPE.Exclusive; }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.type = 2; // STGTY_STREAM pstatstg.cbSize = this._stream.Length; pstatstg.grfMode = 0; if (this._stream.CanRead && this._stream.CanWrite) { pstatstg.grfMode |= 2; } else if (this._stream.CanRead && !_stream.CanWrite) { pstatstg.grfMode |= 1; } else { throw new IOException(); } }
public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.cbSize = m_stream.Length; if ((grfStatFlag & 0x0001/* STATFLAG_NONAME */) != 0) return; pstatstg.pwcsName = m_stream.ToString(); }
HResult IStream.Stat(out comtypes.STATSTG pstatstg, int grfStatFlag) { const int STGM_READ = 0x00000000; const int STGM_WRITE = 0x00000001; const int STGM_READWRITE = 0x00000002; var tmp = new comtypes.STATSTG(); tmp.type = 2; //STGTY_STREAM tmp.cbSize = Length; if (CanWrite && CanRead) tmp.grfMode = STGM_READWRITE; else if (CanRead) tmp.grfMode = STGM_READ; else if (CanWrite) tmp.grfMode = STGM_WRITE; else throw new ObjectDisposedException("Stream"); pstatstg = tmp; return HResult.S_OK; }
/// <summary> /// Encrypts a package /// </summary> /// <param name="package">The package as a byte array</param> /// <param name="encryption">The encryption info from the workbook</param> /// <returns></returns> internal MemoryStream EncryptPackage(byte[] package, ExcelEncryption encryption) { byte[] encryptionKey; //Create the Encryption Info. This also returns the Encryptionkey var encryptionInfo = CreateEncryptionInfo(encryption.Password, encryption.Algorithm == EncryptionAlgorithm.AES128 ? AlgorithmID.AES128 : encryption.Algorithm == EncryptionAlgorithm.AES192 ? AlgorithmID.AES192 : AlgorithmID.AES256, out encryptionKey); ILockBytes lb; var iret = CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lb); IStorage storage = null; MemoryStream ret = null; //Create the document in-memory if (StgCreateDocfileOnILockBytes(lb, STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE | STGM.TRANSACTED, 0, out storage)==0) { //First create the dataspace storage CreateDataSpaces(storage); //Create the Encryption info Stream comTypes.IStream stream; storage.CreateStream("EncryptionInfo", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); byte[] ei=encryptionInfo.WriteBinary(); stream.Write(ei, ei.Length, IntPtr.Zero); stream = null; //Encrypt the package byte[] encryptedPackage=EncryptData(encryptionKey, package, false); storage.CreateStream("EncryptedPackage", (uint)(STGM.CREATE | STGM.WRITE | STGM.DIRECT | STGM.SHARE_EXCLUSIVE), (uint)0, (uint)0, out stream); //Write Size here MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write((ulong)package.LongLength); bw.Flush(); byte[] length = ms.ToArray(); //Write Encrypted data length first as an unsigned long stream.Write(length, length.Length, IntPtr.Zero); //And now the Encrypted data stream.Write(encryptedPackage, encryptedPackage.Length, IntPtr.Zero); stream = null; storage.Commit(0); lb.Flush(); //Now copy the unmanaged stream to a byte array --> memory stream var statstg = new comTypes.STATSTG(); lb.Stat(out statstg, 0); int size = (int)statstg.cbSize; IntPtr buffer = Marshal.AllocHGlobal(size); UIntPtr readSize; byte[] pack=new byte[size]; lb.ReadAt(0, buffer, size, out readSize); Marshal.Copy(buffer, pack, 0, size); Marshal.FreeHGlobal(buffer); ret = new MemoryStream(); ret.Write(pack, 0, size); } Marshal.ReleaseComObject(storage); Marshal.ReleaseComObject(lb); return ret; }
/// <summary> /// Returns details about the stream, including its length, type and name. /// </summary> /// <param name="pstatstg"></param> /// <param name="grfStatFlag"></param> public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.cbSize = mInner.Length; pstatstg.type = 2; // stream type pstatstg.pwcsName = (mInner is FileStream) ? ((FileStream)mInner).Name : String.Empty; }
void IStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG { type = 2 }; }
/// <summary> /// Processes sub streams and storages on the specified storage. /// </summary> /// <param name="storage"> The storage to get sub streams and storages for. </param> protected virtual void LoadStorage(NativeMethods.IStorage storage) { if (storage == null) throw new ArgumentNullException("storage", "Storage can not be null"); _storage = storage; // Ensures memory is released ReferenceManager.AddItem(storage); NativeMethods.IEnumSTATSTG storageElementEnum = null; try { // Enum all elements of the storage storage.EnumElements(0, IntPtr.Zero, 0, out storageElementEnum); // Iterate elements while (true) { // Get 1 element out of the COM enumerator uint elementStatCount; var elementStats = new STATSTG[1]; storageElementEnum.Next(1, elementStats, out elementStatCount); // Break loop if element not retrieved if (elementStatCount != 1) break; var elementStat = elementStats[0]; switch (elementStat.type) { case 1: // Element is a storage, add its statistics object to the storage dictionary _subStorageStatistics.Add(elementStat.pwcsName, elementStat); break; case 2: // Element is a stream, add its statistics object to the stream dictionary _streamStatistics.Add(elementStat.pwcsName, elementStat); break; } } } finally { // Free memory if (storageElementEnum != null) Marshal.ReleaseComObject(storageElementEnum); } }
/// <summary> /// Obtain stream stats. /// </summary> /// <remarks> /// STATSG has to be qualified because it is defined both in System.Runtime.InteropServices and /// System.Runtime.InteropServices.ComTypes. /// The STATSTG structure is shared by streams, storages and byte arrays. Members irrelevant to streams /// or not available from System.IO.Stream are not returned, which leaves only cbSize and grfMode as /// meaningful and available pieces of information. /// grfStatFlag is used to indicate whether the stream name should be returned and is ignored because /// this information is unavailable. /// </remarks> void IStream.Stat(out System.Runtime.InteropServices.ComTypes.STATSTG streamStats, int grfStatFlag) { streamStats = new System.Runtime.InteropServices.ComTypes.STATSTG(); streamStats.type = NativeMethods.STGTY_STREAM; streamStats.cbSize = _ioStream.Length; // Return access information in grfMode. streamStats.grfMode = 0; // default value for each flag will be false if (_ioStream.CanRead && _ioStream.CanWrite) { streamStats.grfMode |= NativeMethods.STGM_READWRITE; } else if (_ioStream.CanRead) { streamStats.grfMode |= NativeMethods.STGM_READ; } else if (_ioStream.CanWrite) { streamStats.grfMode |= NativeMethods.STGM_WRITE; } else { // A stream that is neither readable nor writable is a closed stream. // Note the use of an exception that is known to the interop marshaller // (unlike ObjectDisposedException). throw new IOException(SR.Get(SRID.StreamObjectDisposed)); } }
/// <summary> /// Get description of the stream. /// </summary> /// <param name="pstatstg">Here is result of method when done.</param> /// <param name="grfStatFlag">Not used, just a placeholder.</param> public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.grfLocksSupported = 0; pstatstg.cbSize = this.Length; }
/// <summary> /// Retrieves the STATSTG structure for this stream. /// </summary> /// <param name="pstatstg"> /// When this method returns, contains a STATSTG structure that describes this stream object. /// This parameter is passed uninitialized. /// </param> /// <param name="grfStatFlag"> /// Members in the STATSTG structure that this method does not return, thus saving some memory allocation operations. /// </param> public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = default(STATSTG); _Validate(); pstatstg.type = STGTY_STREAM; pstatstg.cbSize = _source.Length; pstatstg.grfMode = STGM_READWRITE; pstatstg.grfLocksSupported = LOCK_EXCLUSIVE; }
/// <summary> /// Retrieves the STATSTG structure for this stream object. /// </summary> public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG(); }
// Retrieves the STATSTG structure for this stream public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag) { if (originalStream == null) throw new ObjectDisposedException("originalStream"); pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG(); pstatstg.type = 2; // STGTY_STREAM pstatstg.cbSize = originalStream.Length; pstatstg.grfMode = 2; // STGM_READWRITE; pstatstg.grfLocksSupported = 2; // LOCK_EXCLUSIVE }
public void Stat(out STATSTG pstatstg, int grfStatFlag) { pstatstg = new STATSTG(); pstatstg.cbSize = _stream.Length; if ((grfStatFlag & 1) == 0) { pstatstg.pwcsName = _stream.ToString(); } }