Skip to content

Commit 019c3b8

Browse files
authored
Replace DllImport with LibraryImport in SMA - 7 (PowerShell#18594)
1 parent ab86878 commit 019c3b8

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#nullable enable
5+
6+
#if !UNIX
7+
using System;
8+
using System.Runtime.InteropServices;
9+
10+
internal static partial class Interop
11+
{
12+
internal static partial class Windows
13+
{
14+
internal enum ActivityControl : uint
15+
{
16+
/// <summary>
17+
/// Gets the ActivityId from thread local storage.
18+
/// </summary>
19+
Get = 1,
20+
21+
/// <summary>
22+
/// Sets the ActivityId in the thread local storage.
23+
/// </summary>
24+
Set = 2,
25+
26+
/// <summary>
27+
/// Creates a new activity id.
28+
/// </summary>
29+
Create = 3,
30+
31+
/// <summary>
32+
/// Sets the activity id in thread local storage and returns the previous value.
33+
/// </summary>
34+
GetSet = 4,
35+
36+
/// <summary>
37+
/// Creates a new activity id, sets thread local storage, and returns the previous value.
38+
/// </summary>
39+
CreateSet = 5
40+
}
41+
42+
[LibraryImport("api-ms-win-eventing-provider-l1-1-0.dll")]
43+
internal static unsafe partial int EventActivityIdControl(ActivityControl controlCode, Guid* activityId);
44+
45+
internal static unsafe int GetEventActivityIdControl(ref Guid activityId)
46+
{
47+
fixed (Guid* guidPtr = &activityId)
48+
{
49+
return EventActivityIdControl(ActivityControl.Get, guidPtr);
50+
}
51+
}
52+
}
53+
}
54+
#endif

src/System.Management.Automation/utils/tracing/EtwActivity.cs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public static Guid CreateActivityId()
300300
public static Guid GetActivityId()
301301
{
302302
Guid activityId = Guid.Empty;
303-
uint hresult = UnsafeNativeMethods.EventActivityIdControl(UnsafeNativeMethods.ActivityControlCode.Get, ref activityId);
303+
Interop.Windows.GetEventActivityIdControl(ref activityId);
304304
return activityId;
305305
}
306306

@@ -496,46 +496,6 @@ private EventProvider GetProvider()
496496

497497
return currentProvider;
498498
}
499-
500-
private static class UnsafeNativeMethods
501-
{
502-
internal enum ActivityControlCode : uint
503-
{
504-
/// <summary>
505-
/// Gets the ActivityId from thread local storage.
506-
/// </summary>
507-
Get = 1,
508-
509-
/// <summary>
510-
/// Sets the ActivityId in the thread local storage.
511-
/// </summary>
512-
Set = 2,
513-
514-
/// <summary>
515-
/// Creates a new activity id.
516-
/// </summary>
517-
Create = 3,
518-
519-
/// <summary>
520-
/// Sets the activity id in thread local storage and returns the previous value.
521-
/// </summary>
522-
GetSet = 4,
523-
524-
/// <summary>
525-
/// Creates a new activity id, sets thread local storage, and returns the previous value.
526-
/// </summary>
527-
CreateSet = 5
528-
}
529-
530-
/// <summary>
531-
/// Provides interop access to creating, querying and setting the current activity identifier.
532-
/// </summary>
533-
/// <param name="controlCode">The <see cref="ActivityControlCode"/> indicating the type of operation to perform.</param>
534-
/// <param name="activityId">The activity id to set or retrieve.</param>
535-
/// <returns>Zero on success.</returns>
536-
[DllImport(PinvokeDllNames.EventActivityIdControlDllName, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = CharSet.Unicode)]
537-
internal static extern unsafe uint EventActivityIdControl([In] ActivityControlCode controlCode, [In][Out] ref Guid activityId);
538-
}
539499
}
540500
}
541501

0 commit comments

Comments
 (0)