CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2007
    Location
    Egypt
    Posts
    19

    Question How to marshal DWORD in C#?

    I have a quesion about DWORD.

    When I have looked on DWORD on MSDN:
    http://msdn.microsoft.com/en-us/libr...51(VS.85).aspx

    I found that it is "unsigned 32-bit integer," so I marshaled it as System.UInt32. In addition, I found it marshaled as System.UInt32 in many places.

    But, when I tried to PInvoke GetStdHandle() function:
    http://msdn.microsoft.com/en-us/libr...51(VS.85).aspx

    I found that it takes one of three values:
    A. -10: to retrieve input device handle
    B. -11: to retrieve output device handle
    C. -12: to retrieve error device handle

    Although, DWORD is "unsigned 32-bit integer" and it is marshaled System.UInt32, System.UInt32 does not support negative values, therefore, it cannot take any value of the three. So, I have marshaled it as System.Int32 instead.

    Now, the question is:
    Do I have a misconception about DWORD? And why we marshal DWORD to System.UInt32 and GetStdHandle() requires negative values?
    Mohammad Elsheimy
    Geming.Leader@Hotmail.com
    Geming.Leader@Yahoo.com

    Just Like a Magic the magic inside the machine
    http://JustLikeAMagic.Wordpress.com

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How to marshal DWORD in C#?

    To be CLS compliant you can't use unsigned types (except for byte).

    The correct CLS compliant way of marshalling DWORD is to int.

    When marshaling, the size of the type is important : not what the resultant outcome is with integer types. Because int & uint are both the same size (4 byes) you don't have a problem.

    With regards to negative values, look up how negative values are held in memory on google (e.g. here).

    -1 = 0xFFFFFFFF for instance.

    Marshaling to int is ok for DWORD unless dealing with values > 0x80000000. The top bit represents of the 32-bits in a DWORD signifies a negative value.

    If the value is > 0x80000000 and you need it not to be negative covert it to a long which is 64-bits by doing the following :

    Code:
    static long IntToLong(int value)
    {
        return ((long)value) & 0xFFFFFFFFL;
    }
    Darwen.
    Last edited by darwen; April 3rd, 2009 at 02:31 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    May 2007
    Location
    Egypt
    Posts
    19

    Re: How to marshal DWORD in C#?

    I am very thankful Darwen. The code segment was very brilliant.

    But I know that the size reserved for the variable is more important, and System.UInt32 is non-CLS-compliant. But the question here, why GetStdHandle() requires DWORD? I think it would be OK if it requires INT instead!
    Mohammad Elsheimy
    Geming.Leader@Hotmail.com
    Geming.Leader@Yahoo.com

    Just Like a Magic the magic inside the machine
    http://JustLikeAMagic.Wordpress.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured