windows 我可以#include 定义DWORD 的最小Windows 标头是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2473334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:09:08  来源:igfitidea点击:

What is the smallest Windows header I can #include to define DWORD?

cwindowsheader

提问by j_random_hacker

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.hjust 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. 我不愿意拖进来windows.h只是为了获得这种类型的官方定义,因为该文件很大,而且我的标题将用于许多不需要它的源模块中。

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

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

On this pageit says that DWORDis 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.)

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

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!

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

[EDIT]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:

[编辑]感谢您的回复。对于那些建议使用不同类型的人,让我解释一下为什么在这种情况下这是不可取的:我在不同的源文件中设置了两个函数的不同平台特定版本,并要求 CMake 配置检测当前平台并选择构建哪一个。在 Windows 上,我的函数如下所示:

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

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, TimePointwill have a different type, e.g. struct timevalon UNIXy platforms.) In effect, values of type TimePointare 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.

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

回答by Todd

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.

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

You could do something like this.

你可以做这样的事情。

#ifndef _WINDEF_
typedef unsigned long DWORD;
#endif

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

不干净,但高效。这个 typedef 不太可能改变。

回答by Aram H?v?rneanu

Include Windows.h and use precompiled headers. Btw, you can define WIN32_LEAN_AND_MEAN and then undef it later!

包括 Windows.h 并使用预编译的头文件。顺便说一句,您可以先定义 WIN32_LEAN_AND_MEAN,然后再取消定义!

回答by tomlogic

A DWORDis always going to be a 32-bit unsigned int, so it doesn't really matter whether you use DWORDor unsigned longor uint32_t. If all three types refer to a 32-bit unsigned int, the compiler is going to consider them equivalent.

ADWORD总是一个 32 位的无符号整数,所以你是否使用DWORDorunsigned longuint32_t. 如果所有三种类型都引用 32 位 unsigned int,编译器将认为它们是等效的。

Since this is part of the platform-specific files, I don't think you need to worry about portability so much. Heck, dig into the headers to find the native type of a DWORDand just put that typedef in your header. C compilers accept duplicate typedefs as long as they have the same underlying type.

由于这是特定于平台的文件的一部分,因此我认为您不必太担心可移植性。哎呀,深入研究标题以找到 a 的本机类型,DWORD然后将该 typedef 放入您的标题中。C 编译器接受重复的 typedef,只要它们具有相同的基础类型。

回答by joexxx

Use this file:include <IntSafe.h>

Use this file:include <IntSafe.h>

回答by Javier

I'd say to just define it yourself. That way, it's more platform-independent (of course, I don't know if the rest of the code requires Windows). If you don't want to do that, use precompiled headers.

我会说你自己定义它。这样,它更独立于平台(当然,我不知道其余代码是否需要Windows)。如果您不想这样做,请使用预编译的头文件。

回答by wallyk

Why don't you instead define the function to return int? This is a highly portable type and completely divorces your code from the evil empireMicrosoft.

你为什么不定义返回的函数int?这是一种高度可移植的类型,将您的代码与邪恶的Microsoft帝国完全分离。

回答by Tuomas Pelkonen

Don't use a DWORD. I have seen too much Windows code that have been ported to other platforms later. Those DWORDs become a real problem when everybody has their own definition for it. I don't think there are any good reasons to use windows specific types in interfaces.

不要使用 DWORD。我见过太多后来移植到其他平台的 Windows 代码。当每个人都有自己的定义时,这些 DWORD 就成了一个真正的问题。我认为在界面中使用 Windows 特定类型没有任何充分的理由。

Even if your code will never be ported to any other platform, I still think code should use the native types or your own types (e.g., MyInt32, MyUInt64, etc), but nothing from windows.h.

即使您的代码永远不会移植到任何其他平台,我仍然认为代码应该使用本机类型或您自己的类型(例如,MyInt32、MyUInt64 等),而不是 windows.h 中的任何内容。

回答by Windows programmer

If you're worried that when your cross-platform program runs on Windows it will load too many Windows DLLs just because your source code had #include <windows.h>, I think you're worrying too much. Even Notepad has to load half of the known universe, and it has been known to load and execute on occasion.

如果你担心当你的跨平台程序在 Windows 上运行时它会加载太多的 Windows DLL,因为你的源代码有 #include <windows.h>,我认为你担心太多了。甚至记事本也必须加载已知宇宙的一半,而且它有时会加载和执行。

If you're worried that when other developers use Windows your cross-platform .h file will put a ton of namespace pollution in their compilation environments, I think you're worrying too much. 99.999% of Windows projects already did an #include <windows.h> before they got to your .h file.

如果您担心当其他开发人员使用 Windows 时,您的跨平台 .h 文件会在他们的编译环境中造成大量命名空间污染,那么我认为您太担心了。99.999% 的 Windows 项目在到达您的 .h 文件之前已经执行了 #include <windows.h>。

回答by antak

Is there a <wtypes.h> where you're at? Because in it, I see:

你所在的位置有 <wtypes.h> 吗?因为在里面,我看到:

#ifndef _DWORD_DEFINED
#define _DWORD_DEFINED
typedef unsigned long DWORD;

#endif // !_DWORD_DEFINED

This file is located under "...\VC98\INCLUDE" here.. which is for VC6 so I'd figured it'll be in later versions.

该文件位于此处的“...\VC98\INCLUDE”下。这是用于 VC6 的,所以我认为它会出现在更高版本中。

I was after the same thing as the OP and solved it by including the said header.

我和 OP 一样,并通过包含上述标题来解决它。

回答by Abhishek Jain

How about - #include <minwindef.h>?

怎么样—— #include <minwindef.h>