﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.IO;

namespace Task_08
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        wraperIntPrt hFile1;
        wraperIntPrt hFile2;
        wraperIntPrt hFile3;
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            hFile1 = new wraperIntPrt();
            hFile2 = new wraperIntPrt();
            hFile3 = new wraperIntPrt();

            hFile1.IntPtrProp = CreateFile(
                "tx.txt",
                0,
                FileShare.Write,
                IntPtr.Zero,      
                FileMode.OpenOrCreate,                   
                0,                 
                IntPtr.Zero);

            hFile2.IntPtrProp = CreateFile(
                "tx2.txt",
                0,
                FileShare.Write,
                IntPtr.Zero,
                FileMode.OpenOrCreate,
                0,
                IntPtr.Zero);

            hFile3.IntPtrProp = CreateFile(
                "tx3.txt",
                0,
                FileShare.Write,
                IntPtr.Zero,
                FileMode.OpenOrCreate,
                0,
                IntPtr.Zero);
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool ReadFile(
            IntPtr hFile,
            IntPtr lpBuffer,
            uint nNumberOfBytesToRead,
            out uint lpNumberOfBytesRead,
            IntPtr lpOverlapped
            );


        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern IntPtr OpenFile(
                                            string file,
                                            out IntPtr sruct,
                                            Int16 style
                                            );

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr CreateFile(
                                                String lpFileName,
                                                int dwDesiredAccess,
                                                System.IO.FileShare dwShareMode,
                                                IntPtr securityAttrs_MustBeZero,                
                                                System.IO.FileMode dwCreationDisposition,
                                                int dwFlagsAndAttributes,
                                                IntPtr hTemplateFile);

        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern IntPtr Close(IntPtr file);

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            hFile1 = null;
            GC.Collect();
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            hFile3.Dispose();
        }
    }
}
