C# User32.dll:找不到注册表项
我最近使用Visual Studio 2010在Visual C#.NET 2010中制作了一个程序,作为Windows窗体应用程序。该程序通过user32.dll函数“RegisterHotkey”使用全局热键。 一切都很顺利。 当按下一个已注册的热键时,我能够显示一个MessageBox(例如)。 然后,今天,在VisualStudio中出现了一些奇怪的错误(与热键无关)(实际上它只是一个未加载的图像)之后,RegisterHotkey函数不再工作 我没有更改热键代码中的任何内容 当我在VisualStudio中调试时,我没有得到任何异常。 通过断点,我发现代码在RegisterHotkey函数处停止。 当我从项目的“debug”文件夹执行.exe文件时,程序显示一个错误,表明在“user32”DLL中找不到“入口点”RegisterHotkey 这很奇怪,因为它一直在工作 为了检查我的项目或代码是否是原因,我创建了一个新的Windows窗体应用程序并输入了代码:C# User32.dll:找不到注册表项,c#,.net,forms,user32,registerhotkey,C#,.net,Forms,User32,Registerhotkey,我最近使用Visual Studio 2010在Visual C#.NET 2010中制作了一个程序,作为Windows窗体应用程序。该程序通过user32.dll函数“RegisterHotkey”使用全局热键。 一切都很顺利。 当按下一个已注册的热键时,我能够显示一个MessageBox(例如)。 然后,今天,在VisualStudio中出现了一些奇怪的错误(与热键无关)(实际上它只是一个未加载的图像)之后,RegisterHotkey函数不再工作 我没有更改热键代码中的任何内容 当我在Vi
using System.Runtime.InteropServices;
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int RegisterHotkey(IntPtr Hwnd, int ID, int Modifiers, int Key);
[DllImport("kernel32", EntryPoint = "GlobalAddAtomA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern short GlobalAddAtom(string IDString);
private void Form1_Load(object sender, EventArgs e)
{
int atomid = GlobalAddAtom("hallo");
RegisterHotkey(this.Handle, atomid, 0, (int)Keys.A);
}
}
}
这产生了同样的错误。尝试调用RegisterHotkey函数时出错。这次我尝试输入尽可能少的代码
表单没有控件,它应该做的就是在其加载事件中注册热键
我的问题是:
有人能告诉我为什么突然找不到注册表项了吗?
我在哪里犯了错误吗?
我该怎么做才能让它再次发挥作用呢
我试图导入“user32.dll”而不是“user32”,但除了错误消息中的文本外,它没有改变任何内容。在那里,“user32”被替换为“user32.dll”
编辑:我不知道它是否相关,但我使用Windows 7 Professional 64位版本和.NET framework 4.0(不是客户端配置文件)这可能是因为函数名是
RegisterHotKey
,大写K,而不是RegisterHotKey
请尝试完全按照上所述进行声明:
哦,非常抱歉。我以为这与奇怪的错误有关,忘记了我用“热键”替换了应用程序中的所有“热键”。谢谢你指出这一点,我自己可能永远也不会发现这一点。
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);