Пример #1
0
        static LutEvaluator7()
        {
            string    fileName  = LutPath;
            Exception exception = null;

            try
            {
                using (FileStream file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (BinaryReader r = new BinaryReader(file))
                    {
                        BdsVersion version = new BdsVersion();
                        version.Read(r);
                        UInt32 formatId    = r.ReadUInt32();
                        UInt32 lutSize     = r.ReadUInt32();
                        UInt32 lutByteSize = lutSize * 4;
                        _lutPtr = UnmanagedMemory.AllocHGlobalExSmartPtr(lutByteSize);
                        pLut    = (UInt32 *)_lutPtr;
                        UnmanagedMemory.Read(r, _lutPtr, lutByteSize);
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                if (_lutPtr != null)
                {
                    _lutPtr.Dispose();
                    _lutPtr = null;
                }
                pLut = (UInt32 *)IntPtr.Zero.ToPointer();

                // If IO error occured leave the class uninitialized. This is a normal case for table generation.
                // If the application tries to use an uninitialized class, an exception will be thrown somewhere.
                // Otherwise rethrow the exception.
                if (!(exception is IOException))
                {
                    throw exception;
                }
            }
        }
Пример #2
0
        void DoTestReadWrite(IntPtr p, Int64 size)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter w  = new BinaryWriter(ms);

            UnmanagedMemory.Write(w, p, size);
            byte[] buffer = ms.ToArray();
            ms = new MemoryStream(buffer);
            using (SmartPtr p1 = UnmanagedMemory.AllocHGlobalExSmartPtr(size))
            {
                BinaryReader r = new BinaryReader(ms);
                UnmanagedMemory.Read(r, p1.Ptr, size);
                byte *pBytes  = (byte *)p;
                byte *pBytes1 = (byte *)p1;
                for (Int64 i = 0; i < size; ++i)
                {
                    Assert.AreEqual(pBytes[i], pBytes1[i], String.Format("Values differ at pos {0}, size {1}", i, size));
                }
                p1.Dispose();
            }
        }