e1a5ec44f728dd54b9761e09248f3b3a2951276f
[mono.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / runtimeenvironment.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*=============================================================================
7 **
8 ** Class: RuntimeEnvironment
9 **
10 **
11 ** Purpose: Runtime information
12 **          
13 **
14 =============================================================================*/
15
16 using System;
17 using System.Text;
18 using System.IO;
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;
27
28 namespace System.Runtime.InteropServices {
29 [System.Runtime.InteropServices.ComVisible(true)]
30 #if FEATURE_CORECLR
31     static
32 #endif
33     public class RuntimeEnvironment {
34
35 #if !FEATURE_CORECLR
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
39         // not public.
40         [Obsolete("Do not create instances of the RuntimeEnvironment class.  Call the static methods directly on this type instead", true)]
41         public RuntimeEnvironment()
42         {
43             // Should not have been instantiable - here for binary compatibility in V4.
44         }
45 #endif
46 #if !MONO
47         [System.Security.SecurityCritical]  // auto-generated
48         [ResourceExposure(ResourceScope.Machine)]
49         [MethodImplAttribute(MethodImplOptions.InternalCall)]
50         internal static extern String GetModuleFileName();
51
52         [System.Security.SecurityCritical]  // auto-generated
53         [ResourceExposure(ResourceScope.Machine)]
54         [MethodImplAttribute(MethodImplOptions.InternalCall)]
55         internal static extern String GetDeveloperPath();
56
57         [System.Security.SecurityCritical]  // auto-generated
58         [ResourceExposure(ResourceScope.Machine)]
59         [MethodImplAttribute(MethodImplOptions.InternalCall)]
60         internal static extern String GetHostBindingFile();
61 #endif
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
69
70         public static bool FromGlobalAccessCache(Assembly a)
71         {
72             return a.GlobalAssemblyCache;
73         }
74         
75 #if !FEATURE_CORECLR
76         [System.Security.SecuritySafeCritical] // public member
77 #endif
78         [MethodImpl (MethodImplOptions.NoInlining)]
79         public static String GetSystemVersion()
80         {
81 #if FEATURE_CORECLR || MONO
82
83             return Assembly.GetExecutingAssembly().ImageRuntimeVersion;
84
85 #else // FEATURE_CORECLR
86
87             String ver = null;
88             _GetSystemVersion(JitHelpers.GetStringHandleOnStack(ref ver));
89             return ver;
90
91 #endif // FEATURE_CORECLR
92
93         }
94         
95         [System.Security.SecuritySafeCritical]  // auto-generated
96         [ResourceExposure(ResourceScope.Machine)]
97         [ResourceConsumption(ResourceScope.Machine)]
98         public static String GetRuntimeDirectory()
99         {
100             String dir = GetRuntimeDirectoryImpl();
101             new FileIOPermission(FileIOPermissionAccess.PathDiscovery, dir).Demand();
102             return dir;
103         }
104
105 #if MONO
106         static String GetRuntimeDirectoryImpl()
107         {
108             return Path.GetDirectoryName (typeof (object).Assembly.Location);
109         }
110 #else
111         [System.Security.SecurityCritical]  // auto-generated
112         [ResourceExposure(ResourceScope.Machine)]
113         [MethodImplAttribute(MethodImplOptions.InternalCall)]
114         internal static extern String GetRuntimeDirectoryImpl();
115 #endif
116         
117         // Returns the system ConfigurationFile
118         public static String SystemConfigurationFile {
119             [System.Security.SecuritySafeCritical]  // auto-generated
120             [ResourceExposure(ResourceScope.Machine)]
121             [ResourceConsumption(ResourceScope.Machine)]
122             get {
123 #if MONO
124                 String path = Environment.GetMachineConfigPath ();
125 #else
126                 StringBuilder sb = new StringBuilder(Path.MAX_PATH);
127                 sb.Append(GetRuntimeDirectory());
128                 sb.Append(AppDomainSetup.RuntimeConfigurationFile);
129                 String path = sb.ToString();
130 #endif
131                 
132                 // Do security check
133                 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
134
135                 return path;
136             }
137         }
138
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);
148
149         //
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.
153         //
154         // Returns unmanaged pointer to requested interface on success. Throws
155         // COMException with failed HR if there is a QI failure.
156         //
157         [System.Security.SecurityCritical]  // do not allow partial trust callers
158         [ComVisible(false)]
159         [ResourceExposure(ResourceScope.Process)]
160         [ResourceConsumption(ResourceScope.Process)]
161         public static IntPtr GetRuntimeInterfaceAsIntPtr(Guid clsid, Guid riid)
162         {
163             return GetRuntimeInterfaceImpl(clsid, riid);
164         }
165
166         //
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.
170         //
171         // Returns an RCW to requested interface on success. Throws
172         // COMException with failed HR if there is a QI failure.
173         //
174         [System.Security.SecurityCritical]  // do not allow partial trust callers
175         [ComVisible(false)]
176         [ResourceExposure(ResourceScope.Process)]
177         [ResourceConsumption(ResourceScope.Process)]
178         public static object GetRuntimeInterfaceAsObject(Guid clsid, Guid riid)
179         {
180             IntPtr p = IntPtr.Zero;
181             try {
182                 p = GetRuntimeInterfaceImpl(clsid, riid);
183                 return Marshal.GetObjectForIUnknown(p);
184             } finally {
185                 if(p != IntPtr.Zero) {
186                     Marshal.Release(p);
187                 }
188             }
189         }
190
191 #endif // FEATURE_COMINTEROP
192     }
193 }