[corlib] RemotingConfiguration now try to use the bundled machine.config first.
[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 #if !MOBILE
101             //
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
106             // used
107             //
108             var sdk = Environment.GetEnvironmentVariable ("CSC_SDK_PATH_DISABLED");
109             if (sdk != null)
110                 return null;
111 #endif
112             String dir = GetRuntimeDirectoryImpl();
113 #if FEATURE_MONO_CAS
114             new FileIOPermission(FileIOPermissionAccess.PathDiscovery, dir).Demand();
115 #endif
116             return dir;
117         }
118
119 #if MONO
120         static String GetRuntimeDirectoryImpl()
121         {
122             return Path.GetDirectoryName (typeof (object).Assembly.Location);
123         }
124 #else
125         [System.Security.SecurityCritical]  // auto-generated
126         [ResourceExposure(ResourceScope.Machine)]
127         [MethodImplAttribute(MethodImplOptions.InternalCall)]
128         internal static extern String GetRuntimeDirectoryImpl();
129 #endif
130         
131         // Returns the system ConfigurationFile
132         public static String SystemConfigurationFile {
133             [System.Security.SecuritySafeCritical]  // auto-generated
134             [ResourceExposure(ResourceScope.Machine)]
135             [ResourceConsumption(ResourceScope.Machine)]
136             get {
137 #if MONO
138                 String path = Environment.GetMachineConfigPath ();
139 #else
140                 StringBuilder sb = new StringBuilder(Path.MAX_PATH);
141                 sb.Append(GetRuntimeDirectory());
142                 sb.Append(AppDomainSetup.RuntimeConfigurationFile);
143                 String path = sb.ToString();
144 #endif
145                 
146 #if FEATURE_MONO_CAS
147                 // Do security check
148                 new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
149 #endif
150                 return path;
151             }
152         }
153
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);
163
164         //
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.
168         //
169         // Returns unmanaged pointer to requested interface on success. Throws
170         // COMException with failed HR if there is a QI failure.
171         //
172         [System.Security.SecurityCritical]  // do not allow partial trust callers
173         [ComVisible(false)]
174         [ResourceExposure(ResourceScope.Process)]
175         [ResourceConsumption(ResourceScope.Process)]
176         public static IntPtr GetRuntimeInterfaceAsIntPtr(Guid clsid, Guid riid)
177         {
178             return GetRuntimeInterfaceImpl(clsid, riid);
179         }
180
181         //
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.
185         //
186         // Returns an RCW to requested interface on success. Throws
187         // COMException with failed HR if there is a QI failure.
188         //
189         [System.Security.SecurityCritical]  // do not allow partial trust callers
190         [ComVisible(false)]
191         [ResourceExposure(ResourceScope.Process)]
192         [ResourceConsumption(ResourceScope.Process)]
193         public static object GetRuntimeInterfaceAsObject(Guid clsid, Guid riid)
194         {
195             IntPtr p = IntPtr.Zero;
196             try {
197                 p = GetRuntimeInterfaceImpl(clsid, riid);
198                 return Marshal.GetObjectForIUnknown(p);
199             } finally {
200                 if(p != IntPtr.Zero) {
201                     Marshal.Release(p);
202                 }
203             }
204         }
205
206 #endif // FEATURE_COMINTEROP
207     }
208 }