Archive

Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Monthly
Go

Search

Pierre's Blog

.Net : Write Large Unsigned Integer in a Registry DWORD Value

Oct 18

Written by:
10/18/2009 9:55 PM  RssIcon

Working on a tool I had to face the specific case of writing large integers into a REG_DWORD value.

The problem is that basically you cannot write anything superior to the Integer.MaxValue() object in a DWORD value using the .Net Managed APIs Registry.SetValue(), which can be problematic (in my case that was for XenApp DefaultPRNFlags and I really need to be able to write these settings down).

To overcome this issue I've done some researches and did not find many documentation on the subject, that's why I've decided to publish this little blog post.

Assuming 'sdm' is the Int64 value received by my function, here is the code snippet allowing to convert it to then write it properly to a REG_DWORD value ;

VB.Net


        Dim myVar As Integer
        If CLng(sdm) > Integer.MaxValue Then
            myVar = (CType(-(UInt32.MaxValue - CLng(sdm)), Integer) - 1)
        Else
            myVar = CInt(sdm)
        End If

C#

         int myVar;
         if ((long)sdm > int.MaxValue) {
               myVar = ((int)-(UInt32.MaxValue - (long)sdm) - 1);
         }
         else {
               myVar = (int)sdm;
         }
 

I hope this helps

Tags:
Categories: