Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / public / internal / VSCommon / inc / ManagedCodeMarkers.cs / 1 / ManagedCodeMarkers.cs
using System; using System.Runtime.InteropServices; using Microsoft.Win32; namespace Microsoft.Internal.Performance { internal sealed class CodeMarkers { // Singleton access public static readonly CodeMarkers Instance = new CodeMarkers(); internal class NativeMethods { ///// Code markers' functions (imported from the code markers dll) #if Codemarkers_IncludeAppEnum [DllImport(DllName, EntryPoint = "InitPerf")] public static extern void DllInitPerf(System.Int32 iApp); [DllImport(DllName, EntryPoint = "UnInitPerf")] public static extern void DllUnInitPerf(System.Int32 iApp); #endif //Codemarkers_IncludeAppEnum [DllImport(DllName, EntryPoint = "PerfCodeMarker")] public static extern void DllPerfCodeMarker(System.Int32 nTimerID, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] byte [] aUserParams, System.Int32 cbParams); [DllImport("kernel32.dll")] public static extern System.UInt16 FindAtom(string lpString); [DllImport("kernel32.dll")] public static extern System.UInt16 AddAtom(string lpString); [DllImport("kernel32.dll")] public static extern System.UInt16 DeleteAtom(System.UInt16 atom); } // Atom name. This ATOM will be set by the host application when code markers are enabled // in the registry. const string AtomName = "VSCodeMarkersEnabled"; // CodeMarkers DLL name const string DllName = "Microsoft.Internal.Performance.CodeMarkers.dll"; // Do we want to use code markers? bool fUseCodeMarkers; // Constructor. Do not call directly. Use CodeMarkers.Instance to access the singleton // Checks to see if code markers are enabled by looking for a named ATOM private CodeMarkers() { // This ATOM will be set by the native Code Markers host fUseCodeMarkers = (NativeMethods.FindAtom(AtomName) != 0); } // Implements sending the code marker value nTimerID. public void CodeMarker(CodeMarkerEvent nTimerID) { if (!fUseCodeMarkers) return; try { NativeMethods.DllPerfCodeMarker((int)nTimerID, null, 0); } catch( DllNotFoundException ) { // If the DLL doesn't load or the entry point doesn't exist, then // abandon all further attempts to send codemarkers. fUseCodeMarkers = false; } } // Implements sending the code marker value nTimerID with additional user data public void CodeMarkerEx(CodeMarkerEvent nTimerID, byte[] aBuff) { if (aBuff == null) throw new ArgumentNullException("aBuff"); if (!fUseCodeMarkers) return; try { NativeMethods.DllPerfCodeMarker((int)nTimerID, aBuff, aBuff.Length); } catch (DllNotFoundException) { // If the DLL doesn't load or the entry point doesn't exist, then // abandon all further attempts to send codemarkers. fUseCodeMarkers = false; } } #if Codemarkers_IncludeAppEnum // Check the registry and, if appropriate, loads and initializes the code markers dll. // Must be used only if your code is called from outside of VS. [Obsolete("Please use InitPerformanceDll(CodeMarkerApp, string) instead to specify a registry root")] public void InitPerformanceDll(CodeMarkerApp iApp) { InitPerformanceDll(iApp, "Software\\Microsoft\\VisualStudio\\8.0" ); } public void InitPerformanceDll(CodeMarkerApp iApp, string strRegRoot ) { fUseCodeMarkers = false; if (!UseCodeMarkers( strRegRoot )) { return; } try { // Add an ATOM so that other CodeMarker enabled code in this process // knows that CodeMarkers are enabled NativeMethods.AddAtom(AtomName); NativeMethods.DllInitPerf((int)iApp); fUseCodeMarkers = true; } catch( DllNotFoundException ) { ; // Ignore, but note that fUseCodeMarkers is false } } [Obsolete("Second parameter is ignored. Please use InitPerformanceDll(CodeMarkerApp, string) instead to specify a registry root")] public void InitPerformanceDll(CodeMarkerApp iApp, bool bEndBoot) { // bEndBoot is ignored InitPerformanceDll( iApp ); } // Checks the registry to see if code markers are enabled bool UseCodeMarkers( string strRegRoot ) { // SECURITY: We no longer check HKCU because that might lead to a DLL spoofing attack via // the code markers DLL. Check only HKLM since that has a strong ACL. You therefore need // admin rights to enable/disable code markers. // It doesn't matter what the value is, if it's present and not empty, code markers are enabled return !String.IsNullOrEmpty( GetPerformanceSubKey(Registry.LocalMachine, strRegRoot) ); } // Reads the Performance subkey from the appropriate registry key // Returns: the Default value from the subkey (null if not found) string GetPerformanceSubKey(RegistryKey hKey, string strRegRoot) { if (hKey == null) return null; // does the subkey exist string str = null; using (RegistryKey key = hKey.OpenSubKey(strRegRoot + "\\Performance")) { if (key != null) { // reads the default value str = key.GetValue("").ToString(); } } return str; } // Opposite of InitPerformanceDLL. Call it when your app does not need the code markers dll. public void UninitializePerformanceDLL(CodeMarkerApp iApp) { if (!fUseCodeMarkers) { return; } fUseCodeMarkers = false; // Delete the atom created during the initialization if it exists System.UInt16 atom = NativeMethods.FindAtom(AtomName); if (atom != 0) { NativeMethods.DeleteAtom(atom); } try { NativeMethods.DllUnInitPerf((int)iApp); } catch( DllNotFoundException ) { // Eat exception } } #endif //Codemarkers_IncludeAppEnum } } // 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
- Transform3D.cs
- FileEnumerator.cs
- TypefaceMap.cs
- LinearKeyFrames.cs
- MouseBinding.cs
- DateTimeHelper.cs
- NavigationPropertyAccessor.cs
- TextSearch.cs
- _LazyAsyncResult.cs
- DetailsView.cs
- AbandonedMutexException.cs
- IntSecurity.cs
- PrivilegeNotHeldException.cs
- ReadOnlyDataSourceView.cs
- TCEAdapterGenerator.cs
- glyphs.cs
- Repeater.cs
- StickyNote.cs
- ConfigurationStrings.cs
- IisTraceListener.cs
- LineServices.cs
- COM2PropertyDescriptor.cs
- CorePropertiesFilter.cs
- VisualStyleTypesAndProperties.cs
- RequestCache.cs
- SqlDataSourceQuery.cs
- AdRotatorDesigner.cs
- CollaborationHelperFunctions.cs
- ContainerActivationHelper.cs
- FontEditor.cs
- DataGridViewMethods.cs
- TextureBrush.cs
- EntityDataSourceUtil.cs
- ObjectCloneHelper.cs
- LinkedResourceCollection.cs
- SiteMapDataSource.cs
- MethodExpr.cs
- CodeDOMProvider.cs
- WebPartTracker.cs
- TdsParserSessionPool.cs
- SerializationAttributes.cs
- ProtocolsConfigurationHandler.cs
- ListBoxAutomationPeer.cs
- VolatileEnlistmentMultiplexing.cs
- SessionSwitchEventArgs.cs
- FixedSOMLineCollection.cs
- SvcMapFileLoader.cs
- RangeValidator.cs
- RegexFCD.cs
- SizeLimitedCache.cs
- WorkflowRuntimeSection.cs
- StringSorter.cs
- PathFigureCollection.cs
- DelegateCompletionCallbackWrapper.cs
- ListViewInsertedEventArgs.cs
- IntegrationExceptionEventArgs.cs
- TemplatedMailWebEventProvider.cs
- DataKeyCollection.cs
- Helpers.cs
- ManagedFilter.cs
- xmlsaver.cs
- ArgumentValue.cs
- EncoderFallback.cs
- ChangeProcessor.cs
- IProvider.cs
- TextAdaptor.cs
- ToolbarAUtomationPeer.cs
- BindingList.cs
- DataRow.cs
- ToolStripDropDownMenu.cs
- PostBackOptions.cs
- ColumnHeaderConverter.cs
- GiveFeedbackEvent.cs
- PathFigureCollectionValueSerializer.cs
- SQLGuid.cs
- WizardStepBase.cs
- Emitter.cs
- URLIdentityPermission.cs
- GifBitmapEncoder.cs
- SemanticKeyElement.cs
- DataObjectEventArgs.cs
- WebControlsSection.cs
- EventLogEntry.cs
- Thickness.cs
- DragDeltaEventArgs.cs
- RightsController.cs
- GB18030Encoding.cs
- ParserHooks.cs
- FixedDSBuilder.cs
- PointAnimationUsingKeyFrames.cs
- EndpointDispatcher.cs
- ExtensionDataObject.cs
- SelectionProviderWrapper.cs
- Stream.cs
- FixedElement.cs
- XmlDocumentType.cs
- TableRow.cs
- DisplayNameAttribute.cs
- XmlUrlResolver.cs
- UInt16Converter.cs