private void Initialize(EventRecord eventRecord)
{
int size = 0;
const uint BufferTooSmall = 122;
const uint ErrorlementNotFound = 1168;
int error = NativeMethods.TdhGetEventInformation(ref eventRecord, 0, IntPtr.Zero, IntPtr.Zero, ref size);
if (error == ErrorlementNotFound)
{
// Nothing else to do here.
this._hasProperties = false;
return;
}
this._hasProperties = true;
if (error != BufferTooSmall)
{
throw new Win32Exception(error);
}
// Get the event information (schema)
this._address = Marshal.AllocHGlobal(size);
error = NativeMethods.TdhGetEventInformation(ref eventRecord, 0, IntPtr.Zero, _address, ref size);
if (error != 0)
{
throw new System.ComponentModel.Win32Exception(error);
}
// Marshal the first part of the trace event information.
_traceEventInfo = (TraceEventInfo)Marshal.PtrToStructure(this._address, typeof(TraceEventInfo));
// Marshal the second part of the trace event information, the array of property info.
int actualSize = Marshal.SizeOf(this._traceEventInfo);
if (size != actualSize)
{
int structSize = Marshal.SizeOf(typeof(EventPropertyInfo));
int itemsLeft = (size - actualSize) / structSize;
this._eventPropertyInfoArray = new EventPropertyInfo[itemsLeft];
long baseAddress = _address.ToInt64() + actualSize;
for (int i = 0; i < itemsLeft; i++)
{
IntPtr structPtr = new IntPtr(baseAddress + (i * structSize));
EventPropertyInfo info = (EventPropertyInfo)Marshal.PtrToStructure(structPtr, typeof(EventPropertyInfo));
this._eventPropertyInfoArray[i] = info;
}
}
// Get the opcode name
if (_traceEventInfo.OpcodeNameOffset > 0)
{
this.EventName = Marshal.PtrToStringUni(new IntPtr(_address.ToInt64() + _traceEventInfo.OpcodeNameOffset));
}
}