Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Documents / WinEventHandler.cs / 1305600 / WinEventHandler.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: WinEventHandler implementation.
//
// History:
// 02/04/2005 : yutakas - created.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
using System.Security.Permissions;
using System.Windows.Threading;
using MS.Win32;
using MS.Internal;
namespace System.Windows.Documents
{
internal class WinEventHandler : IDisposable
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructors
// ctor that takes a range of events
///
/// Critical - as this calls the setter for _winEventProc.Value.
/// Safe - as this doesn't allow an arbitrary value to be set.
///
[SecurityCritical, SecurityTreatAsSafe]
internal WinEventHandler(int eventMin, int eventMax)
{
_eventMin = eventMin;
_eventMax = eventMax;
_winEventProc.Value = new NativeMethods.WinEventProcDef(WinEventDefaultProc);
// Keep the garbage collector from moving things around
_gchThis = GCHandle.Alloc(_winEventProc.Value);
// Workaround for bug 150666.
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
if (dispatcher != null)
{
dispatcher.ShutdownFinished += new EventHandler(OnDispatcherShutDown);
}
}
~WinEventHandler()
{
Clear();
}
#endregion Constructors
//------------------------------------------------------
//
// Internal Methods
//
//-----------------------------------------------------
#region Internal Methods
public void Dispose()
{
// no need to call this finalizer.
GC.SuppressFinalize(this);
Clear();
}
// callback for the class inherited from this.
internal virtual void WinEventProc(int eventId, IntPtr hwnd)
{
}
///
/// Critical: This code frees the gchandle and is critical because it calls GCHandle
/// TreatAsSafe: Ok to call free on a handle that we have allocated
///
[SecurityCritical,SecurityTreatAsSafe]
internal void Clear()
{
// Make sure that the hooks is uninitialzied.
Stop();
// free GC handle.
if (_gchThis.IsAllocated)
{
_gchThis.Free();
}
}
// install WinEvent hook and start getting the callback.
///
/// Critical - as this calls SetWinEventHook which has a SUC on it and also calls
/// the setter for _hHook.
/// Safe - as this does not allow an arbitrary method to be set up as a hook and
/// also doesn't allow an aribtrary value to be set on _hHook.Value.
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Start()
{
if (_gchThis.IsAllocated)
{
_hHook.Value = UnsafeNativeMethods.SetWinEventHook(_eventMin, _eventMax, IntPtr.Zero, _winEventProc.Value,
0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT);
if (_hHook.Value == IntPtr.Zero )
{
Stop();
}
}
}
// uninstall WinEvent hook.
///
/// Critical - as this calls UnhookWinEvent which has a SUC on it and also
/// calls the setter for _hHook.Value.
/// Safe - as this does not allow an arbitrary hook to be removed and sets
/// _hHook.Value to IntPtr.Zero which is safe.
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Stop()
{
if (_hHook.Value != IntPtr.Zero )
{
UnsafeNativeMethods.UnhookWinEvent(_hHook.Value);
_hHook.Value = IntPtr.Zero ;
}
}
#endregion Internal Methods
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
private void WinEventDefaultProc(int winEventHook, int eventId, IntPtr hwnd, int idObject, int idChild, int eventThread, int eventTime)
{
WinEventProc(eventId , hwnd);
}
// Workaround for bug 150666.
private void OnDispatcherShutDown(object sender, EventArgs args)
{
Stop();
}
#endregion Private Methods
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Fields
// min WinEvent.
private int _eventMin;
// max WinEvent.
private int _eventMax;
// hook handle
private SecurityCriticalDataForSet _hHook;
// the callback.
private SecurityCriticalDataForSet _winEventProc;
// GCHandle to keep the garbage collector from moving things around
private GCHandle _gchThis;
#endregion Private Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: WinEventHandler implementation.
//
// History:
// 02/04/2005 : yutakas - created.
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
using System.Security.Permissions;
using System.Windows.Threading;
using MS.Win32;
using MS.Internal;
namespace System.Windows.Documents
{
internal class WinEventHandler : IDisposable
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructors
// ctor that takes a range of events
///
/// Critical - as this calls the setter for _winEventProc.Value.
/// Safe - as this doesn't allow an arbitrary value to be set.
///
[SecurityCritical, SecurityTreatAsSafe]
internal WinEventHandler(int eventMin, int eventMax)
{
_eventMin = eventMin;
_eventMax = eventMax;
_winEventProc.Value = new NativeMethods.WinEventProcDef(WinEventDefaultProc);
// Keep the garbage collector from moving things around
_gchThis = GCHandle.Alloc(_winEventProc.Value);
// Workaround for bug 150666.
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
if (dispatcher != null)
{
dispatcher.ShutdownFinished += new EventHandler(OnDispatcherShutDown);
}
}
~WinEventHandler()
{
Clear();
}
#endregion Constructors
//------------------------------------------------------
//
// Internal Methods
//
//-----------------------------------------------------
#region Internal Methods
public void Dispose()
{
// no need to call this finalizer.
GC.SuppressFinalize(this);
Clear();
}
// callback for the class inherited from this.
internal virtual void WinEventProc(int eventId, IntPtr hwnd)
{
}
///
/// Critical: This code frees the gchandle and is critical because it calls GCHandle
/// TreatAsSafe: Ok to call free on a handle that we have allocated
///
[SecurityCritical,SecurityTreatAsSafe]
internal void Clear()
{
// Make sure that the hooks is uninitialzied.
Stop();
// free GC handle.
if (_gchThis.IsAllocated)
{
_gchThis.Free();
}
}
// install WinEvent hook and start getting the callback.
///
/// Critical - as this calls SetWinEventHook which has a SUC on it and also calls
/// the setter for _hHook.
/// Safe - as this does not allow an arbitrary method to be set up as a hook and
/// also doesn't allow an aribtrary value to be set on _hHook.Value.
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Start()
{
if (_gchThis.IsAllocated)
{
_hHook.Value = UnsafeNativeMethods.SetWinEventHook(_eventMin, _eventMax, IntPtr.Zero, _winEventProc.Value,
0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT);
if (_hHook.Value == IntPtr.Zero )
{
Stop();
}
}
}
// uninstall WinEvent hook.
///
/// Critical - as this calls UnhookWinEvent which has a SUC on it and also
/// calls the setter for _hHook.Value.
/// Safe - as this does not allow an arbitrary hook to be removed and sets
/// _hHook.Value to IntPtr.Zero which is safe.
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Stop()
{
if (_hHook.Value != IntPtr.Zero )
{
UnsafeNativeMethods.UnhookWinEvent(_hHook.Value);
_hHook.Value = IntPtr.Zero ;
}
}
#endregion Internal Methods
//------------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
private void WinEventDefaultProc(int winEventHook, int eventId, IntPtr hwnd, int idObject, int idChild, int eventThread, int eventTime)
{
WinEventProc(eventId , hwnd);
}
// Workaround for bug 150666.
private void OnDispatcherShutDown(object sender, EventArgs args)
{
Stop();
}
#endregion Private Methods
//-----------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Fields
// min WinEvent.
private int _eventMin;
// max WinEvent.
private int _eventMax;
// hook handle
private SecurityCriticalDataForSet _hHook;
// the callback.
private SecurityCriticalDataForSet _winEventProc;
// GCHandle to keep the garbage collector from moving things around
private GCHandle _gchThis;
#endregion Private Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- QuotedPrintableStream.cs
- EdmSchemaAttribute.cs
- LicenseProviderAttribute.cs
- ChooseAction.cs
- ComboBoxAutomationPeer.cs
- PowerStatus.cs
- MimeWriter.cs
- XNameTypeConverter.cs
- BindingMAnagerBase.cs
- TableRowsCollectionEditor.cs
- DynamicPropertyReader.cs
- EndpointDiscoveryMetadata.cs
- CompareValidator.cs
- UnsafeNativeMethods.cs
- DataGridTemplateColumn.cs
- CloudCollection.cs
- DataFormat.cs
- ButtonFlatAdapter.cs
- ObjectQueryExecutionPlan.cs
- ThemeableAttribute.cs
- AspCompat.cs
- WizardStepBase.cs
- EFDataModelProvider.cs
- ActionMessageFilter.cs
- GPStream.cs
- __ComObject.cs
- Authorization.cs
- CodeCastExpression.cs
- PersonalizationState.cs
- input.cs
- PageParserFilter.cs
- MachineSettingsSection.cs
- EmbossBitmapEffect.cs
- DefaultMemberAttribute.cs
- DataGridViewCellStyleConverter.cs
- ColorConvertedBitmapExtension.cs
- PerspectiveCamera.cs
- CreateUserWizardStep.cs
- XmlIterators.cs
- StylusEventArgs.cs
- BulletedList.cs
- XpsStructure.cs
- InfiniteTimeSpanConverter.cs
- XmlSerializationGeneratedCode.cs
- ReliabilityContractAttribute.cs
- SerializationUtility.cs
- StyleCollection.cs
- ImageAnimator.cs
- Transform3DGroup.cs
- XhtmlConformanceSection.cs
- OlePropertyStructs.cs
- Subtree.cs
- TextTreePropertyUndoUnit.cs
- ReferenceEqualityComparer.cs
- SqlDependencyUtils.cs
- TableLayoutSettingsTypeConverter.cs
- OdbcInfoMessageEvent.cs
- TemplateBindingExtensionConverter.cs
- IteratorDescriptor.cs
- MultitargetingHelpers.cs
- LinqDataSourceSelectEventArgs.cs
- IsolatedStorage.cs
- SessionSwitchEventArgs.cs
- QilLiteral.cs
- _AutoWebProxyScriptEngine.cs
- QuotedPrintableStream.cs
- TraceLevelHelper.cs
- SqlReorderer.cs
- SubtreeProcessor.cs
- ComplexBindingPropertiesAttribute.cs
- UIElementHelper.cs
- ProxyAttribute.cs
- PolyLineSegment.cs
- TitleStyle.cs
- Exceptions.cs
- TagPrefixInfo.cs
- DynamicDiscoSearcher.cs
- Debug.cs
- ProviderManager.cs
- ShowExpandedMultiValueConverter.cs
- DesignerTransaction.cs
- PropertyMetadata.cs
- TraceLevelHelper.cs
- HuffCodec.cs
- MessagePropertyFilter.cs
- WindowsScrollBar.cs
- AssemblyCollection.cs
- InstanceCreationEditor.cs
- XsdBuilder.cs
- IPipelineRuntime.cs
- Imaging.cs
- CommandLibraryHelper.cs
- EpmTargetPathSegment.cs
- DataSourceProvider.cs
- SecureConversationServiceCredential.cs
- ConfigXmlAttribute.cs
- BindingNavigator.cs
- sitestring.cs
- TemplateBindingExtensionConverter.cs
- HtmlAnchor.cs