Example #1
0
        internal static bool IsLocalMachineName(string machineName)
        {
            const string comResDllName     = "ComRes.dll";
            const string englishMyComputer = "My Computer";
            const int    IDS_MYCOMPUTER    = 1824; // resource id for My Computer in ComRes.dll

            if (localizedMyComputerString == null)
            {
                // default value
                localizedMyComputerString = englishMyComputer;

#pragma warning suppress 56523
                IntPtr comResDll = SafeNativeMethods.LoadLibrary(comResDllName);
                if (comResDll != IntPtr.Zero)
                {
                    StringBuilder sb;
                    int           len, size = 16;
                    do
                    {
                        sb = new StringBuilder(size);
#pragma warning suppress 56523
                        len = SafeNativeMethods.LoadString(comResDll, IDS_MYCOMPUTER, sb, size);
                        if (len > 0 && len < size)
                        {
                            localizedMyComputerString = sb.ToString();
                            break;
                        }
                        size *= 2;
                    } while (len > 0);
#pragma warning suppress 56523
                    SafeNativeMethods.FreeLibrary(comResDll);
                }
            }

            return(string.IsNullOrEmpty(machineName) || SafeCompare(machineName, "localhost") ||
                   SafeCompare(machineName, localizedMyComputerString) || SafeCompare(machineName, LocalHostName));
        }