GetPrivateProfileString() private method

private GetPrivateProfileString ( string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath ) : int
Section string
Key string
Default string
RetVal StringBuilder
Size int
FilePath string
return int
Exemplo n.º 1
0
        public void LoadEntry(SettingsEntry entry, string section, string key)
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    Logger.Warn("Failed to acquire settings lock");
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                // Ignore; this might be a previous instance that crashed
            }

            try
            {
                const int len          = 255;
                var       must_migrate = false;
                var       tmp          = new StringBuilder(len);
                var       result       = NativeMethods.GetPrivateProfileString(section, key, entry.ToString(),
                                                                               tmp, len, FullPath);
                if (result == 0)
                {
                    // Compatibility code for keys that moved from the "global"
                    // to the "composing" or "tweaks" section.
                    if (section != "global")
                    {
                        result = NativeMethods.GetPrivateProfileString("global", key, entry.ToString(),
                                                                       tmp, len, FullPath);
                        if (result == 0)
                        {
                            return;
                        }
                        must_migrate = true;
                    }
                }

                // This may throw, but will be caught gracefully
                entry.LoadString(tmp.ToString());

                if (must_migrate)
                {
                    NativeMethods.WritePrivateProfileString("global", key, null,
                                                            FullPath);
                    NativeMethods.WritePrivateProfileString(section, key, entry.ToString(),
                                                            FullPath);
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex, $"Failed to load settings entry {section}.{key}");
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Exemplo n.º 2
0
        private static void LoadEntry(SettingsEntry entry, string section, string key)
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                /* Ignore; this might be a previous instance that crashed */
            }

            try
            {
                const int len    = 255;
                var       tmp    = new StringBuilder(len);
                var       result = NativeMethods.GetPrivateProfileString(section, key, "",
                                                                         tmp, len, GetConfigFile());
                if (result == 0)
                {
                    return;
                }
                entry.LoadString(tmp.ToString());
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Exemplo n.º 3
0
        private static string LoadEntry(string key)
        {
            const int len = 255;
            var       tmp = new StringBuilder(len);

            NativeMethods.GetPrivateProfileString("global", key, "",
                                                  tmp, len, GetConfigFile());
            return(tmp.ToString());
        }
Exemplo n.º 4
0
        private static void LoadEntry(SettingsEntry entry, string section, string key)
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    return;
                }
            }
            catch (AbandonedMutexException)
            {
                /* Ignore; this might be a previous instance that crashed */
            }

            try
            {
                const int len      = 255;
                var       migrated = false;
                var       tmp      = new StringBuilder(len);
                var       result   = NativeMethods.GetPrivateProfileString(section, key, "",
                                                                           tmp, len, GetConfigFile());
                if (result == 0)
                {
                    // Compatibility code for keys that moved from the "global"
                    // to the "composing" or "tweaks" section.
                    if (section != "global")
                    {
                        result = NativeMethods.GetPrivateProfileString("global", key, "",
                                                                       tmp, len, GetConfigFile());
                        if (result == 0)
                        {
                            return;
                        }
                        migrated = true;
                    }
                }

                entry.LoadString(tmp.ToString());

                if (migrated)
                {
                    NativeMethods.WritePrivateProfileString("global", key, null,
                                                            GetConfigFile());
                    NativeMethods.WritePrivateProfileString(section, key, entry.ToString(),
                                                            GetConfigFile());
                }
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads this settings entry from the settings file. This operation
        /// is thread-safe and process-safe.
        /// </summary>
        /// <returns>A <see cref="bool"/> indicating whether the operation
        /// was successful.</returns>
        public bool Load()
        {
            try
            {
                if (!m_mutex.WaitOne(2000))
                {
                    return(false);
                }
            }
            catch (AbandonedMutexException)
            {
                /* Ignore; this might be a previous instance that crashed */
            }

            try
            {
                const int len           = 255;
                var       stringBuilder = new StringBuilder(len);
                var       result        = NativeMethods.GetPrivateProfileString(Section, Key, "", stringBuilder, len, Settings.GetConfigFile());
                if (result == 0)
                {
                    return(false);
                }

                var strVal = stringBuilder.ToString();
                m_value = Deserialize(strVal);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                // Ensure the mutex is always released even if an
                // exception is thrown
                m_mutex.ReleaseMutex();
            }
        }