From 019c3b87df68d970a98f88124c46a66179361016 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 29 Nov 2022 00:18:04 +0500 Subject: [PATCH] Replace `DllImport` with `LibraryImport` in SMA - 7 (#18594) --- .../Interop/Windows/EventActivityIdControl.cs | 54 +++++++++++++++++++ .../utils/tracing/EtwActivity.cs | 42 +-------------- 2 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 src/System.Management.Automation/engine/Interop/Windows/EventActivityIdControl.cs diff --git a/src/System.Management.Automation/engine/Interop/Windows/EventActivityIdControl.cs b/src/System.Management.Automation/engine/Interop/Windows/EventActivityIdControl.cs new file mode 100644 index 0000000000..8152a79314 --- /dev/null +++ b/src/System.Management.Automation/engine/Interop/Windows/EventActivityIdControl.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#nullable enable + +#if !UNIX +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Windows + { + internal enum ActivityControl : uint + { + /// + /// Gets the ActivityId from thread local storage. + /// + Get = 1, + + /// + /// Sets the ActivityId in the thread local storage. + /// + Set = 2, + + /// + /// Creates a new activity id. + /// + Create = 3, + + /// + /// Sets the activity id in thread local storage and returns the previous value. + /// + GetSet = 4, + + /// + /// Creates a new activity id, sets thread local storage, and returns the previous value. + /// + CreateSet = 5 + } + + [LibraryImport("api-ms-win-eventing-provider-l1-1-0.dll")] + internal static unsafe partial int EventActivityIdControl(ActivityControl controlCode, Guid* activityId); + + internal static unsafe int GetEventActivityIdControl(ref Guid activityId) + { + fixed (Guid* guidPtr = &activityId) + { + return EventActivityIdControl(ActivityControl.Get, guidPtr); + } + } + } +} +#endif diff --git a/src/System.Management.Automation/utils/tracing/EtwActivity.cs b/src/System.Management.Automation/utils/tracing/EtwActivity.cs index dfe642ee84..6a0285682d 100644 --- a/src/System.Management.Automation/utils/tracing/EtwActivity.cs +++ b/src/System.Management.Automation/utils/tracing/EtwActivity.cs @@ -300,7 +300,7 @@ namespace System.Management.Automation.Tracing public static Guid GetActivityId() { Guid activityId = Guid.Empty; - uint hresult = UnsafeNativeMethods.EventActivityIdControl(UnsafeNativeMethods.ActivityControlCode.Get, ref activityId); + Interop.Windows.GetEventActivityIdControl(ref activityId); return activityId; } @@ -496,46 +496,6 @@ namespace System.Management.Automation.Tracing return currentProvider; } - - private static class UnsafeNativeMethods - { - internal enum ActivityControlCode : uint - { - /// - /// Gets the ActivityId from thread local storage. - /// - Get = 1, - - /// - /// Sets the ActivityId in the thread local storage. - /// - Set = 2, - - /// - /// Creates a new activity id. - /// - Create = 3, - - /// - /// Sets the activity id in thread local storage and returns the previous value. - /// - GetSet = 4, - - /// - /// Creates a new activity id, sets thread local storage, and returns the previous value. - /// - CreateSet = 5 - } - - /// - /// Provides interop access to creating, querying and setting the current activity identifier. - /// - /// The indicating the type of operation to perform. - /// The activity id to set or retrieve. - /// Zero on success. - [DllImport(PinvokeDllNames.EventActivityIdControlDllName, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = CharSet.Unicode)] - internal static extern unsafe uint EventActivityIdControl([In] ActivityControlCode controlCode, [In][Out] ref Guid activityId); - } } }