1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
//------------------------------------------------------------------------------
// <copyright file="FileChangesMonitor.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Util {
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Web.Hosting;
using Microsoft.Win32;
internal sealed class Misc {
const string APPLICATION_ID = "\r\n\r\nApplication ID: ";
const string PROCESS_ID = "\r\n\r\nProcess ID: ";
const string EXCEPTION = "\r\n\r\nException: ";
const string INNER_EXCEPTION = "\r\n\r\nInnerException: ";
const string MESSAGE = "\r\n\r\nMessage: ";
const string STACK_TRACE = "\r\n\r\nStackTrace: ";
static StringComparer s_caseInsensitiveInvariantKeyComparer;
internal static StringComparer CaseInsensitiveInvariantKeyComparer {
get {
if (s_caseInsensitiveInvariantKeyComparer == null) {
s_caseInsensitiveInvariantKeyComparer = StringComparer.Create(CultureInfo.InvariantCulture, true);
}
return s_caseInsensitiveInvariantKeyComparer;
}
}
[SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString(System.IFormatProvider)",
Justification = "This is the proper culture for writing to the event log.")]
internal static void WriteUnhandledExceptionToEventLog(AppDomain appDomain, Exception exception) {
if (appDomain == null || exception == null) {
return;
}
ProcessImpersonationContext imperContext = null;
try {
imperContext = new ProcessImpersonationContext();
String appId = appDomain.GetData(".appId") as String;
if (appId == null) {
appId = appDomain.FriendlyName;
}
string pid = SafeNativeMethods.GetCurrentProcessId().ToString(CultureInfo.InstalledUICulture);
string description = SR.Resources.GetString(SR.Unhandled_Exception, CultureInfo.InstalledUICulture);
Misc.ReportUnhandledException(exception, new string[5] {description, APPLICATION_ID, appId, PROCESS_ID, pid});
}
catch {
// ignore exceptions so that WriteErrorToEventLog never throws
}
finally {
if (imperContext != null) {
imperContext.Undo();
}
}
}
internal static void ReportUnhandledException(Exception e, String[] strings) {
UnsafeNativeMethods.ReportUnhandledException(FormatExceptionMessage(e, strings));
}
internal static String FormatExceptionMessage(Exception e, String[] strings) {
StringBuilder sb = new StringBuilder(4096);
for (int i = 0; i < strings.Length; i++) {
sb.Append(strings[i]);
}
for (Exception current = e; current != null; current = current.InnerException) {
if (current == e)
sb.Append(EXCEPTION);
else
sb.Append(INNER_EXCEPTION);
sb.Append(current.GetType().FullName);
sb.Append(MESSAGE);
sb.Append(current.Message);
sb.Append(STACK_TRACE);
sb.Append(current.StackTrace);
}
return sb.ToString();
}
internal unsafe static void CopyMemory(IntPtr src, int srcOffset, byte[] dest, int destOffset, int size) {
//
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(src.ToInt64()+srcOffset), dest, destOffset, size);
}
internal unsafe static void CopyMemory(byte[] src, int srcOffset, IntPtr dest, int destOffset, int size) {
//
System.Runtime.InteropServices.Marshal.Copy(src, srcOffset, new IntPtr(dest.ToInt64()+destOffset), size);
}
internal unsafe static void CopyMemory(IntPtr src, int srcOffset, IntPtr dest, int destOffset, int size) {
byte *s = ((byte*)src) + srcOffset;
byte *d = ((byte*)dest) + destOffset;
StringUtil.memcpyimpl(s, d, size);
}
internal static void ThrowIfFailedHr(int hresult) {
// SUCCEEDED >= 0
// FAILED < 0
if (hresult < 0) {
Marshal.ThrowExceptionForHR(hresult);
}
}
internal static IProcessHostSupportFunctions CreateLocalSupportFunctions(IProcessHostSupportFunctions proxyFunctions) {
IProcessHostSupportFunctions localFunctions = null;
// get the underlying COM object
IntPtr pUnk = Marshal.GetIUnknownForObject(proxyFunctions);
// this object isn't a COM object
if (IntPtr.Zero == pUnk) {
return null;
}
IntPtr ppv = IntPtr.Zero;
try {
// QI it for the interface
Guid g = typeof(IProcessHostSupportFunctions).GUID;
int hresult = Marshal.QueryInterface(pUnk, ref g, out ppv);
if (hresult < 0) {
Marshal.ThrowExceptionForHR(hresult);
}
// create a RCW we can hold onto in this domain
// this bumps the ref count so we can drop our refs on the raw interfaces
localFunctions = (IProcessHostSupportFunctions)Marshal.GetObjectForIUnknown(ppv);
}
finally {
// drop our explicit refs and keep the managed instance
if (IntPtr.Zero != ppv) {
Marshal.Release(ppv);
}
if (IntPtr.Zero != pUnk) {
Marshal.Release(pUnk);
}
}
return localFunctions;
}
// Open ASP.NET's reg key, or one of its subkeys
internal static RegistryKey OpenAspNetRegKey(string subKey) {
String ver = VersionInfo.SystemWebVersion;
// Zero out minor version number VSWhidbey 602541
// Eg. 2.0.50727.42 becomes 2.0.50727.0
if (!string.IsNullOrEmpty(ver)) {
int pos = ver.LastIndexOf('.');
if (pos > -1) {
ver = ver.Substring(0, pos + 1) + "0";
}
}
// The main ASP.NET reg key
string key = @"Software\Microsoft\ASP.NET\" + ver;
// If we're asked for a subkey, append it
if (subKey != null)
key += @"\" + subKey;
// Open and return the key
return Registry.LocalMachine.OpenSubKey(key);
}
// Get an ASP.NET registry value, from the main key or a subkey
// The Registry class does a full demand, so we'll turn it into a LinkDemand
[RegistryPermission(SecurityAction.LinkDemand, Unrestricted = true)]
[RegistryPermission(SecurityAction.Assert, Unrestricted = true)]
internal static object GetAspNetRegValue(string subKey, string valueName, object defaultValue) {
try {
using (RegistryKey regKey = OpenAspNetRegKey(subKey)) {
// Return the default value if the key doesn't exist
if (regKey == null)
return defaultValue;
return regKey.GetValue(valueName, defaultValue);
}
}
catch {
// Return the default value if anything goes wrong
return defaultValue;
}
}
}
}
|