What's the equivalent of WORD in C#?
Asked Answered
C

1

20

I'm trying to access an unmanaged library and am lucky to have access to a comprehensive guide to the API.

Unfortunately, I've no idea what the C# equivalent of C++'s WORD type is. Similarly, I've no idea what DWORD would be.

Carmelcarmela answered 30/3, 2011 at 18:16 Comment(1)
Apparently the guide isn't that comprehensive ;)Gorden
M
40

You're probably looking for ushort for WORD and uint for DWORD.

Mors answered 30/3, 2011 at 18:18 Comment(7)
Note that this is correct only because a C# application is (extremely) highly likely to be running on a machine for which those are the widths of the C++ data types. The C++ types (in contrast to the C# types) are not universally the same. Exceptions to this would be so unlikely, though, that this comment is really quite pointless. Sorry.Nunez
and don't forget ulong for QWORD, although the OP didn't ask about those :PGumbo
See figure 2 in Calling Win32 DLLs in C# with P/Invoke for some common types.Cleavage
@Jeffrey I don't think your comment is that pointless. What if I run Mono on a non-32-bit platform?Turco
You're absolutely right, which is why I said "probably" in my response. I usually always add that caveat for completeness, but the likelihood of someone asking a question and being on a platform with non-standard type widths is so low that I should just leave it out.Mors
@Jeffrey: the Windows API defines WORD and DWORD in terms of bit sizes and signs, not in terms of C/C++ types, so there should be no problem. That is, unless the types in question are not the WinAPI types...Gorden
I would encourage you, when doing any kind of interop or using data from other sources, to use the .NET defined types like UInt16, UInt32, and UInt64 rather than the C# aliases. You'll save yourself a lot of confusion.Fluster

© 2022 - 2024 — McMap. All rights reserved.