3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*=============================================================================
8 ** Class: RuntimeEnvironment
11 ** Purpose: Runtime information
14 =============================================================================*/
19 using System.Runtime.CompilerServices;
20 using System.Runtime.InteropServices;
21 using System.Security;
22 using System.Security.Permissions;
23 using System.Reflection;
24 using Microsoft.Win32;
25 using System.Runtime.Versioning;
26 using StackCrawlMark = System.Threading.StackCrawlMark;
28 namespace System.Runtime.InteropServices {
29 [System.Runtime.InteropServices.ComVisible(true)]
33 public class RuntimeEnvironment {
36 // This should have been a static class, but wasn't as of v3.5. Clearly, this is
37 // broken. We'll keep this in V4 for binary compat, but marked obsolete as error
38 // so migrated source code gets fixed. On Silverlight, this type exists but is
40 [Obsolete("Do not create instances of the RuntimeEnvironment class. Call the static methods directly on this type instead", true)]
41 public RuntimeEnvironment()
43 // Should not have been instantiable - here for binary compatibility in V4.
47 [System.Security.SecurityCritical] // auto-generated
48 [ResourceExposure(ResourceScope.Machine)]
49 [MethodImplAttribute(MethodImplOptions.InternalCall)]
50 internal static extern String GetModuleFileName();
52 [System.Security.SecurityCritical] // auto-generated
53 [ResourceExposure(ResourceScope.Machine)]
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 internal static extern String GetDeveloperPath();
57 [System.Security.SecurityCritical] // auto-generated
58 [ResourceExposure(ResourceScope.Machine)]
59 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60 internal static extern String GetHostBindingFile();
62 #if !FEATURE_CORECLR && !MONO
63 [System.Security.SecurityCritical] // auto-generated
64 [ResourceExposure(ResourceScope.None)]
65 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
66 [SuppressUnmanagedCodeSecurity]
67 internal static extern void _GetSystemVersion(StringHandleOnStack retVer);
68 #endif //!FEATURE_CORECLR
70 public static bool FromGlobalAccessCache(Assembly a)
72 return a.GlobalAssemblyCache;
76 [System.Security.SecuritySafeCritical] // public member
78 [MethodImpl (MethodImplOptions.NoInlining)]
79 public static String GetSystemVersion()
81 #if FEATURE_CORECLR || MONO
83 return Assembly.GetExecutingAssembly().ImageRuntimeVersion;
85 #else // FEATURE_CORECLR
88 _GetSystemVersion(JitHelpers.GetStringHandleOnStack(ref ver));
91 #endif // FEATURE_CORECLR
95 [System.Security.SecuritySafeCritical] // auto-generated
96 [ResourceExposure(ResourceScope.Machine)]
97 [ResourceConsumption(ResourceScope.Machine)]
98 public static String GetRuntimeDirectory()
100 String dir = GetRuntimeDirectoryImpl();
101 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, dir).Demand();
106 static String GetRuntimeDirectoryImpl()
108 return Path.GetDirectoryName (typeof (object).Assembly.Location);
111 [System.Security.SecurityCritical] // auto-generated
112 [ResourceExposure(ResourceScope.Machine)]
113 [MethodImplAttribute(MethodImplOptions.InternalCall)]
114 internal static extern String GetRuntimeDirectoryImpl();
117 // Returns the system ConfigurationFile
118 public static String SystemConfigurationFile {
119 [System.Security.SecuritySafeCritical] // auto-generated
120 [ResourceExposure(ResourceScope.Machine)]
121 [ResourceConsumption(ResourceScope.Machine)]
124 String path = Environment.GetMachineConfigPath ();
126 StringBuilder sb = new StringBuilder(Path.MAX_PATH);
127 sb.Append(GetRuntimeDirectory());
128 sb.Append(AppDomainSetup.RuntimeConfigurationFile);
129 String path = sb.ToString();
133 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
139 #if FEATURE_COMINTEROP && !MONO
140 [System.Security.SecurityCritical]
141 [ResourceExposure(ResourceScope.Process)]
142 [ResourceConsumption(ResourceScope.Process)]
143 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
144 [SuppressUnmanagedCodeSecurity]
145 private static extern IntPtr GetRuntimeInterfaceImpl(
146 [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
147 [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
150 // This function does the equivalent of calling GetInterface(clsid, riid) on the
151 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
152 // CLSIDs and IIDs supported by this method.
154 // Returns unmanaged pointer to requested interface on success. Throws
155 // COMException with failed HR if there is a QI failure.
157 [System.Security.SecurityCritical] // do not allow partial trust callers
159 [ResourceExposure(ResourceScope.Process)]
160 [ResourceConsumption(ResourceScope.Process)]
161 public static IntPtr GetRuntimeInterfaceAsIntPtr(Guid clsid, Guid riid)
163 return GetRuntimeInterfaceImpl(clsid, riid);
167 // This function does the equivalent of calling GetInterface(clsid, riid) on the
168 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
169 // CLSIDs and IIDs supported by this method.
171 // Returns an RCW to requested interface on success. Throws
172 // COMException with failed HR if there is a QI failure.
174 [System.Security.SecurityCritical] // do not allow partial trust callers
176 [ResourceExposure(ResourceScope.Process)]
177 [ResourceConsumption(ResourceScope.Process)]
178 public static object GetRuntimeInterfaceAsObject(Guid clsid, Guid riid)
180 IntPtr p = IntPtr.Zero;
182 p = GetRuntimeInterfaceImpl(clsid, riid);
183 return Marshal.GetObjectForIUnknown(p);
185 if(p != IntPtr.Zero) {
191 #endif // FEATURE_COMINTEROP