misc‎ > ‎h3‎ > ‎H3Code‎ > ‎

MSHelpUtils.cs

Extracted by Reftector - Help Viewer 1.1
HlpViewer.exe\Microsoft\Help\PowerTool\Utilities\MSHelpUtil.cs

Lots of basic commands - Of interest is = IsHelpOffline()


namespace Microsoft.Help.PowerTool.Utilities
{
    using Microsoft.Help.Common;
    using Microsoft.Win32;
    using System;
    using System.Configuration;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;

    internal static class MSHelpUtil
    {
        private static string cachedHost = "";
        private const string HelpLibAgentName = "HelpLibAgent.exe";
        private const string HelpLibAgentProcessName = "HelpLibAgent";
        private const string HelpLibManagerName = "HelpLibManager.exe";
        private const string HelpRegKey = @"Software\Microsoft\Help\v1.0";
        private static int s_SessionNumber = -1;

        public static Uri BuildMsdnStartParam(string baseOfflineUrl)
        {
            if (string.IsNullOrEmpty(baseOfflineUrl))
            {
                return null;
            }
            return new Uri(string.Concat(new object[] { baseOfflineUrl, "method=page&id=-1&product=VS&productVersion=100&locale=", CultureInfo.CurrentUICulture, "&format=html&embedded=true" }));
        }

        public static int FindHelpLibAgent()
        {
            Process[] processesByName = Process.GetProcessesByName("HelpLibAgent");
            if (processesByName.Count<Process>() != 0)
            {
                int sessionNumber = GetSessionNumber();
                foreach (Process process in processesByName)
                {
                    if (process.SessionId == sessionNumber)
                    {
                        return process.Id;
                    }
                }
            }
            return 0;
        }

        public static string GetBaseOfflineUrl(int pid)
        {
            if (pid <= 0)
            {
                return string.Empty;
            }
            return (baseOfflineUrl + GetSessionNumber().ToString() + "-" + pid.ToString() + "/ms.help?");
        }

        private static string GetHelpAppRoot()
        {
            string str = string.Empty;
            RegistryKey key = null;
            RegistryKey key2 = null;
            try
            {
                RegistryView view = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
                key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
                if (key != null)
                {
                    key2 = key.OpenSubKey(@"Software\Microsoft\Help\v1.0");
                    if (key2 != null)
                    {
                        string str2 = (string) key2.GetValue("AppRoot");
                        if (str2 != null)
                        {
                            str = str2;
                        }
                    }
                }
            }
            finally
            {
                if (key2 != null)
                {
                    key2.Close();
                }
                if (key != null)
                {
                    key.Close();
                }
            }
            return str;
        }

        public static string GetHelpLibManagerFilePath()
        {
            return Path.Combine(GetHelpAppRoot(), "HelpLibManager.exe");
        }

        private static int GetSessionNumber()
        {
            if (s_SessionNumber != -1)
            {
                return s_SessionNumber;
            }
            IntPtr zero = IntPtr.Zero;
            int num = 0;
            try
            {
                uint num2;
                if (Microsoft.Help.PowerTool.Utilities.NativeMethods.WTSQuerySessionInformation(Microsoft.Help.PowerTool.Utilities.NativeMethods.WTS_CURRENT_SERVER_HANDLE, Microsoft.Help.PowerTool.Utilities.NativeMethods.WTS_CURRENT_SESSION, Microsoft.Help.PowerTool.Utilities.NativeMethods.WTSInfoClass.WTSSessionId, out zero, out num2))
                {
                    num = Marshal.ReadInt32(zero);
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Microsoft.Help.PowerTool.Utilities.NativeMethods.WTSFreeMemory(zero);
                }
            }
            s_SessionNumber = num;
            return num;
        }

        public static bool HelpLibAgentReady(int pid)
        {
            return MutexExists("Microsoft.Help.DdeReady." + pid.ToString());
        }

        public static bool IsHelpOffline()
        {
            bool flag = false;
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\HelpLibrary\HelpClient.cfg");
            if (!File.Exists(path))
            {
                path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "HelpClient.cfg");
            }
            if (!File.Exists(path))
            {
                return flag;
            }
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap {
                ExeConfigFilename = path
            };
            System.Configuration.Configuration configuration = null;
            try
            {
                configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            }
            catch (ConfigurationErrorsException)
            {
                Thread.Sleep(0x3e8);
                configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            }
            if (configuration == null)
            {
                return flag;
            }
            AppSettingsSection section = (AppSettingsSection) configuration.GetSection("appSettings");
            if (section.Settings["helpMode"].Value.Equals("online", StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }
            return true;
        }

        public static bool IsValidMsHelpQuery(string url)
        {
            bool flag;
            if (url.IndexOf("/ms.help?", StringComparison.OrdinalIgnoreCase) < 0)
            {
                return false;
            }
            try
            {
                Uri uri = new Uri(url);
                if (!uri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
                {
                    goto Label_0095;
                }
                if (Host.Equals("127.0.0.1", StringComparison.Ordinal))
                {
                    if (uri.Host.Equals(Host, StringComparison.Ordinal))
                    {
                        return true;
                    }
                    goto Label_0095;
                }
                if (!Host.Equals("[::1]", StringComparison.Ordinal) || (!uri.Host.Equals(Host, StringComparison.Ordinal) && !uri.Host.Equals("[0000:0000:0000:0000:0000:0000:0000:0001]", StringComparison.Ordinal)))
                {
                    goto Label_0095;
                }
                flag = true;
            }
            catch (UriFormatException)
            {
            }
            return flag;
        Label_0095:
            return false;
        }

        public static bool MutexExists(string mutexName)
        {
            bool flag = false;
            Mutex mutex = null;
            try
            {
                mutex = Mutex.OpenExisting(mutexName);
                flag = true;
            }
            catch (WaitHandleCannotBeOpenedException)
            {
            }
            catch (UnauthorizedAccessException)
            {
                flag = true;
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.Close();
                    mutex = null;
                }
            }
            return flag;
        }

        public static int StartHelpLibAgent()
        {
            return Process.Start(Path.Combine(GetHelpAppRoot(), "HelpLibAgent.exe")).Id;
        }

        public static string baseOfflineUrl
        {
            get
            {
                return ("http://" + Host + ":47873/help/");
            }
        }

        public static string Host
        {
            get
            {
                if (string.IsNullOrEmpty(cachedHost))
                {
                    cachedHost = "127.0.0.1";
                    if (AppConfigUtility.GetSetting("IPV6", "HlpViewer.exe.config") == "1")
                    {
                        cachedHost = "[::1]";
                    }
                }
                return cachedHost;
            }
        }
    }
}



Comments