1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 namespace System
6 {
7     partial struct Guid
8     {
9         // This will create a new guid.  Since we've now decided that constructors should 0-init,
10         // we need a method that allows users to create a guid.
NewGuidSystem.Guid11         public static Guid NewGuid()
12         {
13             // CoCreateGuid should never return Guid.Empty, since it attempts to maintain some
14             // uniqueness guarantees.  It should also never return a known GUID, but it's unclear
15             // how extensively it checks for known values.
16 
17             Guid g;
18             int hr = Interop.mincore.CoCreateGuid(out g);
19             // We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
20             // in the HR to ComException mappings into the core library just for this so we will try a generic exception if
21             // we ever hit this condition.
22             if (hr != 0)
23             {
24                 Exception ex = new Exception();
25                 ex.SetErrorCode(hr);
26                 throw ex;
27             }
28             return g;
29         }
30     }
31 }
32