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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Tom Wright
  • 11,278
  • 15
  • 74
  • 148

1 Answers1

40

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

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
yan
  • 20,644
  • 3
  • 38
  • 48
  • 9
    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. – Jeffrey L Whitledge Mar 30 '11 at 18:26
  • 3
    and don't forget `ulong` for `QWORD`, although the OP didn't ask about those :P – Edurne Pascual Mar 30 '11 at 18:27
  • 3
    See figure 2 in [Calling Win32 DLLs in C# with P/Invoke](http://msdn.microsoft.com/en-us/magazine/cc164123.aspx#S5) for some common types. – Randy Levy Mar 30 '11 at 18:28
  • 1
    @Jeffrey I don't think your comment is that pointless. What if I run Mono on a non-32-bit platform? – Federico klez Culloca Mar 30 '11 at 18:28
  • 1
    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. – yan Mar 30 '11 at 18:28
  • 4
    @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... – R. Martinho Fernandes Mar 30 '11 at 18:31
  • 10
    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. – Jim Mischel Mar 30 '11 at 18:56