恼人的DisconnectContext检测异常

时间:2012-04-07 21:35:56

标签: c# .net winapi wmi screen

我得到了令人讨厌的DisconectContext异常。 我知道我的计算机支持WMI功能,因为其他人的应用程序可以做同样的事情。

基本上,这是一个在按下Fn + Up或Fn + Down时设置屏幕亮度的程序。

所有代码都在同一个线程中运行,程序是WinForms,但我将其编译为控制台应用程序,因此我可以看到调试消息。 我试过把它变成Windows应用程序,但它没有解决问题,所以我不认为它是控制台。

在注释...

的SetBrightness(int)中发生异常

HiddenForm.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComputerControl
{
public partial class HiddenForm : Form
{
    public HiddenForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        WindowState = FormWindowState.Minimized;

        Utils.SetHook();

        if (Utils._hook.ToInt32() <= 0)
        {
            MessageBox.Show("The application failed to hook the keyboard and will now exit.", "Application Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }

        bool supports = Utils.LoadBrightnessLevels();

        if (!supports)
        {
            MessageBox.Show("The computer does not support WMI.  The application will now exit.", "Application Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();
        }
    }
}
}

Utils.cs:

using System;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;

namespace ComputerControl
{
public class Utils
{
    public static int index;

    static byte[] supportedBrightness;

    public static void IncrementBrightness()
    {
        if (index == supportedBrightness.Length - 1)
            return;

        SetBrightness(++index);
    }

    public static void DecrementBrightness()
    {
        if (index == 0)
            return;

        SetBrightness(--index);
    }

    public static void SetBrightness(int index)
    {
        ManagementScope ms = new ManagementScope("root\\WMI");
        SelectQuery sq = new SelectQuery("WmiMonitorBrightnessMethods");

        ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, sq);

        ManagementObjectCollection moc = mos.Get(); // Context <id> is disconnected.  No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

        foreach (ManagementObject mo in moc)
        {
            mo.InvokeMethod("WmiSetBrightness", new object[] { 60, supportedBrightness[index] });
            break;
        }

        moc.Dispose();
        mos.Dispose();
    }

    public static int GetCurrentBrightness()
    {
        ManagementScope ms = new ManagementScope("root\\WMI");
        SelectQuery sq = new SelectQuery("WmiMonitorBrightness");

        ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, sq);

        ManagementObjectCollection moc = mos.Get();

        byte currentBrightness = 0;

        foreach(ManagementObject mo in moc)
        {
            currentBrightness = (byte)mo.GetPropertyValue("CurrentBrightness");
            break;
        }

        moc.Dispose();
        mos.Dispose();

        return (int)currentBrightness;
    }

    public static bool LoadBrightnessLevels()
    {
        supportedBrightness = GetBrightnessLevels();

        if (supportedBrightness.Length > 0)
        {
            int current = GetCurrentBrightness();
            index = Array.IndexOf(supportedBrightness, (byte)current);
        }

        return supportedBrightness.Length != 0;
    }

    public static byte[] GetBrightnessLevels()
    {
        ManagementScope ms = new ManagementScope("root\\WMI");
        SelectQuery sq = new SelectQuery("WmiMonitorBrightness");

        ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, sq);

        byte[] levels = new byte[0];

        try
        {
            ManagementObjectCollection moc = mos.Get();

            foreach (ManagementObject o in moc)
            {
                levels = (byte[])o.GetPropertyValue("Level");
                break;
            }

            moc.Dispose();
        }
        catch { }

        mos.Dispose();

        return levels;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct KeyboardHookStruct
    {
        public int VirtualKeyCode;
        public int ScanCode;
        public int Flags;
        public int Time;
        public IntPtr ExtraInfo;
    }

    public static IntPtr _hook = IntPtr.Zero;

    public static void SetHook()
    {
        _hook = SetWindowsHookEx(13, HookCallback, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
    }

    public static void Unhook()
    {
        UnhookWindowsHookEx(_hook);
    }

    public delegate IntPtr LowLevelKeyBoardProc(int nCode, IntPtr wParam, IntPtr lParam);

    public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

        if (nCode >= 0 && wParam == (IntPtr)0x0100)
        {
            KeyboardHookStruct khs = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

            // up is SC:8, VK:255, down is SC:9, VK:255
            if (khs.VirtualKeyCode == 255)
            {
                if (khs.ScanCode == 8)
                {
                    IncrementBrightness();
                    Console.WriteLine("Brightness up! {0}", supportedBrightness[index]);
                }
                else if (khs.ScanCode == 9)
                {
                    DecrementBrightness();
                    Console.WriteLine("Brightness down! {0}", supportedBrightness[index]);
                }
            }
        }

        return CallNextHookEx(_hook, nCode, wParam, lParam);
    }

    [DllImport("user32.dll")]
    public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyBoardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll")]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    private static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl);

    [DllImport("kernel32.dll")]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}

的Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ComputerControl
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new HiddenForm());

        // Check if the hook is set, and if it is, unhook it.
        if (Utils._hook.ToInt32() > 0)
            Utils.Unhook();
    }
}
}

感谢所有帮助! 谢谢!

0 个答案:

没有答案