When translating the Windows API (including data types) into P/Invoke, should I replace DWORD with int
or uint
?
It's normally unsigned, but I see people using int
everywhere instead (is it just because of the CLS warning? even the .NET Framework itself does this), and so I'm never really sure which one is the correct one to use.
A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal).
uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero).
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.
Well according to the MSDN DWORD
is an unsigned integer with a range of 0 to 4294967295.
So ideally you should replace it with uint
rather than int
.
However, as you have spotted uint
is non-CLS compliant so if your method is publicly visible you should use int
and do the conversion. The corollary to that is that if your method isn't used outside your assembly you should mark it as internal
rather than public
. Then you'll be able use a uint
.
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