尝试在 C++/CLI 项目中调用 GetAsyncKeyState() 时出错

发布于 2024-12-09 22:53:34 字数 822 浏览 3 评论 0原文

我正在开发一个相当复杂的项目,在该项目中我正在使用 C++/CLI 的项目访问用普通旧式 C++ 编写的类。它是一个 Windows 窗体 GUI 项目,使用许多与其(非 CLI)C++ 姐妹项目相同的功能。

在我试图调整以在两种环境中工作的一门课程中,我使用此功能轮询按键:

inline bool IsKeyDown(unsigned char ch) const { 
    return (GetAsyncKeyState(ch) & (1u << 15)) != 0; 
}

我同时收到“未解析的令牌”和“未解析的外部符号”错误 “extern “C”短 __stdcall GetAsyncKeyState(int)” (?GetAsyncKeyState@@$$J14YGFH@Z) 在函数“public: bool __clrcall Engine::InputManager::IsKeyDown(unsigned char)const ” (?IsKeyDown) 中引用@InputManager@Engine@@$$FQBM_NE@Z)

显然,这个问题与GetAsyncKeyState() 但我不确定 CLI 友好的实现需要什么不同。谁能指导我如何解决这个问题?该函数在我的非 CLI 环境中正常运行(并且已经运行了几个月)。我对 CLI 东西非常陌生,所以任何帮助都会很棒,而且任何帮助都不会太具体。

如果有帮助,我正在使用 Visual Studio 2010 并使用 /clr 参数(而不是 :pure:safe)进行编译。

I'm working on a fairly convoluted project in which I'm accessing classes written in plain-old-C++ from a project using C++/CLI. It's a Windows Forms GUI project that uses a lot of the same functions as its (non-CLI) C++ sister project.

In one of my classes that I'm trying to adjustment to work in both environments, I'm polling keypresses with this function:

inline bool IsKeyDown(unsigned char ch) const { 
    return (GetAsyncKeyState(ch) & (1u << 15)) != 0; 
}

I'm getting both Unresolved Token and Unresolved External Symbol errors on
"extern "C" short __stdcall GetAsyncKeyState(int)" (?GetAsyncKeyState@@$$J14YGFH@Z) referenced in function "public: bool __clrcall Engine::InputManager::IsKeyDown(unsigned char)const " (?IsKeyDown@InputManager@Engine@@$$FQBM_NE@Z)

Obviously, the issue is related to GetAsyncKeyState() but I'm not sure what needs to be different for a CLI-friendly implementation. Can anyone direct me how to fix this? The function works properly in my non-CLI environment (and has for months). I'm very new to this CLI stuff so any help would be wonderful and no help could be too-specific.

If it helps, I'm using Visual Studio 2010 and compiling with the /clr parameter (not :pure or :safe).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-12-16 22:53:34

MSDN 库<​​/a> 提供了任何需要包含哪些头文件和库的详细信息。特定功能。

这种情况中,您需要 windows.h (您必须已经拥有)和 user32.lib (可能缺少)。 添加

#pragma comment(lib, "user32.lib")

因此,在代码顶部 ,一切都应该很好。或者,您可以将 user32.lib 添加到项目属性页面中的库列表中,记住为每个配置(例如调试和发布)执行此操作。

The MSDN library provides the details of which header files and libraries need to be included for any particular function.

In this case, you need windows.h (which you must already have) and user32.lib (which is probably missing). So add

#pragma comment(lib, "user32.lib")

at the top of your code and all should be well. Alternatively, you could add user32.lib to the list of libraries in the project properties pages, remembering to do this for each configuration, e.g., debug as well as release.

梦晓ヶ微光ヅ倾城 2024-12-16 22:53:34

刚刚遇到同样的问题。解决方案是链接到具有相应符号的 .lib,在本例中为 user32.lib。

我通过使项目使用继承值解决了 Visual Studio 中的问题。在项目属性中选中此复选框,它应该可以修复它:
输入图片此处描述

Just got the same issue. The solution is to link against a .lib which has a respective symbol, which in this case is user32.lib.

I fixed a problem in Visual Studio by making a project use inherited values. Check this checkbox in your project properties and it should fix it:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文