转发声明 HINSTANCE 和好友

发布于 2024-08-27 14:40:55 字数 495 浏览 4 评论 0原文

有没有办法从 WinAPI 前向声明 HINSTANCE 类型,而不包含完整的(和大的)windows.h 标头?

例如,如果我有一个 RenderWindow 类,它拥有一个 HINSTANCE mInstance,我必须在 RenderWindow 中包含 windows.h。 h.因此,需要 RenderWindow 的所有内容也必须包含 windows.h

我尝试包含 windef.h 但这似乎需要 windows.h 中的一些内容。 :-( 如果我无法转发声明它,是否至少有一种可移植的方法可以在 RenderWindow 中使用 long mInstance 而不是 HINSTANCE

Is there a way to forward-declare the HINSTANCE type from the WinAPI without including the full (and big) windows.h header?

For example, if I have a class RenderWindow which owns an HINSTANCE mInstance, i will have to include windows.h in RenderWindow.h. So everything that needs RenderWindow also has to include windows.h.

I tried including windef.h but this seems to need some things from windows.h. :-( If I can't forward declare it, is there at least a portable way to use something like long mInstance in RenderWindow instead of HINSTANCE?

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

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

发布评论

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

评论(5

初与友歌 2024-09-03 14:40:56

HINSTANCE 在 WinDef.h 中声明为 typedef HINSTANCE__* HINSTANCE;

您可以在标头中写入:

#ifndef _WINDEF_
class HINSTANCE__; // Forward or never
typedef HINSTANCE__* HINSTANCE;
#endif

当未包含 WinDef.h 时,引用 HINSTANCE 时将出现编译错误。

HINSTANCE is declared in WinDef.h as typedef HINSTANCE__* HINSTANCE;

You may write in your headers:

#ifndef _WINDEF_
class HINSTANCE__; // Forward or never
typedef HINSTANCE__* HINSTANCE;
#endif

You will get compilation errors referencing a HINSTANCE when WinDef.h is not included.

祁梦 2024-09-03 14:40:56

您可以将其声明为 void* 并消除错误。但这几乎是一场永无休止的战斗,迟早你会被绊倒。使用预编译标头,这样您就不必关心 windows.h stdafx.h 的大小

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

You could declare it void* and cast the errors away. This is close to a never-ending battle though, sooner or later you'll get tripped up. Use pre-compiled headers so you don't care about the size of windows.h

stdafx.h:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
金兰素衣 2024-09-03 14:40:56

例如,如果我有一个 RenderWindow 类,它拥有一个 HINSTANCE mInstance,我必须在 中包含 windows.h >RenderWindow.h。因此,需要 RenderWindow 的所有内容也必须包含 windows.h

您看过Pimpl 习语吗?这允许您隐藏私人成员。副作用是您不必将它们的标头包含在类的标头中。

For example, if I have a class RenderWindow which owns an HINSTANCE mInstance, i will have to include windows.h in RenderWindow.h. So everything that needs RenderWindow also has to include windows.h.

Have you looked at the Pimpl idiom? This allows you to hide private members. A side-effect is that you don't have to include their headers in your class' header.

柏拉图鍀咏恒 2024-09-03 14:40:56

嘿@NoSenseEtAl 我想我们还在那里。

2021 年,HINSTANCE 中定义。直接包含 会出现错误:“No Target Architecture”

要解决该错误,请执行以下操作(假设针对 x64 进行构建):

#define _AMD64_
#include <minwindef.h>

int main() {
    HINSTANCE h;
}

请注意宏 _AMD64_ 没有记录,它以下划线开头,所以不能由用户定义。

而且它仅由 定义,因此没有更小的标头可以包含它进行定义。

显然,人们更希望 Windows SDK 能够与模块一起正常工作,因此可以通过模块来修复构建速度。

Hey @NoSenseEtAl I guess we are still there.

In 2021, HINSTANCE is defined in <minwindef.h>. Including directly <minwindef.h> gives the error: "No Target Architecture"

To work around the error, do the following (assuming building for x64):

#define _AMD64_
#include <minwindef.h>

int main() {
    HINSTANCE h;
}

Note that the macro _AMD64_ is not documented, it starts with underscore, so not to be defined by user.

And it is defined only by <Windows.h>, so there's no smaller header to include make it defined.

Apparently there's more hope that Windows SDK will work fine with Modules, so can fix the build speed by Modules instead.

夏见 2024-09-03 14:40:56

处理不包含标头的句柄的最佳可移植方法是将它们reinterpret_cast转换为具有完全相同大小的类型。

大多数句柄的指针大小为1。所以 void*uintptr_t 就可以了。示例:

  • _beginthreadex 返回 uintptr_t 而不是 HANDLE 到线程。
  • MSVC thread::native_handle 返回 void*

请务必在您看到类型的位置使用 static_assert 获取类型大小。

1 很少有句柄没有指针大小,我只能回忆起 AcquireCredentialsHandle 返回的句柄。

The best portable way to deal with handles without header including is reinterpret_casting them to a type with exactly the same size.

Most handles have pointer size1. So void* or uintptr_t will do. Examples:

  • _beginthreadex returns uintptr_t instead of HANDLE to a thread.
  • MSVC thread::native_handle returns void*

Be sure to static_assert for type sizes in a place where you see the type.

1 Few handles don't have pointer size, I can only recall the one returned by AcquireCredentialsHandle.

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