private static Result CloseImpl(IntPtr thisPtr, IntPtr pData) { try { IncludeShadow shadow = ToShadow <IncludeShadow>(thisPtr); Include callback = (Include)shadow.Callback; if (shadow._frames.TryGetValue(pData, out Frame frame)) { shadow._frames.Remove(pData); callback.Close(frame.Stream); frame.Dispose(); } return(Result.Ok); } catch (SharpGenException exception) { return(exception.ResultCode.Code); } catch (Exception) { return(Result.Fail); } }
private static Result OpenImpl(IntPtr thisPtr, IncludeType includeType, IntPtr fileNameRef, IntPtr pParentData, ref IntPtr dataRef, ref int bytesRef) { unsafe { try { IncludeShadow shadow = ToShadow <IncludeShadow>(thisPtr); Include callback = (Include)shadow.Callback; Stream stream = null; Stream parentStream = null; if (shadow._frames.ContainsKey(pParentData)) { parentStream = shadow._frames[pParentData].Stream; } stream = callback.Open(includeType, Marshal.PtrToStringAnsi(fileNameRef), parentStream); if (stream == null) { return(Result.Fail); } GCHandle handle; //if (stream is DataStream) //{ // // Magic shortcut if we happen to get a DataStream // var data = (DataStream)stream; // dataRef = data.PositionPointer; // bytesRef = (int)(data.Length - data.Position); // handle = new GCHandle(); //} //else { // Read the stream into a byte array and pin it byte[] data = ReadStream(stream); handle = GCHandle.Alloc(data, GCHandleType.Pinned); dataRef = handle.AddrOfPinnedObject(); bytesRef = data.Length; } shadow._frames.Add(dataRef, new Frame(stream, handle)); return(Result.Ok); } catch (SharpGenException exception) { return(exception.ResultCode.Code); } catch (Exception) { return(Result.Fail); } } }