Пример #1
0
        static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                uint lpType = 0;
                uint lpcbData = 1024;
                StringBuilder AgeBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, AgeBuffer, ref lpcbData);
                string Age = AgeBuffer.ToString();
                return Age;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
Пример #2
0
        public static string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int) RegSAM.QueryValue | (int) in32or64key, out hkey);
                if (0 != lResult) return null;
                RegistryValueKind lpType = 0;
                uint lpcbData = uint.MaxValue;
                var strBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, strBuffer, ref lpcbData);
                string value = strBuffer.ToString();
                return value;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
Пример #3
0
	static public string getRegKeyValue( UIntPtr root_key, String key_name, RegSAM is32or64key, String inPropertyName)
	{
		int phkResult = 0;

		try
		{
			uint lResult = RegOpenKeyEx( root_key, key_name, 0, (int) RegSAM.QueryValue | (int) is32or64key, out phkResult );
			if ( lResult != 0 )
				return null;
			uint lpType = 0;
			uint lpcbData = 1024;
			StringBuilder value_buffer = new StringBuilder( 1024 );
			RegQueryValueEx( phkResult, inPropertyName, 0, ref lpType, value_buffer, ref lpcbData );
			string value = value_buffer.ToString();
			return value;
		}
		finally
		{
			if ( phkResult != 0 )
				RegCloseKey( phkResult );
		}
	}
Пример #4
0
        public static string[] GetSubKeyNames64(UIntPtr inHive, String inKeyName, RegSAM in32or64key)
        {
            //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0,
                                            (int) RegSAM.QueryValue | (int) in32or64key | (int) RegSAM.EnumerateSubKeys,
                                            out hkey);
                if (0 != lResult) return null;
                int lpcbData = int.MaxValue;
                int subkeys = 0;
                int namebuffer = 0;
                RegQueryInfoKey(hkey, null, ref lpcbData, 0, ref subkeys, ref namebuffer, 0, 0, 0, 0, 0, 0);
                lpcbData = namebuffer;
                var subkeynames = new string[subkeys];
                var strBuffer = new StringBuilder(namebuffer);
                for (int i = 0; i < subkeys; i++)
                {
                    RegEnumKeyEx(hkey, i, strBuffer, ref lpcbData, 0, 0, 0, 0);
                    lpcbData = namebuffer;
                    subkeynames[i] = strBuffer.ToString();
                    strBuffer.Clear();
                }
                return subkeynames;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
Пример #5
0
 public static extern int RegCreateKeyEx(
     UIntPtr hKey,
     string lpSubKey,
     int Reserved,
     string lpClass,
     RegOption dwOptions,
     RegSAM samDesired,
     IntPtr lpSecurityAttributes,
     out IntPtr phkResult,
     out RegResult lpdwDisposition);
Пример #6
0
 private static extern int RegOpenKeyEx(
     IntPtr hKey,
     string subKey,
     uint options,
     RegSAM sam,
     out IntPtr phkResult);
Пример #7
0
 private static extern int RegLoadAppKey(String hiveFile, out int hKey, RegSAM samDesired, int options, int reserved);
Пример #8
0
        /// <summary>
        /// Method to retrieve the data for the specified value name associated with the opened registry key.
        /// </summary>
        /// <param name="inHive">i.e. RegHive.HKEY_LOCAL_MACHINE</param>
        /// <param name="inKeyName"></param>
        /// <param name="in32or64key"></param>
        /// <param name="keyName">the name of the key to search or empty value to check for the existance of the key</param>
        /// <returns>
        /// the method returns null if the registry key doesn't exists
        /// if we don't specify keyName the method returns "value not set" if the key exists.
        /// if we specify a keyName the keyName value is returned
        /// </returns>
        public static string GetRegKey(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String keyName)
        {
            int keyHandle = 0;
            try
            {
                uint result = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out keyHandle);
                if (keyName == "(Default)" && result == 0)
                    return "value not set";
                if (result != 0)
                    return null;

                RegistryValueKind type = RegistryValueKind.Unknown;
                int size = 1024;
                IntPtr value = Marshal.AllocHGlobal(size);
                int retVal = RegQueryValueEx(keyHandle, keyName, 0, ref type, value, ref size);

                switch (type)
                {
                    case RegistryValueKind.String:
                        return Marshal.PtrToStringAnsi(value);
                  case RegistryValueKind.ExpandString:
                        return Marshal.PtrToStringAnsi(value);
                    case RegistryValueKind.DWord:
                        return Marshal.ReadInt32(value).ToString();
                    case RegistryValueKind.QWord:
                        return Marshal.ReadInt64(value).ToString();
                }
                return null;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                return null;
            }
            finally
            {
                if (keyHandle != 0)
                    RegCloseKey(keyHandle);
            }
        }
			internal static extern int RegOpenKeyEx(HKEY hKey, string subKey, uint options, RegSAM samDesired, out HKEY hKeyResult);
Пример #10
0
        /*
        static public string GetFirstKeyName(UIntPtr inHive, String inKeyName, RegSAM in32or64key)
        {
            //A handle to an open registry key.
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                //uint lResult = RegEnumKeyEx(inHive, inKeyName, 0, (int)RegSAM.EnumerateSubKeys, out hkey);
                if (lResult != 0)
                    return null;
                //A pointer to a buffer that receives the name of the subkey, including the terminating null character.
                StringBuilder valueName = new StringBuilder(1024);
                //A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in characters.
                uint lpcchValueName = 1024;
                int result = RegEnumValue(hkey, 0, valueName, ref lpcchValueName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                string retVal = valueName.ToString();
                return retVal;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                return null;
            }
            finally
            {
                if (hkey != 0)
                    RegCloseKey(hkey);
            }
        }*/
        /// <summary>
        /// Method to enumerates the values for the specified open registry key.
        /// </summary>
        /// <param name="inHive">Registry Hive to look up</param>
        /// <param name="inKeyName">Path to registry key</param>
        /// <param name="in32or64key">Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.</param>
        /// <param name="getOnlyFirstValue">Enumerate only the first registry value - used in ProductInfo.getProductCode()</param>
        /// <returns>Returns a String List with enumerated registry values</returns>
        public static List<string> GetSubKeyNames(UIntPtr inHive, String inKeyName, RegSAM in32or64key)
        {
            int hkey = 0;
            try
            {
                var subKeyNames = new List<string>();
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.EnumerateSubKeys | (int)in32or64key, out hkey);
                if (lResult != 0)
                    return null;
                //A pointer to a buffer that receives the name of the subkey, including the terminating null character.
                StringBuilder subKeyName = new StringBuilder(64);
                //A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in characters.
                uint lpcchValueName = 64;
                // if getOnlyFirstValue is false we need to enumerate all registry values
                uint dwIndex = 0;
                // RegEnumValue result returns 0 (ERROR_SUCCESS) on success and 259 (ERROR_NO_MORE_ITEMS)
                int result = 0;

                while ((result = RegEnumKeyEx(hkey, dwIndex, subKeyName, ref lpcchValueName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)) != 259)
                {
                    subKeyNames.Add(subKeyName.ToString());
                    dwIndex++;
                    // lpcchValueName now holds the size of the subKeyName so we need to set it back to 64
                    lpcchValueName = 64;
                }
                return subKeyNames;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                return null;
            }
        }
Пример #11
0
 static extern int RegOpenKeyEx(
     RegistryHive hKey,
     [MarshalAs(UnmanagedType.VBByRefStr)] ref string subKey,
     RegOption dwOptions,
     RegSAM samDesired,
     out UIntPtr phkResult);
Пример #12
0
 static extern int RegCreateKeyEx(
     RegistryHive hKey,
     string lpSubKey,
     int Reserved,
     string lpClass,
     RegOption dwOptions,
     RegSAM samDesired,
     //UIntPtr lpSecurityAttributes,
     SECURITY_ATTRIBUTES lpSecurityAttributes,
     out UIntPtr phkResult,
     out RegResult lpdwDisposition);
Пример #13
0
			internal static extern int RegDeleteKeyEx(HKEY hKey, string subKey, RegSAM samDesired, uint reserved);
Пример #14
0
			internal static extern int RegCreateKeyEx(HKEY hkey, string subKey, uint reserved, string lpClass, uint options, RegSAM samDesired, IntPtr securityAttributes, out HKEY result, out uint disposition);
Пример #15
0
 /// <summary>
 /// Method to enumerates the values for the specified open registry key.
 /// </summary>
 /// <param name="inHive">i.e. RegHive.HKEY_LOCAL_MACHINE</param>
 /// <param name="inKeyName">Path to registry key</param>
 /// <param name="in32or64key">Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. Example RegSAM.WOW64_32Key</param>
 /// <param name="getOnlyFirstValue">Enumerate only the first registry value - used in ProductInfo.getProductCode()</param>
 /// <returns>Returns a String List with enumerated registry values</returns>
 public static List<string> GetValueNames(UIntPtr inHive, String inKeyName, RegSAM in32or64key, bool getOnlyFirstValue = false)
 {
     int hkey = 0;
     try
     {
         List<string> valueNames = new List<string>();
         uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
         if (lResult != 0)
             return null;
         //A pointer to a buffer that receives the name of the subkey, including the terminating null character.
         StringBuilder valueName = new StringBuilder(64);
         //A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in characters.
         uint lpcchValueName = 64;
         // If getOnlyFirstValue is true we need to enumerate only the first value
         // Because values are not ordered RegEnumValue may return values in any order.
         if (getOnlyFirstValue)
         {
             RegEnumValue(hkey, 0, valueName, ref lpcchValueName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
             valueNames.Add(valueName.ToString());
         }
         // if getOnlyFirstValue is false we need to enumerate all registry values
         else
         {
             uint dwIndex = 0;
             // RegEnumValue returns 0 (ERROR_SUCCESS) on success and 259 (ERROR_NO_MORE_ITEMS)
             while (RegEnumValue(hkey, dwIndex, valueName, ref lpcchValueName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 259)
             {
                 //result = RegEnumValue(hkey, i, valueName, ref lpcchValueName, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                 if (valueName.ToString() != "Patches")
                     valueNames.Add(valueName.ToString());
                 dwIndex++;
                 // lpcchValueName is set to 32 so we need to increment it in order to add the termination char
                 lpcchValueName++;
             }
         }
         return valueNames;
     }
     catch (Exception ex)
     {
         Logger.Error(ex.ToString());
         return null;
     }
 }
Пример #16
0
        public static byte[] GetRegKey64AsByteArray(UIntPtr inHive, String inKeyName, RegSAM in32or64key,
                                                    String inPropertyName)
        {
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int) RegSAM.QueryValue | (int) in32or64key, out hkey);
                if (0 != lResult) return null;
                RegistryValueKind lpType = 0;
                uint lpcbData = 2048;

                // Just make a big buffer the first time
                var byteBuffer = new byte[1000];
                // The first time, get the real size
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, byteBuffer, ref lpcbData);
                // Now create a correctly sized buffer
                byteBuffer = new byte[lpcbData];
                // now get the real value
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, byteBuffer, ref lpcbData);

                return byteBuffer;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
Пример #17
0
 internal static bool IsKeyExist(UIntPtr uIntPtr, string keyPath, RegSAM in32or64key)
 {
     string key = RegistryWrapper.GetRegKey(RegHive.LocalMachine, keyPath, in32or64key, "(Default)");
     return (key != null && key == "value not set") ? true : false;
 }
Пример #18
0
        /// <summary>
        /// Method to append the none-duplicated products from Uninstaller key
        /// We do this by iterating through the 64 and 32 bit Uninstall key @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" 
        /// and @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall".
        /// Ignore all SystemComponent key names that have value of 1
        /// Then we iterate through all user keys HKEY_USERS\user-sid\Software\Microsoft\Windows\CurrentVersion\Uninstall
        /// Then we add all none-duplicated products from @"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\user-sid\Products\product-code\InstallProperties"
        /// </summary>
        /// <param name="tempInstalledProducts"></param>
        /// <param name="inHive"></param>
        /// <param name="uninstallerKeyPath"></param>
        /// <param name="in64or32key"></param>
        /// <returns></returns>
        public static List<InstalledProgram> AddProductsFromUninstallerKey(this List<InstalledProgram> tempInstalledProducts, UIntPtr inHive, string uninstallerKeyPath, RegSAM in64or32key)
        {
            var regex = new Regex("KB[0-9]{5,7}$");
              int updatesCount = 0;

              List<string> subkeyNames = RegistryWrapper.GetSubKeyNames(inHive, uninstallerKeyPath, in64or32key);
              if (subkeyNames != null)
              {
            foreach (var subkeyName in subkeyNames)
            {
              // Check only keys like {CFEF48A8-BFB8-3EAC-8BA5-DE4F8AA267CE}
              // but not {CFEF48A8-BFB8-3EAC-8BA5-DE4F8AA267CE}.KB2504637 which are updates
              if (!regex.Match(subkeyName).Success)
              {
            string SystemComponent = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "SystemComponent");
            if (SystemComponent != "1")
            {
              string Windowsinstaller = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "Windowsinstaller");
              if (Windowsinstaller != "1")
              {
                //Make sure we are not dealing with a patch or an update
                string releaseType = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "ReleaseType");
                string parentKeyName = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "ParentKeyName");

                if (parentKeyName != null)
                {
                  if (parentKeyName != "" || releaseType == "Security Update" || releaseType == "Update Rollup" || releaseType == "Hotfix" )
                    updatesCount++;
                }

                if (parentKeyName == null)
                {
                  string uninstallString = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "UninstallString");
                  if (uninstallString != null)
                  {
                    string displayName = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayName");
                    string displayVersion = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayVersion");
                    string installDate = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "InstallDate");

                    if (displayName != null && displayName != "")
                    {
                      var product = new InstalledProgram(displayName, displayVersion, uninstallString, installDate);
                      if (!tempInstalledProducts.Contains(product))
                        tempInstalledProducts.Add(product);
                    }
                  }
                }
              }
              else
              {
                string productId = GetInstallerKeyNameFromGuid(subkeyName);
                string productKey = @"Software\Classes\Installer\Products\" + productId;
                string productName = RegistryWrapper.GetRegKey(inHive, productKey, RegSAM.WOW64_64Key, "ProductName");
                if (productName != null)
                {
                  string displayVersion = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayVersion");
                  string uninstallString = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "UninstallString");
                  string installDate = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "InstallDate");

                  var product = new InstalledProgram(productName, displayVersion, uninstallString, installDate);
                  if (!tempInstalledProducts.Contains(product))
                    tempInstalledProducts.Add(product);
                }
              }
            }
              }
            }
              }
              return tempInstalledProducts;
        }
Пример #19
0
		ExtendedRegistry(UIntPtr rootKey, RegSAM keyType) {
			this.rootKey = rootKey;
			this.keyType = keyType;
		}