Пример #1
0
        private IntPtr ReadHandleFromFromMappedFile()
        {
            int error = NativeConstants.ERROR_SUCCESS;

            IntPtr lpData = SafeNativeMethods.MapViewOfFile(
                this.hFileMapping,
                NativeConstants.FILE_MAP_READ,
                0,
                0,
                new UIntPtr((uint)mappingSize));

            error = Marshal.GetLastWin32Error();

            if (lpData == IntPtr.Zero)
            {
                throw new Win32Exception(error, "MapViewOfFile() returned NULL (" + error + ")");
            }

            byte[] int64Bytes = new byte[(int)mappingSize];
            Marshal.Copy(lpData, int64Bytes, 0, mappingSize);

            long int64 = 0;

            for (int i = 0; i < mappingSize; ++i)
            {
                int64 += (long)(int64Bytes[i] << (i * 8));
            }

            bool bResult = SafeNativeMethods.UnmapViewOfFile(lpData);

            error = Marshal.GetLastWin32Error();

            if (!bResult)
            {
                throw new Win32Exception(error, "UnmapViewOfFile() returned FALSE (" + error + ")");
            }

            IntPtr hValue = new IntPtr(int64);

            return(hValue);
        }
Пример #2
0
        private void WriteHandleValueToMappedFile(IntPtr hValue)
        {
            int  error   = NativeConstants.ERROR_SUCCESS;
            bool bResult = true;

            IntPtr lpData = SafeNativeMethods.MapViewOfFile(
                this.hFileMapping,
                NativeConstants.FILE_MAP_WRITE,
                0,
                0,
                new UIntPtr((uint)mappingSize));

            error = Marshal.GetLastWin32Error();

            if (lpData == IntPtr.Zero)
            {
                throw new Win32Exception(error, "MapViewOfFile() returned NULL (" + error + ")");
            }

            long int64 = hValue.ToInt64();

            byte[] int64Bytes = new byte[(int)mappingSize];

            for (int i = 0; i < mappingSize; ++i)
            {
                int64Bytes[i] = (byte)((int64 >> (i * 8)) & 0xff);
            }

            Marshal.Copy(int64Bytes, 0, lpData, mappingSize);

            bResult = SafeNativeMethods.UnmapViewOfFile(lpData);
            error   = Marshal.GetLastWin32Error();

            if (!bResult)
            {
                throw new Win32Exception(error, "UnmapViewOfFile() returned FALSE (" + error + ")");
            }
        }