有关 WinAPI 包装器的帮助

发布于 2024-12-03 02:49:27 字数 291 浏览 2 评论 0原文

我一直在制作一个简单的 WinAPI 包装器,但遇到了一个相当大的问题。在代码中的某个位置,Window 类的句柄 (HWND) 设置为 NULL,并且需要它来处理消息。我已经检查代码一个小时了,但我找不到任何东西。有人可以帮忙吗?

Git 存储库

我很确定逻辑错误发生在声明...\Window.hpp 和声明中的某个地方...\Application.hpp,因为这些是唯一保存正在调用的代码的文件。

I've been making a simple WinAPI wrapper, and I've run into a pretty big problem. Somewhere in the code, the Window class's handle (HWND) is set to NULL, and it's required to process the messages. I've been looking over the code for an hour now, and I can't find anything. Can anyone help?

Git repository

I'm pretty sure the logic error happens somewhere in Declarations...\Window.hpp and Declarations...\Application.hpp, because those are the only files that hold code that is being called.

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-12-10 02:49:27

让我们从这个开始:

namespace WinAPI{
    #include <windows.h>
    #include <commctrl.h>
}

不要这样做。

它可以工作,但代价是可能必须为每个新的 SDK 版本更新代码,但这绝对不适合初学者。

下面介绍了如何在 C++ 中包含 标头:

#undef UNICODE
#define UNICODE
#undef STRICT
#define STRICT
#undef NOMINMAX
#define NOMINMAX

#include <windows.h>
#include <windowsx.h>

将其放入包装器标头中。

将包装器包含在全局命名空间中。

哦,您的问题,导致您的 HWND 值消失的逻辑错误?您不能真正指望其他人调试您的代码。

干杯&呵呵,,

Let's start with this:

namespace WinAPI{
    #include <windows.h>
    #include <commctrl.h>
}

Don't do that.

It can be made to work, at the cost of possibly having to update your code for every new SDK release, but it's definitely not something for a beginner.

Here's how to include the <windows.h> header in C++:

#undef UNICODE
#define UNICODE
#undef STRICT
#define STRICT
#undef NOMINMAX
#define NOMINMAX

#include <windows.h>
#include <windowsx.h>

Place that in a wrapper header.

Include the wrapper in the global namespace.

Oh, the question you asked about, the logic error that made your HWND value disappear? You can't really expect others to debug your code.

Cheers & hth.,

疯了 2024-12-10 02:49:27

如果您还不熟悉 assert(),现在可能是了解它的好时机。断言是您认为正确且希望调试器检查的表达式。在您的情况下,您可以查看更新 HWND 的所有位置,并编写 assert(newHWNDvalue != NULL);

断言是评论的一种形式。与 // 不同,编译器会编译它们,调试器会检查它们,因此它们不会随着时间的推移而变得陈旧。

assert 是一个宏,您需要包含 标头。在发布版本中,断言表达式不会被编译,因此没有开销。这意味着您通常可以负担得起重要的支票。对于复杂的类,添加 private: bool CheckInvariants() const 方法可能很有用,这样您就可以在其他成员中 assert(CheckInvariants());

If you're not already familiar with assert(), this may be a good time to learn about it. An assertion is an expression that you believe is true, and which you'd like the debugger to check. In your case, you could look at all the places which update the HWND, and write assert(newHWNDvalue != NULL);.

Assertions are a form of comment. Unlike //, the compiler compiles them and the debugger does check them, and therefore they don't go stale over time.

assert is a macro, and you'll need to include the <cassert> header. In release builds, the asserted expression is not compiled, so there's no overhead. That means that you can usually afford non-trivial checks. For complex classes, it may be useful to add a private: bool CheckInvariants() const method, so you can assert(CheckInvariants()); in other members.

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