Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

UuidCreate (rpcrt4)
 
.
Summary
Creates a new UUID
Summary
Create a new UUID

C# Signature:

[DllImport("rpcrt4.dll", SetLastError=true)]
static extern int UuidCreateSequential(out Guid guid);
[DllImport("rpcrt4.dll"]
static extern int UuidCreate(ref UUID id);

VB Signature:

Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer
Declare Function UuidCreate Lib "rpcrt4.dll" (ByRef id As UUID) As Integer

User-Defined Types:

None.

[StructLayout(LayoutKind.Sequential)]
private struct UUID
{
     public uint Data1;
     public ushort Data2;
     public ushort Data3;
     [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
     public byte[] Data4;
}

Notes:

Microsoft changed the UuidCreate function so it no longer uses the machine's MAC address as part of the UUID. Since CoCreateGuid calls UuidCreate to get its GUID, its output also changed. If you still like the GUIDs to be generated in sequential order (helpful for keeping a related group of GUIDs together in the system registry), you can use the UuidCreateSequential function.

private const int RPC_S_OK = 0;
private const int RPC_S_OUT_OF_MEMORY = 14;
private const int RPC_S_UUID_LOCAL_ONLY = 1824;

CoCreateGuid generates random-looking GUIDs like these:

Alternative Managed API:

System.Guid guid;
guid = System.Guid.NewGuid();
string stringGuid = guid.ToString();

Notes:

None.

92E60A8A-2A99-4F53-9A71-AC69BD7E4D75
BB88FD63-DAC2-4B15-8ADF-1D502E64B92F
28F8800C-C804-4F0F-B6F1-24BFC4D4EE80
EBD133A6-6CF3-4ADA-B723-A8177B70D268
B10A35C0-F012-4EC1-9D24-3CC91D2B7122

UuidCreateSequential generates sequential GUIDs like these:

19F287B4-8830-11D9-8BFC-000CF1ADC5B7
19F287B5-8830-11D9-8BFC-000CF1ADC5B7
19F287B6-8830-11D9-8BFC-000CF1ADC5B7
19F287B7-8830-11D9-8BFC-000CF1ADC5B7
19F287B8-8830-11D9-8BFC-000CF1ADC5B7

Here is a summary of the differences in the output of UuidCreateSequential:

  • The last six bytes reveal your MAC address
  • Several GUIDs generated in a row are sequential

Tips & Tricks:

Please add some!

Sample Code in C#:

static Guid UuidCreateSequential()

Sample Code:

UUID id = new UUID();
rc = UuidCreate(ref id);
if (rc != RPC_S_OK && rc != RPC_S_UUID_LOCAL_ONLY)
{
   const int RPC_S_OK = 0;
   Guid g;
   int hr = UuidCreateSequential(out g);
   if (hr != RPC_S_OK)
     throw new ApplicationException
       ("UuidCreateSequential failed: " + hr);
   return g;
     // Handle error
}

Typically, the UUID is required as a string. Here is how:

Sample Code in VB:

Sub Main()
   Dim myId As Guid
   Dim code As Integer
   code = UuidCreateSequential(myId)
   If code <> 0 Then
     Console.WriteLine("UuidCreateSequential failed: {0}", code)
   Else
     Console.WriteLine(myId)
   End If
End Sub
[DllImport("rpcrt4.dll")]
private static extern int UuidToString(ref UUID uuid, ref System.IntPtr str);

[DllImport("rpcrt4.dll")]
private static extern int RpcStringFree(ref System.IntPtr str);

public static string generateUUID()
{
     int rc;

Alternative Managed API:

Do you know one? Please contribute it!

     UUID id = new UUID();
     rc = UuidCreate(ref id);
     if (rc != RPC_S_OK && rc != RPC_S_UUID_LOCAL_ONLY)
     {
     throw <cannot create UUID exception>;
     }

Documentation

     System.IntPtr str = new System.IntPtr();
     rc = UuidToString(ref id, ref str);
     switch(rc)
     {
     case RPC_S_OK:
     {
         break;
     }
     case RPC_S_OUT_OF_MEMORY:
     {
         throw <out of memory excepetion>;
     }
     default:
     {
         throw <UuidToString failed exception>;
     }
     }

     string result;
     try
     {
     result = Marshal.PtrToStringAnsi(str);
     }
     catch(System.Exception ex)
     {
     throw <PtrToStringAnsi failed exception>;
     }
     if ((rc = RpcStringFree(ref str)) != RPC_S_OK)
     {
     throw <RpcStringFree failed exception>;
     }
     return result;
}

Documentation
UuidCreate @msdn on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions