@@ -78,6 +78,7 @@ internal SessionInfo(int sessionIdBit_, int etwSessionId_)
78
78
79
79
[ SecurityCritical ]
80
80
UnsafeNativeMethods . ManifestEtw . EtwEnableCallback m_etwCallback ; // Trace Callback function
81
+ GCHandle m_thisGCHandle ;
81
82
private long m_regHandle ; // Trace Registration Handle
82
83
private byte m_level ; // Tracing Level
83
84
private long m_anyKeywordMask ; // Trace Enable Flags
@@ -149,13 +150,25 @@ internal EventProvider()
149
150
internal unsafe void Register ( Guid providerGuid )
150
151
{
151
152
m_providerId = providerGuid ;
152
- uint status ;
153
153
m_etwCallback = new UnsafeNativeMethods . ManifestEtw . EtwEnableCallback ( EtwEnableCallBack ) ;
154
154
155
- status = EventRegister ( ref m_providerId , m_etwCallback ) ;
156
- if ( status != 0 )
155
+ if ( m_thisGCHandle . IsAllocated )
156
+ m_thisGCHandle . Free ( ) ;
157
+
158
+ m_thisGCHandle = GCHandle . Alloc ( this ) ;
159
+
160
+ try
161
+ {
162
+ var status = UnsafeNativeMethods . ManifestEtw . EventRegister ( ref providerGuid , m_etwCallback , GCHandle . ToIntPtr ( m_thisGCHandle ) . ToPointer ( ) , ref m_regHandle ) ;
163
+ if ( status != 0 )
164
+ {
165
+ throw new ArgumentException ( Win32Native . GetMessage ( unchecked ( ( int ) status ) ) ) ;
166
+ }
167
+ }
168
+ catch
157
169
{
158
- throw new ArgumentException ( Win32Native . GetMessage ( unchecked ( ( int ) status ) ) ) ;
170
+ m_thisGCHandle . Free ( ) ;
171
+ throw ;
159
172
}
160
173
}
161
174
@@ -264,22 +277,44 @@ private unsafe void Deregister()
264
277
{
265
278
EventUnregister ( ) ;
266
279
m_regHandle = 0 ;
280
+ m_thisGCHandle . Free ( ) ;
267
281
}
268
282
}
269
283
284
+ [ AttributeUsage ( AttributeTargets . Method ) ]
285
+ private sealed class MonoPInvokeCallbackAttribute : Attribute
286
+ {
287
+ public MonoPInvokeCallbackAttribute ( Type t )
288
+ {
289
+ }
290
+ }
291
+
292
+ [ MonoPInvokeCallback ( typeof ( UnsafeNativeMethods . ManifestEtw . EtwEnableCallback ) ) ]
293
+ unsafe static void EtwEnableCallBack (
294
+ [ In ] ref System . Guid sourceId ,
295
+ [ In ] int controlCode ,
296
+ [ In ] byte setLevel ,
297
+ [ In ] long anyKeyword ,
298
+ [ In ] long allKeyword ,
299
+ [ In ] UnsafeNativeMethods . ManifestEtw . EVENT_FILTER_DESCRIPTOR * filterData ,
300
+ [ In ] void * callbackContext
301
+ )
302
+ {
303
+ var _this = ( EventProvider ) GCHandle . FromIntPtr ( new IntPtr ( callbackContext ) ) . Target ;
304
+ _this . EtwEnableCallBackImpl ( controlCode , setLevel , anyKeyword , allKeyword , filterData ) ;
305
+ }
306
+
270
307
// <SecurityKernel Critical="True" Ring="0">
271
308
// <UsesUnsafeCode Name="Parameter filterData of type: Void*" />
272
309
// <UsesUnsafeCode Name="Parameter callbackContext of type: Void*" />
273
310
// </SecurityKernel>
274
311
[ System . Security . SecurityCritical ]
275
- unsafe void EtwEnableCallBack (
276
- [ In ] ref System . Guid sourceId ,
312
+ unsafe void EtwEnableCallBackImpl (
277
313
[ In ] int controlCode ,
278
314
[ In ] byte setLevel ,
279
315
[ In ] long anyKeyword ,
280
316
[ In ] long allKeyword ,
281
- [ In ] UnsafeNativeMethods . ManifestEtw . EVENT_FILTER_DESCRIPTOR * filterData ,
282
- [ In ] void * callbackContext
317
+ [ In ] UnsafeNativeMethods . ManifestEtw . EVENT_FILTER_DESCRIPTOR * filterData
283
318
)
284
319
{
285
320
// This is an optional callback API. We will therefore ignore any failures that happen as a
@@ -539,7 +574,7 @@ private unsafe bool GetDataFromController(int etwSessionId,
539
574
dataStart = 0 ;
540
575
if ( filterData == null )
541
576
{
542
- #if ! ES_BUILD_PCL
577
+ #if ! ES_BUILD_PCL && ! MONO
543
578
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerId + "}" ;
544
579
if ( System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( IntPtr ) ) == 8 )
545
580
regKey = @"HKEY_LOCAL_MACHINE\Software" + @"\Wow6432Node" + regKey ;
@@ -1161,13 +1196,6 @@ internal unsafe bool WriteEventRaw(
1161
1196
1162
1197
// These are look-alikes to the Manifest based ETW OS APIs that have been shimmed to work
1163
1198
// either with Manifest ETW or Classic ETW (if Manifest based ETW is not available).
1164
- [ SecurityCritical ]
1165
- private unsafe uint EventRegister ( ref Guid providerId , UnsafeNativeMethods . ManifestEtw . EtwEnableCallback enableCallback )
1166
- {
1167
- m_providerId = providerId ;
1168
- m_etwCallback = enableCallback ;
1169
- return UnsafeNativeMethods . ManifestEtw . EventRegister ( ref providerId , enableCallback , null , ref m_regHandle ) ;
1170
- }
1171
1199
1172
1200
[ SecurityCritical ]
1173
1201
private uint EventUnregister ( )
0 commit comments