【问题标题】:BlockInput method doesn't work on windows 7?BlockInput 方法在 Windows 7 上不起作用?
【发布时间】:2014-01-17 10:54:23
【问题描述】:

我试图在我的程序中阻止输入,但它不起作用......我阅读了这篇文章,但没有找到解决方案,只有来自 vista-7 及以上 Windows 的问题...... 我也在这里找到了未解决的话题,我希望你能帮助我......

来自

的代码

“先生。LeftTechticle”youtube video

他完美地运行了它......

我尝试了代码,但没有任何反应......

//------------------------------------------Block Class----------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WindowsFormsApplication14
    {
        static class InputBlocker
        {
            [DllImport("user32.dll")]
            static extern bool BlockInput(bool fBlockIt);
            private static Timer timer = new Timer();
            static InputBlocker()
            {
                timer.Tick += new EventHandler(tick);
            }
            public static void Block(int mill)
            {
                BlockInput(true);
                timer.Interval = mill;
                timer.Start();
            }
            private static void tick(object sender ,EventArgs e)
            {
                BlockInput(false);
                timer.Stop();
            }
        }
    }

//------------------------------------------Form class---------------------------------
    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 WindowsFormsApplication14
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                InputBlocker.Block(10000);
            }
        }
    }

【问题讨论】:

  • 亲爱的 Vitor 如您所见,我正在输入代码问题不在于代码 Windows 7 系统的问题不适用于这些语句.. 有什么想法吗?
  • 只是指出他们在那里谈论类似的问题。他们甚至有一个代码示例应该可以满足您的需求,但有人指出它在他们的 Windows 7 上不起作用。您是否尝试过返回 BlockInput(true) 以查看它的 truefalse
  • 嗨..我测试它是假的.. O.o 为什么会这样?

标签: c# .net


【解决方案1】:

正确实施 pinvoke 是战斗的 95%。它需要看起来像这样:

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool BlockInput(bool fBlockIt);

    public static void Block(int mill)
    {
        if (!BlockInput(true)) {
            throw new System.ComponentModel.Win32Exception();
        }
        // etc..
    }

期望您现在收到一条很好的异常消息,告诉您失败的原因。期望您得到“访问被拒绝”,阻止用户操作他的机器需要UAC elevation,原因很明显。

这是一个非常大的锤子,实现 IMessageFilter 并吞下所有输入事件是较小的邪恶。

【讨论】:

  • 汉斯先生,请问您不能用这种方式阻止移动..?如果是,您能告诉我如何将输入操作变黑,因为这只是对方法的理解,我将使用该方法创建阻止用户输入的函数,直到他在 windows 运行时输入正确的密码而不锁定 windows
  • 哦好的好的我知道了我试试>>
  • 嗯,我给了你一个链接,告诉你如何使它成为可能。相当讽刺的是,UAC 提升对话框也会要求输入密码,不是吗?这只是完全错误的方法, BlockInput() 实际上并没有阻止用户重新获得对机器的控制权。 Ctrl+Alt+Del 和任务管理器将其返回。 Windows 使用的 secure 密码输入对话框切换桌面。
  • @RadiSoufan 考虑将汉斯的回答视为这个问题的“答案”。
  • 是的,抱歉我缺乏规则知识,我要感谢韩先生和维托先生:)
猜你喜欢
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多