All pastes #2077525 Raw Edit

gmail/Aida64 C# apps

public text v1 · immutable
#2077525 ·published 2011-08-21 03:53 UTC
rendered paste body
AIDAReader code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace AIDAReader
{
    class AIDA
    {

        static void printHeader(AIDA.Data d)
        {
            String header = "";
            for (int i = 0; i < d.numFans; i++)
            {
                header += "Fan " + i + ",";
            }
            for (int i = 0; i < d.numTemps; i++)
            {
                header += "Temp " + i + ",";
            }
            for (int i = 0; i < d.numVolts; i++)
            {
                header += "Volt " + i + ",";
            }
            header += "Flags";
            Console.WriteLine(header);
        }

        static void printData(AIDA.Data d)
        {
            if (d != null)
            {
                String output = "";
                for (int i = 0; i < d.numFans; i++)
                {
                    output += d.fans[i] + ",";
                }
                for (int i = 0; i < d.numTemps; i++)
                {
                    output += d.temps[i] + ",";
                }
                for (int i = 0; i < d.numVolts; i++)
                {
                    output += d.volts[i] + ",";
                }
                output += d.flags;

                Console.WriteLine(output);
            }
        }

        static void Main(string[] args)
        {
            AIDA sf = new AIDA();

            sf.OpenView();
            bool done = false;
            StreamWriter writer = null;
            if (args.Length > 0)
            {
                try
                {
                    writer = new StreamWriter(args[0]);
                    writer.AutoFlush = true;
                    Console.SetOut(writer); // redirect stdout to the file
                }
                catch (IOException e)
                {
                    Console.WriteLine("Failed to open file: " + args[0] + " : " + e.Message);
                    return;
                }
            }
            try
            {
                bool first = true;
                while (!done)
                {
                    AIDA.Data d = sf.GetData();

                    if (first)
                    {
                        printHeader(d);
                        first = false;
                    }
                    printData(d);

                    if (writer != null)
                        writer.Flush();

                    // Sleep for a second
                    System.Threading.Thread.Sleep(1000);
                }
            }
            finally
            {
                if (writer != null)
                {
                    Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
                    writer.Flush();
                    writer.Close();
                }
                Console.WriteLine("Done!");
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public class Data
        {
            public ushort version;
            public ushort flags;
            public Int32 size;
            public Int32 handle;
            public ushort numTemps;
            public ushort numFans;
            public ushort numVolts;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public Int32[] temps;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public Int32[] fans;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public Int32[] volts;

            public Data()
            {
                temps = new Int32[32];
                fans = new Int32[32];
                volts = new Int32[32];
            }
        }

        #region Win32 API stuff
        public const int FILE_MAP_READ = 0x0004;

        [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern IntPtr OpenFileMapping(int dwDesiredAccess,
            bool bInheritHandle, StringBuilder lpName);

        [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern IntPtr MapViewOfFile(IntPtr hFileMapping,
            int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow,
            int dwNumberOfBytesToMap);

        [DllImport("Kernel32.dll")]
        internal static extern bool UnmapViewOfFile(IntPtr map);

        [DllImport("kernel32.dll")]
        internal static extern bool CloseHandle(IntPtr hObject);
        #endregion

        private bool fileOpen = false;
        private IntPtr map;
        private IntPtr handle;

        ~AIDA()
        {
            CloseView();
        }

        public bool OpenView()
        {
            if (!fileOpen)
            {
                StringBuilder sharedMemFile = new StringBuilder("AIDA64_SensorValues");
                handle = OpenFileMapping(FILE_MAP_READ, false, sharedMemFile);
                if (handle == IntPtr.Zero)
                {
                    throw new Exception("Unable to open file mapping.");
                }
                map = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, Marshal.SizeOf((Type)typeof(Data)));
                if (map == IntPtr.Zero)
                {
                    throw new Exception("Unable to read shared memory.");
                }
                fileOpen = true;
            }
            return fileOpen;
        }

        public void CloseView()
        {
            if (fileOpen)
            {
                UnmapViewOfFile(map);
                CloseHandle(handle);
            }
        }

        public Data GetData()
        {
            if (fileOpen)
            {
                Data data = (Data)Marshal.PtrToStructure(map, typeof(Data));

                return data;
            }
            else
                return null;
        }
    }
}






GMAIL CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Net;
using System.Text;
using System.Runtime.InteropServices;

/// <summary>
/// Get the Number of Email
/// </summary>
/// <param name=”sirbow2”></param>
/// <param name=”password”></param>
/// <returns></returns>

namespace GmailNotifier
{
    class Gmail
    {
        static void Main(string[] args)
        {
            Console.WriteLine("You have: Emails."); 
        }

        private int GetNumberOfMail(string sirbow2, string password)
        {

            XmlDocument xmldoc = GetGmailFeed(sirbow2, password);
            XmlNodeList count = xmldoc.GetElementsByTagName("fullcount");
            return int.Parse(count[0].InnerText);

        }

        /// <summary>
        /// Get the Gmail Atom Feed
        /// Adapted from Kevin Daly
        /// http://www.dotnetjunkies.com/WebLog/kevdaly/archive/2004/12/24/38612.aspx
        /// </summary>
        /// <param name=”sirbow2”></param>
        /// <param name=”password”></param>
        /// <returns></returns>
        private static XmlDocument GetGmailFeed(string sirbow2, string password)
        {

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://mail.google.com/mail/feed/atom/");
            req.Method = "GET";
            req.Credentials = new NetworkCredential(sirbow2, password);
            XmlDocument response = new XmlDocument();
            HttpWebResponse resp;
            try
            {

                resp = (HttpWebResponse)req.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {

                    XmlTextReader reader = new XmlTextReader(resp.GetResponseStream());
                    response.Load(reader);
                    reader.Close();

                }
                resp.Close();
                return response;

            }
            catch
            {
                Console.WriteLine("Error… HttpWebResponse");
                return response;

            }
        }
    }
}