Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / Microsoft / Win32 / SafeNativeMethods.cs / 1305376 / SafeNativeMethods.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace Microsoft.Win32 { using System.Runtime.InteropServices; using System.Runtime.Versioning; using System; #if !SILVERLIGHT using System.Diagnostics.CodeAnalysis; using System.Security.Permissions; using System.Collections; using System.IO; using System.Text; using System.Threading; using Microsoft.Win32.SafeHandles; using System.Runtime.ConstrainedExecution; using System.Diagnostics; [HostProtection(MayLeakOnAbort = true)] [System.Security.SuppressUnmanagedCodeSecurity] #endif // !SILVERLIGHT internal static class SafeNativeMethods { #if SILVERLIGHT [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr CFStringCreateWithCharacters( IntPtr alloc, // CFAllocatorRef [MarshalAs(UnmanagedType.LPWStr)] string chars, int numChars); [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern void CFRelease(IntPtr cf); [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern int CFUserNotificationDisplayAlert(double timeout, uint flags, IntPtr iconUrl, IntPtr soundUrl, IntPtr localizationUrl, IntPtr alertHeader, IntPtr alertMessage, IntPtr defaultButtonTitle, IntPtr alternateButtonTitle, IntPtr otherButtonTitle, ref uint responseFlags); #endif public const int MB_RIGHT = 0x00080000, MB_RTLREADING = 0x00100000; #if !FEATURE_PAL [DllImport(ExternDll.Gdi32, CharSet = System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static extern bool GetTextMetrics(IntPtr hDC, [In, Out] NativeMethods.TEXTMETRIC tm); [DllImport(ExternDll.Gdi32, CharSet = System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern IntPtr GetStockObject(int nIndex); #endif [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, BestFitMapping = true)] [ResourceExposure(ResourceScope.None)] public static extern void OutputDebugString(String message); [DllImport(ExternDll.User32, CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, string text, string caption, int type); #if !SILVERLIGHT public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100, FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200, FORMAT_MESSAGE_FROM_STRING = 0x00000400, FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000, FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true, BestFitMapping=true)] [SuppressMessage("Microsoft.Security", "CA2101:SpecifyMarshalingForPInvokeStringArguments")] [ResourceExposure(ResourceScope.None)] public static unsafe extern int FormatMessage(int dwFlags, SafeHandle lpSource, uint dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] arguments); [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true, BestFitMapping=true)] [ResourceExposure(ResourceScope.None)] public static extern int FormatMessage(int dwFlags, HandleRef lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr arguments); [DllImport(ExternDll.Kernel32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [ResourceExposure(ResourceScope.None)] public static extern bool CloseHandle(IntPtr handle); [DllImport(ExternDll.Kernel32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)] [ResourceExposure(ResourceScope.None)] public static extern bool CloseHandle(HandleRef handle); [DllImport(ExternDll.Kernel32)] [ResourceExposure(ResourceScope.None)] public static extern bool QueryPerformanceCounter(out long value); [DllImport(ExternDll.Kernel32)] [ResourceExposure(ResourceScope.None)] public static extern bool QueryPerformanceFrequency(out long value); #if !FEATURE_PAL public const int FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF, FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; [DllImport(ExternDll.User32, CharSet=System.Runtime.InteropServices.CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] public static extern int RegisterWindowMessage(string msg); #if DEBUG // Used only from debug code to assert we're on the right thread // for calling certain Windows methods. [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern int GetCurrentThreadId(); [DllImport(ExternDll.User32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId); #endif // file src\Services\Monitoring\system\Diagnosticts\SafeNativeMethods.cs [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)] [ResourceExposure(ResourceScope.Process)] public static extern IntPtr LoadLibrary(string libFilename); [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)] [ResourceExposure(ResourceScope.Process)] public static extern bool FreeLibrary(HandleRef hModule); [DllImport(ExternDll.Kernel32, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.None)] public static extern bool GetComputerName(StringBuilder lpBuffer, int[] nSize); public static unsafe int InterlockedCompareExchange(IntPtr pDestination, int exchange, int compare) { return Interlocked.CompareExchange(ref *(int *)pDestination.ToPointer(), exchange, compare); } [DllImport(ExternDll.PerfCounter, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static unsafe extern int FormatFromRawValue( uint dwCounterType, uint dwFormat, ref long pTimeBase, NativeMethods.PDH_RAW_COUNTER pRawValue1, NativeMethods.PDH_RAW_COUNTER pRawValue2, NativeMethods.PDH_FMT_COUNTERVALUE pFmtValue ); #endif // !FEATURE_PAL [DllImport(ExternDll.Kernel32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] internal static extern SafeWaitHandle CreateSemaphore(NativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes, int initialCount, int maximumCount, String name); [DllImport(ExternDll.Kernel32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] internal static extern SafeWaitHandle OpenSemaphore(/* DWORD */ int desiredAccess, bool inheritHandle, String name); [DllImport(ExternDll.Kernel32, SetLastError=true)] [ResourceExposure(ResourceScope.Machine)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal static extern bool ReleaseSemaphore(SafeWaitHandle handle, int releaseCount, out int previousCount); [DllImport(ExternDll.Kernel32, SetLastError = true)] [ResourceExposure(ResourceScope.None)] public static extern bool IsWow64Process(SafeProcessHandle hProcess, ref bool Wow64Process); [StructLayout(LayoutKind.Sequential)] internal class PROCESS_INFORMATION { // The handles in PROCESS_INFORMATION are initialized in unmanaged functions. // We can't use SafeHandle here because Interop doesn't support [out] SafeHandles in structures/classes yet. public IntPtr hProcess = IntPtr.Zero; public IntPtr hThread = IntPtr.Zero; public int dwProcessId = 0; public int dwThreadId = 0; // Note this class makes no attempt to free the handles // Use InitialSetHandle to copy to handles into SafeHandles } /* The following code has been removed to prevent FXCOP violations. The code is left here incase it needs to be resurrected. // From file src\services\timers\system\timers\safenativemethods.cs public delegate void TimerAPCProc(IntPtr argToCompletionRoutine, int timerLowValue, int timerHighValue); */ #endif //!SILVERLIGHT } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace Microsoft.Win32 { using System.Runtime.InteropServices; using System.Runtime.Versioning; using System; #if !SILVERLIGHT using System.Diagnostics.CodeAnalysis; using System.Security.Permissions; using System.Collections; using System.IO; using System.Text; using System.Threading; using Microsoft.Win32.SafeHandles; using System.Runtime.ConstrainedExecution; using System.Diagnostics; [HostProtection(MayLeakOnAbort = true)] [System.Security.SuppressUnmanagedCodeSecurity] #endif // !SILVERLIGHT internal static class SafeNativeMethods { #if SILVERLIGHT [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr CFStringCreateWithCharacters( IntPtr alloc, // CFAllocatorRef [MarshalAs(UnmanagedType.LPWStr)] string chars, int numChars); [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern void CFRelease(IntPtr cf); [DllImport("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", CallingConvention = CallingConvention.Cdecl)] public static extern int CFUserNotificationDisplayAlert(double timeout, uint flags, IntPtr iconUrl, IntPtr soundUrl, IntPtr localizationUrl, IntPtr alertHeader, IntPtr alertMessage, IntPtr defaultButtonTitle, IntPtr alternateButtonTitle, IntPtr otherButtonTitle, ref uint responseFlags); #endif public const int MB_RIGHT = 0x00080000, MB_RTLREADING = 0x00100000; #if !FEATURE_PAL [DllImport(ExternDll.Gdi32, CharSet = System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static extern bool GetTextMetrics(IntPtr hDC, [In, Out] NativeMethods.TEXTMETRIC tm); [DllImport(ExternDll.Gdi32, CharSet = System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern IntPtr GetStockObject(int nIndex); #endif [DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto, BestFitMapping = true)] [ResourceExposure(ResourceScope.None)] public static extern void OutputDebugString(String message); [DllImport(ExternDll.User32, CharSet = System.Runtime.InteropServices.CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, string text, string caption, int type); #if !SILVERLIGHT public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100, FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200, FORMAT_MESSAGE_FROM_STRING = 0x00000400, FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000, FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true, BestFitMapping=true)] [SuppressMessage("Microsoft.Security", "CA2101:SpecifyMarshalingForPInvokeStringArguments")] [ResourceExposure(ResourceScope.None)] public static unsafe extern int FormatMessage(int dwFlags, SafeHandle lpSource, uint dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr[] arguments); [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true, BestFitMapping=true)] [ResourceExposure(ResourceScope.None)] public static extern int FormatMessage(int dwFlags, HandleRef lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr arguments); [DllImport(ExternDll.Kernel32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [ResourceExposure(ResourceScope.None)] public static extern bool CloseHandle(IntPtr handle); [DllImport(ExternDll.Kernel32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)] [ResourceExposure(ResourceScope.None)] public static extern bool CloseHandle(HandleRef handle); [DllImport(ExternDll.Kernel32)] [ResourceExposure(ResourceScope.None)] public static extern bool QueryPerformanceCounter(out long value); [DllImport(ExternDll.Kernel32)] [ResourceExposure(ResourceScope.None)] public static extern bool QueryPerformanceFrequency(out long value); #if !FEATURE_PAL public const int FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF, FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; [DllImport(ExternDll.User32, CharSet=System.Runtime.InteropServices.CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] public static extern int RegisterWindowMessage(string msg); #if DEBUG // Used only from debug code to assert we're on the right thread // for calling certain Windows methods. [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern int GetCurrentThreadId(); [DllImport(ExternDll.User32, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.Process)] public static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId); #endif // file src\Services\Monitoring\system\Diagnosticts\SafeNativeMethods.cs [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)] [ResourceExposure(ResourceScope.Process)] public static extern IntPtr LoadLibrary(string libFilename); [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Unicode, SetLastError=true)] [ResourceExposure(ResourceScope.Process)] public static extern bool FreeLibrary(HandleRef hModule); [DllImport(ExternDll.Kernel32, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.None)] public static extern bool GetComputerName(StringBuilder lpBuffer, int[] nSize); public static unsafe int InterlockedCompareExchange(IntPtr pDestination, int exchange, int compare) { return Interlocked.CompareExchange(ref *(int *)pDestination.ToPointer(), exchange, compare); } [DllImport(ExternDll.PerfCounter, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static unsafe extern int FormatFromRawValue( uint dwCounterType, uint dwFormat, ref long pTimeBase, NativeMethods.PDH_RAW_COUNTER pRawValue1, NativeMethods.PDH_RAW_COUNTER pRawValue2, NativeMethods.PDH_FMT_COUNTERVALUE pFmtValue ); #endif // !FEATURE_PAL [DllImport(ExternDll.Kernel32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] internal static extern SafeWaitHandle CreateSemaphore(NativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes, int initialCount, int maximumCount, String name); [DllImport(ExternDll.Kernel32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)] [ResourceExposure(ResourceScope.Machine)] internal static extern SafeWaitHandle OpenSemaphore(/* DWORD */ int desiredAccess, bool inheritHandle, String name); [DllImport(ExternDll.Kernel32, SetLastError=true)] [ResourceExposure(ResourceScope.Machine)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] internal static extern bool ReleaseSemaphore(SafeWaitHandle handle, int releaseCount, out int previousCount); [DllImport(ExternDll.Kernel32, SetLastError = true)] [ResourceExposure(ResourceScope.None)] public static extern bool IsWow64Process(SafeProcessHandle hProcess, ref bool Wow64Process); [StructLayout(LayoutKind.Sequential)] internal class PROCESS_INFORMATION { // The handles in PROCESS_INFORMATION are initialized in unmanaged functions. // We can't use SafeHandle here because Interop doesn't support [out] SafeHandles in structures/classes yet. public IntPtr hProcess = IntPtr.Zero; public IntPtr hThread = IntPtr.Zero; public int dwProcessId = 0; public int dwThreadId = 0; // Note this class makes no attempt to free the handles // Use InitialSetHandle to copy to handles into SafeHandles } /* The following code has been removed to prevent FXCOP violations. The code is left here incase it needs to be resurrected. // From file src\services\timers\system\timers\safenativemethods.cs public delegate void TimerAPCProc(IntPtr argToCompletionRoutine, int timerLowValue, int timerHighValue); */ #endif //!SILVERLIGHT } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataGridPageChangedEventArgs.cs
- FaultBookmark.cs
- ThemeDictionaryExtension.cs
- COAUTHINFO.cs
- Scalars.cs
- FrameworkReadOnlyPropertyMetadata.cs
- StatusBarPanelClickEvent.cs
- StorageAssociationSetMapping.cs
- TextEvent.cs
- CustomLineCap.cs
- StateRuntime.cs
- PointHitTestResult.cs
- DataGridViewRowCollection.cs
- ServiceSecurityContext.cs
- ConfigurationErrorsException.cs
- PersonalizationDictionary.cs
- Classification.cs
- CommonRemoteMemoryBlock.cs
- HttpBrowserCapabilitiesBase.cs
- XmlHierarchyData.cs
- MessageVersionConverter.cs
- DataColumn.cs
- SystemException.cs
- AutoCompleteStringCollection.cs
- ToolStripProgressBar.cs
- NamedPermissionSet.cs
- ExpressionVisitorHelpers.cs
- EncoderParameter.cs
- BigInt.cs
- ValueTypeFixupInfo.cs
- PerformanceCountersElement.cs
- wgx_exports.cs
- TailCallAnalyzer.cs
- XXXInfos.cs
- WebPartDisplayModeCollection.cs
- XmlImplementation.cs
- WindowsSolidBrush.cs
- FontStyle.cs
- EmissiveMaterial.cs
- NameValuePermission.cs
- DiscoveryReference.cs
- WorkflowControlClient.cs
- CFGGrammar.cs
- XamlToRtfWriter.cs
- Pair.cs
- CatalogPart.cs
- MonitoringDescriptionAttribute.cs
- ConfigurationManager.cs
- PageSetupDialog.cs
- TextModifier.cs
- DefaultPrintController.cs
- UnSafeCharBuffer.cs
- PrimitiveList.cs
- ReliableChannelFactory.cs
- ToolStripActionList.cs
- smtpconnection.cs
- SQLByteStorage.cs
- MarginsConverter.cs
- Transactions.cs
- MediaContextNotificationWindow.cs
- HttpApplicationFactory.cs
- LinkLabelLinkClickedEvent.cs
- TabOrder.cs
- ToolStripItemCollection.cs
- TemplatedMailWebEventProvider.cs
- DaylightTime.cs
- ThreadAbortException.cs
- TreeViewImageGenerator.cs
- ColumnTypeConverter.cs
- VScrollProperties.cs
- Transaction.cs
- Int16Storage.cs
- StreamedWorkflowDefinitionContext.cs
- DataGridRowEventArgs.cs
- DataIdProcessor.cs
- Ref.cs
- ExceptionAggregator.cs
- MultiView.cs
- Logging.cs
- UriScheme.cs
- ClientTarget.cs
- SafeWaitHandle.cs
- CanonicalFontFamilyReference.cs
- mediaclock.cs
- SByte.cs
- VerificationException.cs
- EntityDesignerUtils.cs
- MonitoringDescriptionAttribute.cs
- Logging.cs
- MediaEntryAttribute.cs
- sqlinternaltransaction.cs
- DataGridViewUtilities.cs
- WindowsEditBoxRange.cs
- CloudCollection.cs
- ProbeMatchesApril2005.cs
- CodeObject.cs
- filewebresponse.cs
- Itemizer.cs
- SafeNativeMethods.cs
- MemberAccessException.cs