Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline Guid.NewGuid on Windows #100719

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static partial class Interop
internal static partial class Ole32
{
[LibraryImport(Libraries.Ole32)]
internal static partial int CoCreateGuid(out Guid guid);
internal static unsafe partial int CoCreateGuid(Guid* guid);
}
}
24 changes: 14 additions & 10 deletions src/libraries/System.Private.CoreLib/src/System/Guid.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ namespace System
{
public partial struct Guid
{
public static Guid NewGuid()
public static unsafe Guid NewGuid()
{
// CoCreateGuid should never return Guid.Empty, since it attempts to maintain some
// uniqueness guarantees.
// CoCreateGuid should never return Guid.Empty, since it attempts to maintain some uniqueness guarantees.

int hr = Interop.Ole32.CoCreateGuid(out Guid g);
// We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
// in the HR to ComException mappings into the core library just for this so we will try a generic exception if
// we ever hit this condition.
Guid g;
int hr = Interop.Ole32.CoCreateGuid(&g);
if (hr != 0)
{
Exception ex = new Exception();
ex.HResult = hr;
throw ex;
ThrowForHr(hr);
}

return g;
}

private static void ThrowForHr(int hr)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
// We don't expect that this will ever throw an error, none are even documented, and so we don't want to pull
// in the HR to ComException mappings into the core library just for this so we will try a generic exception if
// we ever hit this condition.
throw new Exception() { HResult = hr };
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading