有关 WinAPI 包装器的帮助
我一直在制作一个简单的 WinAPI 包装器,但遇到了一个相当大的问题。在代码中的某个位置,Window 类的句柄 (HWND) 设置为 NULL,并且需要它来处理消息。我已经检查代码一个小时了,但我找不到任何东西。有人可以帮忙吗?
我很确定逻辑错误发生在声明...\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?
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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们从这个开始:
不要这样做。
它可以工作,但代价是可能必须为每个新的 SDK 版本更新代码,但这绝对不适合初学者。
下面介绍了如何在 C++ 中包含
标头:将其放入包装器标头中。
将包装器包含在全局命名空间中。
哦,您问的问题,导致您的 HWND 值消失的逻辑错误?您不能真正指望其他人调试您的代码。
干杯&呵呵,,
Let's start with this:
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++: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.,
如果您还不熟悉
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 theHWND
, and writeassert(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 aprivate: bool CheckInvariants() const
method, so you canassert(CheckInvariants());
in other members.