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()
102 // Workaround for csc hardcoded behaviour where executing mscorlib
103 // location is always the first path to search for references unless
104 // they have full path. Mono build is using simple assembly names for
105 // references and -lib for path which is by default csc dehaviour never
108 var sdk = Environment.GetEnvironmentVariable ("CSC_SDK_PATH_DISABLED");
112 String dir = GetRuntimeDirectoryImpl();
114 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, dir).Demand();
120 static String GetRuntimeDirectoryImpl()
122 return Path.GetDirectoryName (typeof (object).Assembly.Location);
125 [System.Security.SecurityCritical] // auto-generated
126 [ResourceExposure(ResourceScope.Machine)]
127 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128 internal static extern String GetRuntimeDirectoryImpl();
131 // Returns the system ConfigurationFile
132 public static String SystemConfigurationFile {
133 [System.Security.SecuritySafeCritical] // auto-generated
134 [ResourceExposure(ResourceScope.Machine)]
135 [ResourceConsumption(ResourceScope.Machine)]
138 String path = Environment.GetMachineConfigPath ();
140 StringBuilder sb = new StringBuilder(Path.MAX_PATH);
141 sb.Append(GetRuntimeDirectory());
142 sb.Append(AppDomainSetup.RuntimeConfigurationFile);
143 String path = sb.ToString();
148 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
154 #if FEATURE_COMINTEROP && !MONO
155 [System.Security.SecurityCritical]
156 [ResourceExposure(ResourceScope.Process)]
157 [ResourceConsumption(ResourceScope.Process)]
158 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
159 [SuppressUnmanagedCodeSecurity]
160 private static extern IntPtr GetRuntimeInterfaceImpl(
161 [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
162 [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
165 // This function does the equivalent of calling GetInterface(clsid, riid) on the
166 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
167 // CLSIDs and IIDs supported by this method.
169 // Returns unmanaged pointer to requested interface on success. Throws
170 // COMException with failed HR if there is a QI failure.
172 [System.Security.SecurityCritical] // do not allow partial trust callers
174 [ResourceExposure(ResourceScope.Process)]
175 [ResourceConsumption(ResourceScope.Process)]
176 public static IntPtr GetRuntimeInterfaceAsIntPtr(Guid clsid, Guid riid)
178 return GetRuntimeInterfaceImpl(clsid, riid);
182 // This function does the equivalent of calling GetInterface(clsid, riid) on the
183 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
184 // CLSIDs and IIDs supported by this method.
186 // Returns an RCW to requested interface on success. Throws
187 // COMException with failed HR if there is a QI failure.
189 [System.Security.SecurityCritical] // do not allow partial trust callers
191 [ResourceExposure(ResourceScope.Process)]
192 [ResourceConsumption(ResourceScope.Process)]
193 public static object GetRuntimeInterfaceAsObject(Guid clsid, Guid riid)
195 IntPtr p = IntPtr.Zero;
197 p = GetRuntimeInterfaceImpl(clsid, riid);
198 return Marshal.GetObjectForIUnknown(p);
200 if(p != IntPtr.Zero) {
206 #endif // FEATURE_COMINTEROP