Esempio n. 1
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            handle = CreateFile("\\\\.\\IBMPmDrv", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FILE_ATTRIBUTES.Normal, IntPtr.Zero);
            if (handle == IntPtr.Zero || handle.ToInt32() == -1)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != 0)
                {
                    Marshal.ThrowExceptionForHR(errorCode);
                }
                else
                {
                    throw new Exception(
                              "SetupDiGetDeviceInterfaceDetail call failed but Win32 didn't catch an error.");
                }
            }

            GET_BATTERY_THRESH_IN batteryThresh = new GET_BATTERY_THRESH_IN();

            batteryThresh.BatteryID = DEFAULT_BATTERY_ID;
            int    batteryThreshSize    = Marshal.SizeOf(batteryThresh);
            IntPtr batteryThreshPointer = Marshal.AllocHGlobal(batteryThreshSize);

            Marshal.StructureToPtr(batteryThresh, batteryThreshPointer, false);

            GET_BATTERY_THRESH_OUT batteryThreshOut = new GET_BATTERY_THRESH_OUT();
            int    batteryThreshOutSize             = Marshal.SizeOf(batteryThreshOut);
            IntPtr batteryThreshOutPointer          = Marshal.AllocHGlobal(batteryThreshOutSize);

            Marshal.StructureToPtr(batteryThreshOut, batteryThreshOutPointer, false);
            try
            {
                DeviceIoControl(handle, GET_BATTERY_THRESH_START, batteryThreshSize, batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                GET_BATTERY_THRESH_OUT outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer, typeof(GET_BATTERY_THRESH_OUT));
                decimal startValue             = outInfo.Value + 1;
                startThreshold.Value = startValue >= 100 ? 30 : startValue;

                DeviceIoControl(handle, GET_BATTERY_THRESH_STOP, batteryThreshSize, batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer, typeof(GET_BATTERY_THRESH_OUT));
                byte stopValue = outInfo.Value;
                stopThreshold.Value = stopValue == 0x00 || stopValue >= 100 ? 100 : stopValue;
            }
            finally
            {
                Marshal.FreeHGlobal(batteryThreshPointer);
                Marshal.FreeHGlobal(batteryThreshOutPointer);
            }
        }
Esempio n. 2
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            handle = CreateFile("\\\\.\\IBMPmDrv", FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FILE_ATTRIBUTES.Normal, IntPtr.Zero);
            if (handle == IntPtr.Zero || handle.ToInt32() == -1)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != 0)
                {
                    Marshal.ThrowExceptionForHR(errorCode);
                }
                else
                {
                    throw new Exception(
                              "SetupDiGetDeviceInterfaceDetail call failed but Win32 didn't catch an error.");
                }
            }

            GET_BATTERY_THRESH_IN batteryThresh = new GET_BATTERY_THRESH_IN();

            batteryThresh.BatteryID = DEFAULT_BATTERY_ID;
            int    batteryThreshSize    = Marshal.SizeOf(batteryThresh);
            IntPtr batteryThreshPointer = Marshal.AllocHGlobal(batteryThreshSize);

            Marshal.StructureToPtr(batteryThresh, batteryThreshPointer, false);

            GET_BATTERY_THRESH_OUT batteryThreshOut = new GET_BATTERY_THRESH_OUT();
            int    batteryThreshOutSize             = Marshal.SizeOf(batteryThreshOut);
            IntPtr batteryThreshOutPointer          = Marshal.AllocHGlobal(batteryThreshOutSize);

            Marshal.StructureToPtr(batteryThreshOut, batteryThreshOutPointer, false);
            try
            {
                DeviceIoControl(handle, GET_BATTERY_THRESH_START, batteryThreshSize, batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                GET_BATTERY_THRESH_OUT outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer, typeof(GET_BATTERY_THRESH_OUT));
                decimal startValue             = outInfo.Value + 1;
                startThreshold.Value = startValue >= 100 ? 30 : startValue;

                DeviceIoControl(handle, GET_BATTERY_THRESH_STOP, batteryThreshSize, batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer, typeof(GET_BATTERY_THRESH_OUT));
                byte stopValue = outInfo.Value;
                stopThreshold.Value = stopValue == 0x00 || stopValue >= 100 ? 100 : stopValue;
            }
            finally
            {
                Marshal.FreeHGlobal(batteryThreshPointer);
                Marshal.FreeHGlobal(batteryThreshOutPointer);
            }

            // contributed code - added command line arguments support
            string[] cmds = Environment.GetCommandLineArgs();
            if (cmds.Length == 3)
            {
                decimal start = Convert.ToInt32(cmds[2]) - 1;
                decimal stop  = Convert.ToInt32(cmds[1]);
                stop = stop == 100 ? 0x00 : stop;
                //先执行停止指令
                SetBatteryThresh(handle, stop, DEFAULT_BATTERY_ID, SET_BATTERY_THRESH_STOP);
                //再执行开始指令
                SetBatteryThresh(handle, start, DEFAULT_BATTERY_ID, SET_BATTERY_THRESH_START);
                Environment.Exit(0);
            }
        }
Esempio n. 3
0
        private void FormMain_Load(object sender, EventArgs e)
        {
            handle = CreateFile("\\\\.\\IBMPmDrv", FileAccess.ReadWrite, FileShare.ReadWrite,
                                IntPtr.Zero, FileMode.Open, FILE_ATTRIBUTES.Normal, IntPtr.Zero);
            if (handle == IntPtr.Zero || handle.ToInt32() == -1)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != 0)
                {
                    Marshal.ThrowExceptionForHR(errorCode);
                }
                else
                {
                    throw new Exception(
                              "SetupDiGetDeviceInterfaceDetail call failed but Win32 didn't catch an error.");
                }
            }

            if (File.Exists(this.defaultConfigFilePath))
            {
                // config file exist then get config value
                string[] cxt = File.ReadAllLines(this.defaultConfigFilePath);
                if (cxt == null)
                {
                    // error
                    return;
                }

                bool parseConfigFileFailed = false;
                foreach (string c in cxt)
                {
                    try
                    {
                        if (c.StartsWith(FormMain.start))
                        {
                            decimal _start = decimal.Parse(c.Substring(FormMain.start.Length));
                            startThreshold.Value = _start;
                        }
                        else if (c.StartsWith(FormMain.stop))
                        {
                            decimal _stop = decimal.Parse(c.Substring(FormMain.stop.Length));
                            stopThreshold.Value = _stop;
                        }
                        else
                        {
                            // other config items.
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show("parse config file failed, passed: " + ex.Message);
                        parseConfigFileFailed = true;
                    }
                }
                if (!parseConfigFileFailed)
                {
                    thresholdExec();
                    return;
                }
            }

            GET_BATTERY_THRESH_IN batteryThresh = new GET_BATTERY_THRESH_IN();

            batteryThresh.BatteryID = DEFAULT_BATTERY_ID;
            int    batteryThreshSize    = Marshal.SizeOf(batteryThresh);
            IntPtr batteryThreshPointer = Marshal.AllocHGlobal(batteryThreshSize);

            Marshal.StructureToPtr(batteryThresh, batteryThreshPointer, false);

            GET_BATTERY_THRESH_OUT batteryThreshOut = new GET_BATTERY_THRESH_OUT();
            int    batteryThreshOutSize             = Marshal.SizeOf(batteryThreshOut);
            IntPtr batteryThreshOutPointer          = Marshal.AllocHGlobal(batteryThreshOutSize);

            Marshal.StructureToPtr(batteryThreshOut, batteryThreshOutPointer, false);
            try
            {
                DeviceIoControl(handle, GET_BATTERY_THRESH_START, batteryThreshSize,
                                batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                GET_BATTERY_THRESH_OUT outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer,
                                                                                                typeof(GET_BATTERY_THRESH_OUT));
                decimal startValue = outInfo.Value + 1;
                startThreshold.Value = startValue >= 100 ? 30 : startValue;

                DeviceIoControl(handle, GET_BATTERY_THRESH_STOP, batteryThreshSize,
                                batteryThreshPointer, batteryThreshOutSize, batteryThreshOutPointer);
                outInfo = (GET_BATTERY_THRESH_OUT)Marshal.PtrToStructure(batteryThreshOutPointer,
                                                                         typeof(GET_BATTERY_THRESH_OUT));
                byte stopValue = outInfo.Value;
                stopThreshold.Value = stopValue == 0x00 || stopValue >= 100 ? 100 : stopValue;
            }
            finally
            {
                Marshal.FreeHGlobal(batteryThreshPointer);
                Marshal.FreeHGlobal(batteryThreshOutPointer);
            }
        }