Skip to content

Commit e0ff36d

Browse files
authored
Streamline Guid.NewGuid on Windows (#100719)
Enable it to be inlined automatically and remove an extra LibraryImport helper
1 parent b8964d8 commit e0ff36d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoCreateGuid.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal static partial class Interop
99
internal static partial class Ole32
1010
{
1111
[LibraryImport(Libraries.Ole32)]
12-
internal static partial int CoCreateGuid(out Guid guid);
12+
internal static unsafe partial int CoCreateGuid(Guid* guid);
1313
}
1414
}

src/libraries/System.Private.CoreLib/src/System/Guid.Windows.cs

+14-10
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ namespace System
55
{
66
public partial struct Guid
77
{
8-
public static Guid NewGuid()
8+
public static unsafe Guid NewGuid()
99
{
10-
// CoCreateGuid should never return Guid.Empty, since it attempts to maintain some
11-
// uniqueness guarantees.
10+
// CoCreateGuid should never return Guid.Empty, since it attempts to maintain some uniqueness guarantees.
1211

13-
int hr = Interop.Ole32.CoCreateGuid(out Guid g);
14-
// We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
15-
// in the HR to ComException mappings into the core library just for this so we will try a generic exception if
16-
// we ever hit this condition.
12+
Guid g;
13+
int hr = Interop.Ole32.CoCreateGuid(&g);
1714
if (hr != 0)
1815
{
19-
Exception ex = new Exception();
20-
ex.HResult = hr;
21-
throw ex;
16+
ThrowForHr(hr);
2217
}
18+
2319
return g;
2420
}
21+
22+
private static void ThrowForHr(int hr)
23+
{
24+
// We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
25+
// in the HR to ComException mappings into the core library just for this so we will try a generic exception if
26+
// we ever hit this condition.
27+
throw new Exception() { HResult = hr };
28+
}
2529
}
2630
}

0 commit comments

Comments
 (0)