using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace TestFuckFramework
{
    class Program
    {
        static void Main(string[] args)
        {
            short initRes = MatrixAPI.Init_MatrixAPI();
            Console.WriteLine("Init_MatrixAPI result = {0}", initRes);

            short dongleMemSize = 85;
            short dongleCount = MatrixAPI.Dongle_Count(dongleMemSize);
            Console.WriteLine("Dongle_Count result = {0}", dongleCount);

			short dongleIndex = 0;
			int dongleMemSize2 = (int)MatrixAPI.Dongle_MemSize(dongleIndex, dongleMemSize);
            Console.WriteLine("Dongle_MemSize result = {0}", dongleMemSize2);
			int[] dongleBytes = new int[dongleMemSize2 / 4];
			IntPtr intPtr = Marshal.AllocHGlobal(4 * dongleBytes.Length);
			try
			{
				Console.WriteLine("memory dump before");
				for (int i = 0; i < dongleBytes.Length; i++)
				{
					if (i > 0 && i % 8 == 0) Console.WriteLine();
					Console.Write("{0:X8} ", dongleBytes[i]);
				}
				Console.WriteLine();
				Console.WriteLine();

				initRes = MatrixAPI.Dongle_ReadData(3838057, intPtr, (short)dongleBytes.Length, dongleIndex, dongleMemSize);
				Marshal.Copy(intPtr, dongleBytes, 0, dongleBytes.Length);

				Console.WriteLine("memory dump after");
				for (int i = 0; i < dongleBytes.Length; i++)
				{
					if (i > 0 && i % 8 == 0) Console.WriteLine();
					Console.Write("{0:X8} ", dongleBytes[i]);
				}
				Console.WriteLine();
				Console.WriteLine();
			}
			finally
			{
				Marshal.FreeHGlobal(intPtr);
			}
			Console.WriteLine("memory dump after GC free");
			for (int i = 0; i < dongleBytes.Length; i++)
			{
				if (i > 0 && i % 8 == 0) Console.WriteLine();
				Console.Write("{0:X8} ", dongleBytes[i]);
			}
			Console.WriteLine();
			Console.WriteLine();
			int dongleModel = MatrixAPI.Dongle_Model(dongleIndex, dongleMemSize);
			Console.WriteLine("Dongle_Model result = {0}", dongleModel);
			int dongleSerial = MatrixAPI.Dongle_ReadSerNr(3838057, dongleIndex, dongleMemSize);
			Console.WriteLine("Dongle_ReadSerNr result = {0}", dongleSerial);

			Console.ReadKey();
        }
    }
}
