Esempio n. 1
0
        private static string GetEnvironmentVariableCore(string variable)
        {
            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if the variable was not found in this case.
                return(null);
            }

            StringBuilder sb           = StringBuilderCache.Acquire(128); // A somewhat reasonable default size
            int           requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);

            if (requiredSize == 0 && Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND)
            {
                StringBuilderCache.Release(sb);
                return(null);
            }

            while (requiredSize > sb.Capacity)
            {
                sb.Capacity  = requiredSize;
                sb.Length    = 0;
                requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);
            }

            return(StringBuilderCache.GetStringAndRelease(sb));
        }
Esempio n. 2
0
        // This is used by the runtime when re-throwing a managed exception.  It will
        //  copy the stack trace to _remoteStackTraceString.
        internal void InternalPreserveStackTrace()
        {
            string tmpStackTraceString;

#if FEATURE_APPX
            if (AppDomain.IsAppXModel())
            {
                // Call our internal GetStackTrace in AppX so we can parse the result should
                // we need to strip file/line info from it to make it PII-free. Calling the
                // public and overridable StackTrace getter here was probably not intended.
                tmpStackTraceString = GetStackTrace(true);

                // Make sure that the _source field is initialized if Source is not overriden.
                // We want it to contain the original faulting point.
                string source = Source;
            }
            else
#else // FEATURE_APPX
            // Preinitialize _source on CoreSystem as well. The legacy behavior is not ideal and
            // we keep it for back compat but we can afford to make the change on the Phone.
            string source = Source;
#endif // FEATURE_APPX
            {
                // Call the StackTrace getter in classic for compat.
                tmpStackTraceString = StackTrace;
            }

            if (tmpStackTraceString != null && tmpStackTraceString.Length > 0)
            {
                _remoteStackTraceString = tmpStackTraceString + Environment.NewLine;
            }

            _stackTrace       = null;
            _stackTraceString = null;
        }
Esempio n. 3
0
        private static void SetEnvironmentVariableCore(string variable, string value)
        {
            // explicitly null out value if is the empty string.
            if (string.IsNullOrEmpty(value) || value[0] == '\0')
            {
                value = null;
            }

            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // so we throw PlatformNotSupportedException.
                throw new PlatformNotSupportedException();
            }

            if (!Win32Native.SetEnvironmentVariable(variable, value))
            {
                int errorCode = Marshal.GetLastWin32Error();

                switch (errorCode)
                {
                case Win32Native.ERROR_ENVVAR_NOT_FOUND:
                    // Allow user to try to clear a environment variable
                    return;

                case Win32Native.ERROR_FILENAME_EXCED_RANGE:
                    // The error message from Win32 is "The filename or extension is too long",
                    // which is not accurate.
                    throw new ArgumentException(GetResourceString("Argument_LongEnvVarValue"));

                default:
                    throw new ArgumentException(Win32Native.GetMessage(errorCode));
                }
            }
        }
Esempio n. 4
0
        private static IDictionary GetEnvironmentVariablesCore()
        {
            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if no environment variables are defined in this case.
                return(new Dictionary <string, string>(0));
            }

            return(GetRawEnvironmentVariables());
        }
Esempio n. 5
0
 public override string ToString()
 {
     if (AppDomain.IsAppXModel())
     {
         IStringable stringable = this as IStringable;
         if (stringable != null)
         {
             return(stringable.ToString());
         }
     }
     return(base.ToString());
 }
Esempio n. 6
0
        /// <summary>
        /// Overrides ToString() to make sure we call to IStringable if the COM
        /// object implements it in the case of weakly typed RCWs
        /// </summary>
        public override string ToString()
        {
            // Only do the IStringable cast when running under AppX for better compat
            // Otherwise we could do a IStringable cast in classic apps which could introduce
            // a thread transition which would lead to deadlock.
            if (AppDomain.IsAppXModel())
            {
                // Check whether the type implements IStringable.
                if (this is IStringable stringableType)
                {
                    return(stringableType.ToString());
                }
            }

            return(base.ToString());
        }
Esempio n. 7
0
        internal static IEnumerable <KeyValuePair <string, string> > EnumerateEnvironmentVariablesFromRegistry(EnvironmentVariableTarget target)
        {
#if FEATURE_WIN32_REGISTRY
            if (AppDomain.IsAppXModel())
#endif
            {
                // Without registry support we have nothing to return
                ValidateTarget(target);
                yield break;
            }
#if FEATURE_WIN32_REGISTRY
            RegistryKey baseKey;
            string      keyName;
            if (target == EnvironmentVariableTarget.Machine)
            {
                baseKey = Registry.LocalMachine;
                keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
            }
            else if (target == EnvironmentVariableTarget.User)
            {
                Debug.Assert(target == EnvironmentVariableTarget.User);
                baseKey = Registry.CurrentUser;
                keyName = @"Environment";
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(target), target, SR.Format(SR.Arg_EnumIllegalVal, target));
            }

            using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: false))
            {
                if (environmentKey != null)
                {
                    foreach (string name in environmentKey.GetValueNames())
                    {
                        string value = environmentKey.GetValue(name, "").ToString();
                        yield return(new KeyValuePair <string, string>(name, value));
                    }
                }
            }
#endif // FEATURE_WIN32_REGISTRY
        }
        // Token: 0x060005CC RID: 1484 RVA: 0x00014A78 File Offset: 0x00012C78
        internal void InternalPreserveStackTrace()
        {
            string stackTrace;

            if (AppDomain.IsAppXModel())
            {
                stackTrace = this.GetStackTrace(true);
                string source = this.Source;
            }
            else
            {
                stackTrace = this.StackTrace;
            }
            if (stackTrace != null && stackTrace.Length > 0)
            {
                this._remoteStackTraceString = stackTrace + Environment.NewLine;
            }
            this._stackTrace       = null;
            this._stackTraceString = null;
        }
Esempio n. 9
0
        private static string GetEnvironmentVariableCore(string variable, EnvironmentVariableTarget target)
        {
            if (target == EnvironmentVariableTarget.Process)
            {
                return(GetEnvironmentVariableCore(variable));
            }

#if FEATURE_WIN32_REGISTRY
            if (AppDomain.IsAppXModel())
#endif
            {
                return(null);
            }
#if FEATURE_WIN32_REGISTRY
            RegistryKey baseKey;
            string      keyName;

            if (target == EnvironmentVariableTarget.Machine)
            {
                baseKey = Registry.LocalMachine;
                keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
            }
            else if (target == EnvironmentVariableTarget.User)
            {
                Debug.Assert(target == EnvironmentVariableTarget.User);
                baseKey = Registry.CurrentUser;
                keyName = "Environment";
            }
            else
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)target));
            }

            using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: false))
            {
                return(environmentKey?.GetValue(variable) as string);
            }
#endif
        }
Esempio n. 10
0
        private static void SetEnvironmentVariableCore(string variable, string value, EnvironmentVariableTarget target)
        {
            if (target == EnvironmentVariableTarget.Process)
            {
                SetEnvironmentVariableCore(variable, value);
                return;
            }

#if FEATURE_WIN32_REGISTRY
            if (AppDomain.IsAppXModel())
#endif
            {
                // other targets ignored
                return;
            }
#if FEATURE_WIN32_REGISTRY
            // explicitly null out value if is the empty string.
            if (string.IsNullOrEmpty(value) || value[0] == '\0')
            {
                value = null;
            }

            RegistryKey baseKey;
            string      keyName;

            if (target == EnvironmentVariableTarget.Machine)
            {
                baseKey = Registry.LocalMachine;
                keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
            }
            else if (target == EnvironmentVariableTarget.User)
            {
                Debug.Assert(target == EnvironmentVariableTarget.User);

                // User-wide environment variables stored in the registry are limited to 255 chars for the environment variable name.
                const int MaxUserEnvVariableLength = 255;
                if (variable.Length >= MaxUserEnvVariableLength)
                {
                    throw new ArgumentException(SR.Argument_LongEnvVarValue, nameof(variable));
                }

                baseKey = Registry.CurrentUser;
                keyName = "Environment";
            }
            else
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumIllegalVal, (int)target));
            }

            using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: true))
            {
                if (environmentKey != null)
                {
                    if (value == null)
                    {
                        environmentKey.DeleteValue(variable, throwOnMissingValue: false);
                    }
                    else
                    {
                        environmentKey.SetValue(variable, value);
                    }
                }
            }

            // send a WM_SETTINGCHANGE message to all windows
            IntPtr r = Win32Native.SendMessageTimeout(new IntPtr(Win32Native.HWND_BROADCAST),
                                                      Win32Native.WM_SETTINGCHANGE, IntPtr.Zero, "Environment", 0, 1000, IntPtr.Zero);

            if (r == IntPtr.Zero)
            {
                Debug.Assert(false, "SetEnvironmentVariable failed: " + Marshal.GetLastWin32Error());
            }
#endif // FEATURE_WIN32_REGISTRY
        }
Esempio n. 11
0
        public static String ExpandEnvironmentVariables(String name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            Contract.EndContractBlock();

            if (name.Length == 0)
            {
                return(name);
            }

            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if no variables are defined in this case.
                return(name);
            }

            int           currentSize = 100;
            StringBuilder blob        = new StringBuilder(currentSize); // A somewhat reasonable default size

#if PLATFORM_UNIX                                                       // Win32Native.ExpandEnvironmentStrings isn't available
            int lastPos = 0, pos;
            while (lastPos < name.Length && (pos = name.IndexOf('%', lastPos + 1)) >= 0)
            {
                if (name[lastPos] == '%')
                {
                    string key   = name.Substring(lastPos + 1, pos - lastPos - 1);
                    string value = Environment.GetEnvironmentVariable(key);
                    if (value != null)
                    {
                        blob.Append(value);
                        lastPos = pos + 1;
                        continue;
                    }
                }
                blob.Append(name.Substring(lastPos, pos - lastPos));
                lastPos = pos;
            }
            blob.Append(name.Substring(lastPos));
#else
            int size;

            blob.Length = 0;
            size        = Win32Native.ExpandEnvironmentStrings(name, blob, currentSize);
            if (size == 0)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            while (size > currentSize)
            {
                currentSize   = size;
                blob.Capacity = currentSize;
                blob.Length   = 0;

                size = Win32Native.ExpandEnvironmentStrings(name, blob, currentSize);
                if (size == 0)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
            }
#endif // PLATFORM_UNIX

            return(blob.ToString());
        }