我可以#include定义DWORD的最小Windows标头是什么? [英] What is the smallest Windows header I can #include to define DWORD?

查看:230
本文介绍了我可以#include定义DWORD的最小Windows标头是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自己的小头文件,其中声明了几个函数,其中一个函数的返回类型为DWORD.我不愿意为了得到这种类型的正式定义而拖入windows.h,因为该文件很大,并且我的标头将用在许多不需要它的源模块中.

I have a small header file of my own which declares a couple of functions, one of which has a return type of DWORD. I'm reluctant to drag in windows.h just to get the official definition of this type since that file is huge, and my header will be used in a number of source modules that don't otherwise need it.

当然,在实践中我知道DWORD只是unsigned int,但我更喜欢更卫生的方法,如果可能的话,包括一个官方头文件.

Of course, in practice I know that DWORD is just unsigned int, but I'd prefer the more hygienic approach of including an official header file if possible.

此页面上,它表示DWORD是在windef.h中定义的,但是不幸的是,仅包含这个小文件会直接导致编译错误-显然,它希望包含在其他标头中. (此外,我的文件是头文件这一事实也意味着我不能只声明WIN32_LEAN_AND_MEAN,因为#includes我的文件的源文件可能需要将其保留为未定义状态.)

On this page it says that DWORD is defined in windef.h, but unfortunately including just this small file directly leads to compilation errors -- apparently it expects to be included by other headers. (Also, the fact that my file is a header file also means I can't just declare WIN32_LEAN_AND_MEAN, since the source file that #includes my file might need this to be left undefined.)

有什么想法吗?我知道这不是世界末日-我可以继续#include <windows.h>-但认为有人可能有更好的主意!

Any ideas? I know it's not the end of the world -- I can just continue to #include <windows.h> -- but thought someone might have a better idea!

感谢您的回复.对于建议使用其他类型的用户,让我解释一下为什么在这种情况下不希望这样做:我在不同的源文件中设置了两个函数的特定于平台的不同版本,并要求CMake配置检测当前平台然后选择要建造的那个.在Windows上,我的功能如下:

Thanks for your responses. To those who suggested using a different type, let me explain why that's not desirable in this case: I've set up different, platform-specific versions of the two functions in different source files, and ask the CMake configuration to detect the current platform and choose which one to build. On Windows, my functions look like:

typedef DWORD TimePoint;
TimePoint GetTimeNow(void);
double TimeDifference(TimePoint start, TimePoint end);

Windows版本的GetTimeNow()仅调用Windows API timeGetTime(),其返回类型为DWORD,因此它必须具有相同的返回类型. (在其他平台上,TimePoint将具有不同的类型,例如在UNIXy平台上为struct timeval.)实际上,类型TimePoint的值是不透明的,您唯一可以做的就是将它们中的两个传递给TimeDifference()测量它们之间经过的时间(以秒为单位).这样可以进行跨平台开发.不幸的是,这仍然意味着客户端代码必须知道TimePoint的具体类型.

The Windows version of GetTimeNow() just calls the Windows API timeGetTime(), which has return type DWORD, and so it must have the same return type. (On other platforms, TimePoint will have a different type, e.g. struct timeval on UNIXy platforms.) In effect, values of type TimePoint are opaque, and the only thing you can do with them is pass two of them to TimeDifference() to measure the elapsed time between them in seconds. This enables cross-platform development. Unfortunately it still means that client code has to know the concrete type of TimePoint.

推荐答案

我相信您曾经能够包含winbase.h,但现在似乎不再如此了.我所见过的所有资源都推荐使用WIN32_LEAN_AND_MEAN选项的windows.h.正如您所指出的,后一种优化对您没有帮助.

I believe you used to be able to include winbase.h, but that doesn't seem to be the case anymore. All of the sources I've seen recommend windows.h, with the option of WIN32_LEAN_AND_MEAN. As you've indicated, the latter optimization doesn't help you.

您可以做这样的事情.

#ifndef _WINDEF_
typedef unsigned long DWORD;
#endif

不干净,但是高效.此typedef不可能更改.

Not clean, but efficient. This typedef isn't likely to ever change.

这篇关于我可以#include定义DWORD的最小Windows标头是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆