Creating a GUID in WinCE

  • Thread starter Thread starter Trey
  • Start date Start date
Not to my knowledge, but here is a class [granted its C#] that can create
new GUIDs...

public class RandomGen
{
// all static methods
private RandomGen()
{
}

#region Public Methods
public static Guid NewGuid()
{
IntPtr hCryptProv = IntPtr.Zero;
Guid guidNew = Guid.Empty;

try
{
// holds random bits for guid
byte[] bytesGuidLength = new byte[Const.GuidByteArraySize];

// get crypto provider handle
if (!WinAPI.CryptAcquireContext(ref hCryptProv, null, null,
WinAPI.PROV_RSA_FULL, WinAPI.CRYPT_VERIFYCONTEXT))
throw new SystemException("Failed to acquire cryptography handle.");

// generate a 128 bit (16 byte) cryptographically random number
if (!WinAPI.CryptGenRandom(hCryptProv, bytesGuidLength.Length,
bytesGuidLength))
throw new SystemException("Failed to generate cryptography random
bytes.");

// set the variant
bytesGuidLength[Const.VariantByte] &= Const.VariantByteMask;
bytesGuidLength[Const.VariantByte] |= ((int)GuidVariant.Standard <<
Const.VariantByteShift);

// set the version
bytesGuidLength[Const.VersionByte] &= Const.VersionByteMask;
bytesGuidLength[Const.VersionByte] |= ((int)GuidVersion.Random <<
Const.VersionByteShift);

// create the new System.Guid object
guidNew = new Guid(bytesGuidLength);
}
finally
{
// release the crypto provider handle
if (hCryptProv != IntPtr.Zero)
WinAPI.CryptReleaseContext(hCryptProv, 0);
}

return guidNew;
}

public static int randInt32(int nBegin, int nEnd)
{
IntPtr hCryptProv = IntPtr.Zero;

int nValue = 0;

try
{
if (nBegin > nEnd)
throw new SystemException("Beginning value greater than ending
value.");

int nLength = 1;

// holds random bits for guid
byte[] bytesGuidLength = new byte[nLength];

// get crypto provider handle
if (!WinAPI.CryptAcquireContext(ref hCryptProv, null, null,
WinAPI.PROV_RSA_FULL, WinAPI.CRYPT_VERIFYCONTEXT))
throw new SystemException("Failed to acquire cryptography handle.");

// generate a 128 bit (16 byte) cryptographically random number
if (!WinAPI.CryptGenRandom(hCryptProv, bytesGuidLength.Length,
bytesGuidLength))
throw new SystemException("Failed to generate cryptography random
bytes.");

double dSeed = Convert.ToDouble(bytesGuidLength[0]) / 256;

int nDiff = nEnd - nBegin + 1;
nValue = (int)Math.Ceiling(nDiff * dSeed) + nBegin - 1;
}
finally
{
// release the crypto provider handle
if (hCryptProv != IntPtr.Zero)
WinAPI.CryptReleaseContext(hCryptProv, 0);
}

return nValue;
}
#endregion

#region Private Properties
// guid variant types
private enum GuidVariant
{
ReservedNCS = 0x00,
Standard = 0x02,
ReservedMicrosoft = 0x06,
ReservedFuture = 0x07
}

// guid version types
private enum GuidVersion
{
TimeBased = 0x01,
Reserved = 0x02,
NameBased = 0x03,
Random = 0x04
}
#endregion

#region Const
// constants that are used in the class
private class Const
{
// number of bytes in guid
public const int GuidByteArraySize = 16;
public const int IntByteArraySize = 10;

// multiplex variant info
public const int VariantByte = 8;
public const int VariantByteMask = 0x3f;
public const int VariantByteShift = 6;

// multiplex version info
public const int VersionByte = 7;
public const int VersionByteMask = 0x0f;
public const int VersionByteShift = 4;
}
#endregion

#region WinAPI
// imports for the crypto api functions
private class WinAPI
{
public const uint PROV_RSA_FULL = 1;
public const uint CRYPT_VERIFYCONTEXT = 0xf0000000;

[DllImport("coredll.dll")]
public static extern bool CryptAcquireContext(ref IntPtr phProv, string
pszContainer, string pszProvider, uint dwProvType, uint dwFlags);

[DllImport("coredll.dll")]
public static extern bool CryptReleaseContext(IntPtr hProv, uint
dwFlags);

[DllImport("coredll.dll")]
public static extern bool CryptGenRandom(IntPtr hProv, int dwLen, byte[]
pbBuffer);
}
#endregion
}
 
Back
Top