private static IntPtr Open(string fileName, int flag, int mode, ref int error, IntPtr data) { FileAccess access = FileAccessFromOpenFlag(flag); FileMode fileMode = FileModeFromOpenFlag(flag); FileShare share = FileShareFromModeFlag(mode); CustomCabData userData = (CustomCabData)((GCHandle)data).Target; var directory = Path.GetDirectoryName(fileName); try { if (access == FileAccess.Write || access == FileAccess.ReadWrite) { if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } } var stream = new FileStream(fileName, fileMode, access, share); return((IntPtr)GCHandle.Alloc(stream)); } catch (IOException e) { userData.ErrorInfo = e; } return(IntPtr.Zero); }
public void CreateFciContext(string cabFileName) { this.error = new NativeHelper.CabError(); NativeHelper.CompressionInfo cabInfo = new NativeHelper.CompressionInfo(cabFileName); this.customData = new CustomCabData(); CustomDataHandle = GCHandle.Alloc(this.customData); fciContext = NativeHelper.FCICreate( this.error, FciFilePlacedMethod, FciAllocMemHandler, FdiFreeMemHandler, FciOpenMethod, FciReadMethod, FciWriteMethod, FciCloseMethod, FciSeekMethod, FciDlDeleteMethod, FciGetTempFileMethod, cabInfo, (IntPtr)CustomDataHandle); if (this.customData.ErrorInfo != null) { throw this.customData.ErrorInfo; } }
private static bool GetTempFile(IntPtr tempFileName, int tempNameSize, IntPtr data) { string fileName = string.Empty; CustomCabData userData = (CustomCabData)((GCHandle)data).Target; try { fileName = Path.GetTempFileName(); if (!string.IsNullOrEmpty(fileName) && fileName.Length < tempNameSize) { byte[] name = Encoding.GetEncoding(0).GetBytes(fileName); Marshal.Copy(name, 0, tempFileName, name.Length); Marshal.WriteByte(tempFileName, name.Length, 0); } } catch (IOException ex) { userData.ErrorInfo = ex; return(false); } finally { if (!string.IsNullOrEmpty(fileName)) { File.Delete(fileName); } } return(true); }
private static IntPtr GetOpenInfo( string fileName, ref int date, ref int time, ref short attributes, ref int error, IntPtr data) { CustomCabData userData = (CustomCabData)((GCHandle)data).Target; try { attributes = AttributeFlagsFromFileAttributes(File.GetAttributes(fileName)); var dateTime = File.GetLastWriteTime(fileName); date = ConvertDate(dateTime); time = ConvertTime(dateTime); var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None); return((IntPtr)GCHandle.Alloc(stream)); } catch (IOException ex) { userData.ErrorInfo = ex; error = 1; return(IntPtr.Zero); } }
private static int Read(IntPtr fileHandle, byte[] buffer, int count, ref int error, IntPtr data) { FileStream stream = (FileStream)((GCHandle)fileHandle).Target; CustomCabData cabData = (CustomCabData)((GCHandle)data).Target; try { return(stream.Read(buffer, 0, count)); } catch (IOException e) { cabData.ErrorInfo = e; return(0); } }
private static int Seek(IntPtr fileHandle, int distance, int seekType, ref int error, IntPtr data) { FileStream stream = (FileStream)((GCHandle)fileHandle).Target; SeekOrigin origin = SeekOriginFromType(seekType); CustomCabData userData = (CustomCabData)((GCHandle)data).Target; try { return((int)stream.Seek(distance, origin)); } catch (IOException e) { userData.ErrorInfo = e; return(-1); } }
private static int Close(IntPtr fileHandle, ref int error, IntPtr data) { CustomCabData userData = (CustomCabData)((GCHandle)data).Target; try { FileStream stream = (FileStream)((GCHandle)fileHandle).Target; stream.Dispose(); return(0); } catch (IOException e) { userData.ErrorInfo = e; return(-1); } finally { ((GCHandle)fileHandle).Free(); } }