转发声明 HINSTANCE 和好友
有没有办法从 WinAPI 前向声明 HINSTANCE
类型,而不包含完整的(和大的)windows.h
标头?
例如,如果我有一个 RenderWindow
类,它拥有一个 HINSTANCE mInstance
,我必须在 RenderWindow 中包含
.因此,需要 windows.h
。 hRenderWindow
的所有内容也必须包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HINSTANCE 在 WinDef.h 中声明为 typedef HINSTANCE__* HINSTANCE;
您可以在标头中写入:
当未包含 WinDef.h 时,引用 HINSTANCE 时将出现编译错误。
HINSTANCE is declared in WinDef.h as typedef HINSTANCE__* HINSTANCE;
You may write in your headers:
You will get compilation errors referencing a HINSTANCE when WinDef.h is not included.
您可以将其声明为 void* 并消除错误。但这几乎是一场永无休止的战斗,迟早你会被绊倒。使用预编译标头,这样您就不必关心 windows.h stdafx.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:
您看过Pimpl 习语吗?这允许您隐藏私人成员。副作用是您不必将它们的标头包含在类的标头中。
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.
嘿@NoSenseEtAl 我想我们还在那里。
2021 年,
HINSTANCE
在
中定义。直接包含
会出现错误:“No Target Architecture”要解决该错误,请执行以下操作(假设针对 x64 进行构建):
请注意宏
_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):
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.
处理不包含标头的句柄的最佳可移植方法是将它们
reinterpret_cast
转换为具有完全相同大小的类型。大多数句柄的指针大小为1。所以
void*
或uintptr_t
就可以了。示例:_beginthreadex
返回uintptr_t
而不是HANDLE
到线程。thread::native_handle
返回void*
请务必在您看到类型的位置使用
static_assert
获取类型大小。1 很少有句柄没有指针大小,我只能回忆起
AcquireCredentialsHandle
返回的句柄。The best portable way to deal with handles without header including is
reinterpret_cast
ing them to a type with exactly the same size.Most handles have pointer size1. So
void*
oruintptr_t
will do. Examples:_beginthreadex
returnsuintptr_t
instead ofHANDLE
to a thread.thread::native_handle
returnsvoid*
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
.