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
156 private static IntPtr GetRuntimeInterfaceImpl(Guid clsid, Guid riid)
158 throw new NotSupportedException();
161 [System.Security.SecurityCritical]
162 [ResourceExposure(ResourceScope.Process)]
163 [ResourceConsumption(ResourceScope.Process)]
164 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
165 [SuppressUnmanagedCodeSecurity]
166 private static extern IntPtr GetRuntimeInterfaceImpl(
167 [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
168 [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
172 // This function does the equivalent of calling GetInterface(clsid, riid) on the
173 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
174 // CLSIDs and IIDs supported by this method.
176 // Returns unmanaged pointer to requested interface on success. Throws
177 // COMException with failed HR if there is a QI failure.
179 [System.Security.SecurityCritical] // do not allow partial trust callers
181 [ResourceExposure(ResourceScope.Process)]
182 [ResourceConsumption(ResourceScope.Process)]
183 public static IntPtr GetRuntimeInterfaceAsIntPtr(Guid clsid, Guid riid)
185 return GetRuntimeInterfaceImpl(clsid, riid);
189 // This function does the equivalent of calling GetInterface(clsid, riid) on the
190 // ICLRRuntimeInfo representing this runtime. See MetaHost.idl for a list of
191 // CLSIDs and IIDs supported by this method.
193 // Returns an RCW to requested interface on success. Throws
194 // COMException with failed HR if there is a QI failure.
196 [System.Security.SecurityCritical] // do not allow partial trust callers
198 [ResourceExposure(ResourceScope.Process)]
199 [ResourceConsumption(ResourceScope.Process)]
200 public static object GetRuntimeInterfaceAsObject(Guid clsid, Guid riid)
202 IntPtr p = IntPtr.Zero;
204 p = GetRuntimeInterfaceImpl(clsid, riid);
205 return Marshal.GetObjectForIUnknown(p);
207 if(p != IntPtr.Zero) {
213 #endif // FEATURE_COMINTEROP