Example #1
0
 private void ResetEvents()
 {
     WinApiClass.ResetEvent(eventHandleRadu);
     WinApiClass.ResetEvent(eventHandleCorina);
     WinApiClass.ResetEvent(eventHandleMaria);
     WinApiClass.ResetEvent(eventHandlerMusca);
 }
Example #2
0
 private void TerminateThreads()
 {
     WinApiClass.TerminateThread(threadHandle1, 0);
     WinApiClass.TerminateThread(threadHandle2, 0);
     WinApiClass.TerminateThread(threadHandle3, 0);
     WinApiClass.TerminateThread(threadHandle4, 0);
 }
Example #3
0
        private void InitializeThreads()
        {
            threadHandle1 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(ComputeDelta),
                (IntPtr)GCHandle.Alloc(0),
                0,
                out threadId1
                );

            threadHandle2 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(ComputeFirstSolution),
                (IntPtr)GCHandle.Alloc(0),
                0,
                out threadId2
                );

            threadHandle3 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(ComputeSecondSolution),
                (IntPtr)GCHandle.Alloc(0),
                0,
                out threadId3
                );
        }
Example #4
0
        private static void ReadFile()
        {
            handleFileMutex = WinApiClass.CreateMutex(IntPtr.Zero, true, "FileMutex");

            var hasToRead = true;

            while (hasToRead)
            {
                Thread.Sleep(200);
                var buffer = new byte[Constants.NumberOfBytesToRead];
                hasToRead = WinApiClass.ReadFile(
                    intPtrFile,
                    buffer,
                    Constants.NumberOfBytesToRead,
                    out var bytesRead,
                    IntPtr.Zero
                    );
                if (bytesRead <= 0)
                {
                    break;
                }

                foreach (var asciiCode in buffer)
                {
                    var character = (char)asciiCode;
                    Console.Write(character);
                }
            }

            WinApiClass.ReleaseMutex(handleFileMutex);
        }
Example #5
0
        private void CreateAndShowFormIfSemaphoreIsFree()
        {
            var dwWaitResult = WinApiClass.WaitForSingleObject(handleTimer, WinApiClass.INFINITE);

            if (dwWaitResult == WinApiClass.WAIT_TIMEOUT)
            {
                //labelNotification.Invoke((MethodInvoker)delegate { labelNotification.Text = "Tzeapa"; });
                return;
            }

            //var ceva = new Thread(() =>
            //{
            var form    = new Form();
            var textBox = new TextBox();

            textBox.Text = DateTime.Now.ToLongTimeString();
            form.Controls.Add(textBox);
            form.FormClosed += (sender1, args1) =>
            {
                WinApiClass.ReleaseSemaphore(handleSemaphore, 1, IntPtr.Zero);
            };
            form.Show();
            //Application.Run(form);
            //});
            //    ceva.Start();
            //    GC.KeepAlive(ceva);
        }
Example #6
0
 private void InitializeEvents()
 {
     eventHandleInput                 = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "ReadFromKB");
     eventHandleComputeDelta          = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "ComputeDelta");
     eventHandleComputeFirstSolution  = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "ComputeFirstSolution");
     eventHandleComputeSecondSolution = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "ComputeSecondSolution");
 }
Example #7
0
        private uint IncrementGlobalCounter(IntPtr lpParam)
        {
            var paramHandle  = (GCHandle)lpParam;
            var threadNumber = (int)paramHandle.Target;

            while (true)
            {
                WinApiClass.EnterCriticalSection(ref criticalSection);

                for (int iteration = 0; iteration < 4; iteration++)
                {
                    globalCounter++;
                    if (threadNumber == 1)
                    {
                        Console.WriteLine($"{threadNumber}: Global counter: {globalCounter}");
                    }
                    else
                    {
                        Console.WriteLine($"                {threadNumber}: Global counter: {globalCounter}");
                    }
                }

                WinApiClass.LeaveCriticalSection(ref criticalSection);
            }
        }
Example #8
0
 private void btnHOR_AW_SLIDE_Click(object sender, EventArgs e)
 {
     this.Hide();
     WinApiClass.AnimateWindow(this, 3000, WinApiClass.AnimateWindowFlags.AW_HOR_POSITIVE | WinApiClass.AnimateWindowFlags.AW_SLIDE);
     this.btnAW_BLEND.Invalidate();
     this.btnHOR_AW_SLIDE.Invalidate();
     this.btnCenter_AW_SLIDE.Invalidate();
 }
Example #9
0
        private void CheckForErrorAndThrowIfError()
        {
            int lastErrorCode = WinApiClass.GetLastError();

            if (lastErrorCode != 0)
            {
                throw new Exception($"Got error code {lastErrorCode} when reading MBR.BIN");
            }
        }
Example #10
0
        private uint ComputeSecondSolution(IntPtr lpParam)
        {
            WinApiClass.WaitForSingleObject(eventHandleComputeDelta, WinApiClass.INFINITE);
            secondSolution = (-b - Math.Sqrt(delta)) / (2 * a);

            WinApiClass.SetEvent(eventHandleComputeSecondSolution);

            return(0);
        }
Example #11
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper wndHelper = new WindowInteropHelper(this);

            int exStyle = (int)WinApiClass.GetWindowLong(wndHelper.Handle, (int)WinApiClass.GetWindowLongFields.GWL_EXSTYLE);

            exStyle |= (int)WinApiClass.ExtendedWindowStyles.WS_EX_TOOLWINDOW;
            WinApiClass.SetWindowLong(wndHelper.Handle, (int)WinApiClass.GetWindowLongFields.GWL_EXSTYLE, (IntPtr)exStyle);
        }
Example #12
0
        private void InitializeThreads()
        {
            threadHandle1 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetMinimumValueFromRow),
                (IntPtr)GCHandle.Alloc(0),
                0,
                out threadId1
                );

            threadHandle2 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetMinimumValueFromRow),
                (IntPtr)GCHandle.Alloc(1),
                0,
                out threadId2
                );

            threadHandle3 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetMinimumValueFromRow),
                (IntPtr)GCHandle.Alloc(2),
                0,
                out threadId3
                );

            threadHandle4 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetMinimumValueFromRow),
                (IntPtr)GCHandle.Alloc(3),
                0,
                out threadId4
                );

            threadHandle5 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetMinimumValueFromRow),
                (IntPtr)GCHandle.Alloc(4),
                0,
                out threadId5
                );

            threadHandle6 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(GetSmallestMinimum),
                IntPtr.Zero,
                0,
                out threadId6
                );
        }
Example #13
0
        private void InitializeTimer()
        {
            var securityAttributes = new WinApiClass.SECURITY_ATTRIBUTES();

            handleTimer = WinApiClass.CreateWaitableTimer(
                ref securityAttributes,
                false,
                "TimerMessageBoxes"
                );
        }
Example #14
0
 private void InitializeEvents()
 {
     eventHandleRadu   = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "radu");
     eventHandleCorina = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "corina");
     eventHandleMaria  = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "maria");
     eventHandlerMusca = WinApiClass.CreateEvent(IntPtr.Zero, true, false, "musca");
     eventHandlers     = new List <IntPtr>()
     {
         eventHandleRadu, eventHandleCorina, eventHandleMaria, eventHandlerMusca
     };
 }
Example #15
0
        private void InitializeSemaphore()
        {
            var securityAttributes = new WinApiClass.SECURITY_ATTRIBUTES();

            handleSemaphore = WinApiClass.CreateSemaphore(
                ref securityAttributes,
                5,
                5,
                "SemaphoreForms"
                );
        }
Example #16
0
 private void InitializeIntPtrFileRead()
 {
     intPtrFile = WinApiClass.CreateFile(
         filePath,
         WinApiClass.FileAccess.GenericRead,
         WinApiClass.FileShare.Write | WinApiClass.FileShare.Read,
         IntPtr.Zero,
         WinApiClass.FileMode.OPEN_EXISTING,
         0,
         IntPtr.Zero
         );
 }
Example #17
0
 private void InitializeIntPtrFileWriteAppend()
 {
     intPtrFile = WinApiClass.CreateFile(
         filePath,
         WinApiClass.FileAccess.FILE_APPEND_DATA,
         WinApiClass.FileShare.Write | WinApiClass.FileShare.Read,
         IntPtr.Zero,
         WinApiClass.FileMode.OPEN_EXISTING,
         0,
         IntPtr.Zero
         );
 }
Example #18
0
        private void InitializeCoefficients()
        {
            Console.Write("a: ");
            a = Convert.ToInt32(Console.ReadLine());

            Console.Write("b: ");
            b = Convert.ToInt32(Console.ReadLine());

            Console.Write("c: ");
            c = Convert.ToInt32(Console.ReadLine());

            WinApiClass.SetEvent(eventHandleInput);
        }
Example #19
0
        public void Start()
        {
            InitializeThreads();
            InitializeEvents();

            WinApiClass.ResumeThread((IntPtr)threadHandle1);
            WinApiClass.ResumeThread((IntPtr)threadHandle2);
            WinApiClass.ResumeThread((IntPtr)threadHandle3);

            InitializeCoefficients();

            ShowSolutions();
        }
Example #20
0
 private void btnAW_BLEND_Click(object sender, EventArgs e)
 {
     // Скрываем окно
     this.Hide();
     // Запускаем анимацию
     // второй параметр в скобках время анимации
     // в милисекундах
     WinApiClass.AnimateWindow(this, 1000, WinApiClass.AnimateWindowFlags.AW_ACTIVATE | WinApiClass.AnimateWindowFlags.AW_BLEND);
     // Отображаем кномпки после анимации
     this.btnAW_BLEND.Invalidate();
     this.btnHOR_AW_SLIDE.Invalidate();
     this.btnCenter_AW_SLIDE.Invalidate();
 }
Example #21
0
        private uint ComputeDelta(IntPtr lpParam)
        {
            WinApiClass.WaitForSingleObject(eventHandleInput, WinApiClass.INFINITE);
            delta = b * b - (4 * a * c);

            if (delta < 0)
            {
                throw new Exception($"delta: {delta}");
            }

            WinApiClass.SetEvent(eventHandleComputeDelta);

            return(0);
        }
Example #22
0
        private void ShowSolutions()
        {
            WinApiClass.WaitForMultipleObjects(
                2,
                new IntPtr[2] {
                eventHandleComputeFirstSolution, eventHandleComputeSecondSolution
            },
                true,
                WinApiClass.INFINITE
                );

            Console.WriteLine($"x1: {firstSolution}");
            Console.WriteLine($"x2: {secondSolution}");
        }
Example #23
0
        private void buttonEndTask_Click(object sender, System.EventArgs e)
        {
            var selectedProcess = processListBox.SelectedItem as string;
            var processID       = Convert.ToInt32(selectedProcess.Split('#')[0]);

            var ptr = WinApiClass.OpenProcess(1, true, processID);

            WinApiClass.TerminateProcess(ptr, 0);

            MessageBox.Show(WinApiClass.GetLastError().ToString());

            var processes = GetProcesses();

            UpdateList(processes);
        }
Example #24
0
        public void CitesteChestiiDinMbr()
        {
            var intPtrReadFile = WinApiClass.CreateFile(
                MbrFilePath,
                WinApiClass.FileAccess.GenericRead,
                WinApiClass.FileShare.None,
                IntPtr.Zero,
                WinApiClass.FileMode.OPEN_EXISTING,
                0,
                IntPtr.Zero
                );

            CheckForErrorAndThrowIfError();

            byte[] buffer    = new byte[Constants.SectorSizeInBytes];
            uint   bytesRead = 0;

            WinApiClass.ReadFile(
                intPtrReadFile,
                buffer,
                (uint)Constants.SectorSizeInBytes,
                out bytesRead,
                IntPtr.Zero
                );

            for (int iteration = 0; iteration < 4; iteration++)
            {
                Console.WriteLine($" > Partition {iteration + 1}");
                Console.WriteLine("-----------------------------------");

                Console.WriteLine($"Boot Flag: {buffer[446 + iteration * 16].ToString("X")}");
                Console.WriteLine($"Head address: {buffer[447 + iteration * 16].ToString("X")}");

                Console.WriteLine($"Sector head: {((buffer[449 + iteration * 16] << 8) | buffer[448 + iteration * 16]).ToString("X")}");

                Console.WriteLine($"Partition type: {buffer[450 + iteration * 16].ToString("X")}");

                Console.WriteLine($"Head address end: {buffer[451 + iteration * 16].ToString("X")}");
                Console.WriteLine($"Sector address end: {((buffer[453 + iteration * 16] << 8) | buffer[452 + iteration * 16]).ToString("X")}");

                Console.WriteLine($"Relative sectors: {(int)(buffer[457 + iteration * 16] << 3 * 8) | (buffer[456 + iteration * 16] << 2 * 8) | (buffer[455 + iteration * 16] << 8) | buffer[454 + iteration * 16]}");

                Console.WriteLine($"Total sectors: {(int)(buffer[461 + iteration * 16] << 3 * 8) | (buffer[460 + iteration * 16] << 2 * 8) | (buffer[459 + iteration * 16] << 8) | buffer[458 + iteration * 16]}\n");


                intPtrReadFile = new IntPtr(intPtrReadFile.ToInt64() + 512);
            }
        }
Example #25
0
        private void CreateAndShowFormIfSemaphoreIsFree()
        {
            var dwWaitResult = WinApiClass.WaitForSingleObject(handleSemaphore, 0);

            if (dwWaitResult == WinApiClass.WAIT_TIMEOUT)
            {
                MessageBox.Show("CAN NOT OPEN ANY MORE FORMS.");
                return;
            }

            var form = new Form();

            form.FormClosed += (sender1, args1) =>
            {
                WinApiClass.ReleaseSemaphore(handleSemaphore, 1, IntPtr.Zero);
            };
            form.Show();
        }
Example #26
0
        private uint GetMinimumValueFromRow(IntPtr lpParam)
        {
            WinApiClass.EnterCriticalSection(ref criticalSection);

            var paramHandle = (GCHandle)lpParam;
            var rowNumber   = (int)paramHandle.Target;

            for (int col = 0; col < Constants.MatrixColumnLength; col++)
            {
                if (matrix[rowNumber, col] < rowMinimumValue[rowNumber])
                {
                    rowMinimumValue[rowNumber] = matrix[rowNumber, col];
                }
            }

            WinApiClass.LeaveCriticalSection(ref criticalSection);

            return(0);
        }
Example #27
0
        private uint GetSmallestMinimum(IntPtr lpParam)
        {
            var minimumValue = int.MaxValue;

            WinApiClass.EnterCriticalSection(ref criticalSection);

            for (int col = 0; col < 5; col++)
            {
                if (rowMinimumValue[col] < minimumValue)
                {
                    minimumValue = rowMinimumValue[col];
                }
            }

            WinApiClass.LeaveCriticalSection(ref criticalSection);

            Console.WriteLine($"Minimum value: {minimumValue}");

            return(0);
        }
Example #28
0
        private void SetTimer()
        {
            var dueTime = -10L * 10000000;

            WinApiClass.SetWaitableTimer(
                handleTimer,
                ref dueTime,
                2 * 1000,
                null,
                IntPtr.Zero,
                false
                );
            var i = 0;

            while (i < 5)
            {
                i++;
                CreateAndShowFormIfSemaphoreIsFree();
            }
        }
Example #29
0
        private void InitializeThreads()
        {
            threadHandle1 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(ReadFile),
                (IntPtr)GCHandle.Alloc(1),
                0,
                out threadId1
                );

            threadHandle2 = WinApiClass.CreateThread(
                IntPtr.Zero,
                0,
                new WinApiClass.LPTHREAD_START_ROUTINE(WriteToFile),
                (IntPtr)GCHandle.Alloc(2),
                0,
                out threadId2
                );
        }
Example #30
0
        private uint WriteToFile(IntPtr lpParam)
        {
            WinApiClass.EnterCriticalSection(ref criticalSection);
            InitializeIntPtrFileWriteAppend();

            var stringToWrite = Constants.StringToWrite;
            var buffer        = stringToWrite.ToCharArray().Select(c => (byte)c).ToArray();

            var result = WinApiClass.WriteFile(
                intPtrFile,
                buffer,
                (uint)buffer.Length,
                out uint lpNumberOfBytesWritten,
                IntPtr.Zero
                );

            WinApiClass.LeaveCriticalSection(ref criticalSection);

            return(0);
        }