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.
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.
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.)
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!
[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:
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, 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
.
Search for and locate the windows. h header file in the C:\Program Files\Microsoft SDKs directory.
h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems.
SDKDDKVer. h is the header file that actually defines the #defines that represent each version of Windows, IE, etc. Follow this answer to receive notifications.
A dword, which is short for "double word," is a data type definition that is specific to Microsoft Windows. When defined in the file windows. h, a dword is an unsigned, 32-bit unit of data. It can contain an integer value in the range 0 through 4,294,967,295.
A DWORD
is always going to be a 32-bit unsigned int, so it doesn't really matter whether you use DWORD
or unsigned long
or uint32_t
. If all three types refer to a 32-bit unsigned int, the compiler is going to consider them equivalent.
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 DWORD
and just put that typedef in your header. C compilers accept duplicate typedefs as long as they have the same underlying type.
Include Windows.h and use precompiled headers. Btw, you can define WIN32_LEAN_AND_MEAN and then undef it later!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With