154

I am creating an .idl file programmatically. How do I create UUIDs for the interfaces and Methods Programmatically.

Can I generate the UUID programmatically?

RAM
  • 1,483
  • 1
  • 10
  • 27
Uma Shankar Subramani
  • 1,637
  • 2
  • 11
  • 12

5 Answers5

265

You are probably looking for System.Guid.NewGuid().

Tudor
  • 58,972
  • 12
  • 89
  • 138
  • 35
    You can also do String UUID = Guid.NewGuid().ToString() – Justin Dec 12 '11 at 17:41
  • 12
    GUID and UUID are all the same? – Uma Shankar Subramani Dec 12 '11 at 17:48
  • 19
    @Uma Shankar Subramani: GUID = Globally Unique Identifier, UUID = Universally Unique Identifier. Different words for the same concept. – Tudor Dec 12 '11 at 17:58
  • 1
    And you need formatting of the GUID as a string that is different from the default, you can use the [`ToString(string format)`](http://msdn.microsoft.com/en-us/library/97af8hh4.aspx) overload that accepts one of several format specifiers. – Michiel van Oosterhout Jun 03 '13 at 15:44
  • 7
    You will probably want to do `System.Guid.NewGuid().ToString("B").ToUpper()` if you want to be compatible with some MS Build tools that can't understand lower case UUIDs. For example, `vdproj` setup projects have UUIDs in upper case and will throw an exception it you give it lower case. – Mark Lakata Jun 19 '13 at 21:27
  • 1
    It's not the same thing. it's the same representation, yet not the same scheme. Guid can be a random representation of numbers and digits while the UUID contains information such as data / time and mac address. https://en.wikipedia.org/wiki/Universally_unique_identifier – Idan Feb 03 '16 at 11:51
50

Be careful: while the string representations for .NET Guid and (RFC4122) UUID are identical, the storage format is not. .NET trades in little-endian bytes for the first three Guid parts.

If you are transmitting the bytes (for example, as base64), you can't just use Guid.ToByteArray() and encode it. You'll need to Array.Reverse the first three parts (Data1-3).

I do it this way:

var rfc4122bytes = Convert.FromBase64String("aguidthatIgotonthewire==");
Array.Reverse(rfc4122bytes,0,4);
Array.Reverse(rfc4122bytes,4,2);
Array.Reverse(rfc4122bytes,6,2);
var guid = new Guid(rfc4122bytes);

See this answer for the specific .NET implementation details.

Edit: Thanks to Jeff Walker, Code Ranger, for pointing out that the internals are not relevant to the format of the byte array that goes in and out of the byte-array constructor and ToByteArray().

Community
  • 1
  • 1
Ben Mosher
  • 12,378
  • 6
  • 63
  • 78
  • Note: I realize the OP likely meant `Guid` (since it's intended for a .idl), but I just ran into this. So here you go, Bingers and Googlers. – Ben Mosher May 23 '13 at 20:08
  • 1
    I can't test this, but are you sure you should check the BitConverter.IsLittleEndian rather than just always reversing. The docs for for Guid.ToByteArray() call out the byte order as little endian, and say the constructor matches. The spec for GUID is little endian. I would think that should be independent of the machine byte order. – Jeff Walker Code Ranger Sep 14 '13 at 18:29
  • @JeffWalkerCodeRanger: from Eric Lippert, ages ago: http://blogs.msdn.com/b/ericlippert/archive/2004/05/25/141525.aspx – Ben Mosher Sep 16 '13 at 17:25
  • I don't see how the Eric Lippert link answers the question. Looking at the Mono code at https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Guid.cs it looks to me that they are always assuming the bytes are in little endian order regardless of the native endianness. That fits with my understanding that if it depended on the platform, it wouldn't match the semantic of the MS api or the spec. Looking at disassembled mscorelib, it seems to assume the bytes in the array are in little endian order too. – Jeff Walker Code Ranger Sep 16 '13 at 23:25
  • Looks like you're right. While the internal storage format is endianness-sensitive, the constructor and `ToByteArray` are not; they're always lil'-endian. Edit incoming. – Ben Mosher Sep 17 '13 at 17:31
  • @JeffWalkerCodeRanger BitConverter.IsLittleEndian returning the same thing all the time is a "implementation detail" if you are running your app on a .NET platform that is not mono nor the Microsoft's .NET CLR, or at some random point in the future when one of those two decide to change it it may not always return the same. – Scott Chamberlain Aug 10 '14 at 01:20
  • @ScottChamberlain I was not implying that BitConverter.IsLittleEndian always returned the same value. In fact I expect it not to. I was arguing that the spec for a GUID specifies the byte order to be little endian regardless of the machine byte order which is what BitConverter.IsLittleEndian reflects. While I did refer to the MS source I did so only because the documentation was a little sparse in that area. Perhaps you were confused by the fact that the answer was changed. It used to contain a call to BitConverter.IsLittleEndian which was removed after I argued against it. – Jeff Walker Code Ranger Aug 13 '14 at 00:16
  • @ScottChamberlain The removal of BitConverter.IsLittleEndian was precisely to ensure the behavior was correct on mono or any future .NET platform implementation. If you check the edit history of the answer you can see the old version. The current version (without BitConverter.IsLittleEndian is correct). I hope this helps clarify. – Jeff Walker Code Ranger Aug 13 '14 at 00:20
3

Here is a client side "sequential guid" solution.

http://www.pinvoke.net/default.aspx/rpcrt4.uuidcreate

using System;
using System.Runtime.InteropServices;


namespace MyCompany.MyTechnology.Framework.CrossDomain.GuidExtend
{
    public static class Guid
    {

        /*

        Original Reference for Code:
        http://www.pinvoke.net/default.aspx/rpcrt4/UuidCreateSequential.html

        */


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

        public static System.Guid NewGuid()
        {
            return CreateSequentialUuid();
        }


        public static System.Guid CreateSequentialUuid()
        {
            const int RPC_S_OK = 0;
            System.Guid g;
            int hr = UuidCreateSequential(out g);
            if (hr != RPC_S_OK)
                throw new ApplicationException("UuidCreateSequential failed: " + hr);
            return g;
        }


        /*

        Text From URL above:

        UuidCreateSequential (rpcrt4)

        Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
        To create a page in a module other than rpcrt4, prefix the name with the module name and a period.
        . Summary
        Creates a new UUID 
        C# Signature:
        [DllImport("rpcrt4.dll", SetLastError=true)]
        static extern int UuidCreateSequential(out Guid guid);


        VB Signature:
        Declare Function UuidCreateSequential Lib "rpcrt4.dll" (ByRef id As Guid) As Integer


        User-Defined Types:
        None.

        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.

        CoCreateGuid generates random-looking GUIDs like these:

        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()
        {
           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;
        }



        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




        */








    }
}

Keywords: CreateSequentialUUID SequentialUUID

granadaCoder
  • 21,474
  • 7
  • 81
  • 117
0

I have a GitHub Gist with a Java like UUID implementation in C#: https://gist.github.com/rickbeerendonk/13655dd24ec574954366

The UUID can be created from the least and most significant bits, just like in Java. It also exposes them. The implementation has an explicit conversion to a GUID and an implicit conversion from a GUID.

0

I don't know about methods; however, the type to GUID can be done via:

Guid iid = System.Runtime.InteropServices.Marshal.GenerateGuidForType(typeof(IFoo));

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.generateguidfortype.aspx

csharptest.net
  • 53,926
  • 10
  • 66
  • 86