Oct18Written by:Pierre Marmignon
10/18/2009 9:55 PM

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 myVarAs 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: