} finally {
if (UIntPtr.Zero != hkey) {
RegCloseKey(hkey);
}
}
}
/// <summary>
/// Создает двоичный параметр в заданном ключе
/// </summary>
/// <param name='keyName'>Имя ключа</param>
/// <param name='valueName'>Имя параметра</param>
/// <param name='dwordData'>Значение параметра</param>
/// <returns>B успешном случае возвращается
/// ERROR_SUCCESS</returns>
public static int CreateValueBinary(UIntPtr root, string keyName,
string valueName, uint binData) {
UIntPtr hkey = UintPtr.Zero;
try {
int result = RegOpenKeyEx(root, keyName, 0, KeyAccess.None, ref hkey);
if (ERROR_SUCCESS != result) return result;
byte[] data = BitConverter.GetBytes(binData);
return RegSetValueEx(hkey, valueName, 0, KeyType.Binary, data,
(uint)data.Length);
} finally {
if (UIntPtr.Zero != hkey) {
RegCloseKey(hkey);
}
}
}
/// <summary>
/// Получает значение строкового параметра
/// </summary>
/// <param name='keyName'>Имя ключа</param>
/// <param name='valueName'>Имя параметра</param>
/// <param name='stringResult'>строковые данные</param>
/// <returns>В успешном случае возвращается
/// ERROR_SUCCESS</returns>
public static int GetStringValue(UIntPtr root, string keyName,
string valueName, ref string stringResult) {
UIntPtr hkey = UintPtr.Zero;
try {
int result = RegOpenKeyEx(root, keyName, 0, KeyAccess.None, ref hkey);
if (ERROR_SUCCESS != result) return result;
byte[] bytes = null;
uint length = 0;
KeyType keyType = KeyType.None;
result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType, null,
ref length);
if (ERROR_SUCCESS != result) return result;
keyType = KeyType.None;
bytes = new byte[length];
result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType, bytes,
ref length);
if (ERROR SUCCESS != result) return result;
stringResult = Encoding.Unicode.GetString(bytes, 0, bytes.Length);
return ERROR_SUCCESS;
} finally {
if (UIntPtr.Zero != hkey) {
RegCloseKey(hkey);
}
}
}
/// <summary>
/// Получает заданное значение типа DWORD
/// </summary>
/// <param name='keyName'>Имя ключа</param>
/// <param name='valueName'>Имя параметра</param>
/// <param name='dwordResult'>Значение параметра</param>
/// <returns>B успешной случае возвращается
/// ERROR_SUCCESS</returns>
public static int GetDWORDValue(UIntPtr root, string keyName,
string valueName, ref uint dwordResult) {
UIntPtr hkey = UintPtr.Zero;
try {
int result = RegOpenKeyEx(root, keyName, 0, KeyAccess.None, ref hkey);
if (ERROR_SUCCESS != result) return result;
byte[] bytes = null;
uint length = 0;
KeyType keyType = KeyType.None;
result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType, null,
ref length);
bytes = new byte[Marshal.SizeOf(typeof(uint))];
length = (uint)bytes.Length;
keyType = KeyType.None;
result = RegQueryValueEx(hkey, valueName, IntPtr.Zero, ref keyType, bytes,
ref length);
if (ERROR_SUCCESS != result) return result;
dwordResult = BitConverter.ToUInt32(bytes, 0);
return ERROR_SUCCESS;
} finally {
if (UIntPtr.Zero != hkey) {
RegCloseKey(hkey);
}
}
}