A
Asaf Ganot
Hi,
I'm looking for a way to retrieve the date type of a registry value using
c#.
Since I couldn't find a way to do it with 'Microsoft.Win32', I tried to use
the good old API call 'RegEnumValue' which gives you the data type as well
as other things...
After many tries, I managed to call the function without any errors.
However, I get the data type, but not the value's name! (I even get the name
length, but the string that supose to recive the name is exactly I
initialized it.
Please help!
Here is the declartion section:
public class APIImportDLLs
{
#region Class Functions
[DllImport("advapi32.dll", EntryPoint="RegEnumValue")]
public static extern int RegEnumValueA(int hKey, int dwIndex, string
lpValueName, ref int lpcbValueName, int lpReserved, ref int lpType, ref byte
lpData, ref int lpcbData);
[DllImport("advapi32.dll", EntryPoint="RegOpenKey")]
public static extern int RegOpenKeyA(int hKey, string lpSubKey, ref int
phkResult) ;
#endregion
public int APIConvertHKey(string hkey)
{
switch(hkey.ToUpper())
{
case "HKEY_CLASSES_ROOT":
case "HKCR":
return System.Convert.ToInt32("80000000",16);
case "HKEY_CURRENT_CONFIG":
case "HKCC":
return System.Convert.ToInt32("80000005",16);
case "HKEY_CURRENT_USER":
case "HKCU":
return System.Convert.ToInt32("80000001",16);
case "HKEY_DYN_DATA":
case "HKDD":
return System.Convert.ToInt32("80000006",16);
case "HKEY_LOCAL_MACHINE":
case "HKLM":
return System.Convert.ToInt32("80000002",16);
//case "HKEY_PERFORMANCE_DATA":
//case "HKPD":
// return System.Convert.ToInt32("80000000",16);
case "HKEY_USERS":
case "HKU":
return System.Convert.ToInt32("80000003",16);
default:
return 0;
}
}
}
Here is my function:
public struct_RegValue GetRegValue(string path)
{
APIImportDLLs apiclass = new APIImportDLLs();
int ret;
int hresult = 0;
int index = 0;
//string curval = " ";
string curval = "";
char[] chr = new char[255];
byte data = 0;
int namelen = 255;
int datelen = 255;
int type = 0;
struct_RegValue regval = new struct_RegValue();
for(int i=0;i<255;i++)
{
curval=curval + " ";
}
string hkey = path.Substring(0,path.IndexOf(@"\"));
string keypath = path.Substring(path.IndexOf(@"\")+1);
ret = APIImportDLLs.RegOpenKeyA(apiclass.APIConvertHKey(hkey),keypath + '\0'
,ref hresult);
if(ret!=0)
{
throw(new Exception("Error openning key using API function 'RegOpenKeyA'"));
}
while(APIImportDLLs.RegEnumValueA(hresult,index,curval,ref namelen,0,ref
type,ref data,ref datelen)!=259)
{
Console.WriteLine(type.ToString());
index++;
}
string temp = path.Substring(0,path.IndexOf(@"\"));
regval.RegKey = GetRegKey(path);
string[] valnames = regval.RegKey.GetValueNames();
foreach (string str in valnames)
{
Console.WriteLine(str);
}
return regval;
}
I'm looking for a way to retrieve the date type of a registry value using
c#.
Since I couldn't find a way to do it with 'Microsoft.Win32', I tried to use
the good old API call 'RegEnumValue' which gives you the data type as well
as other things...
After many tries, I managed to call the function without any errors.
However, I get the data type, but not the value's name! (I even get the name
length, but the string that supose to recive the name is exactly I
initialized it.
Please help!
Here is the declartion section:
public class APIImportDLLs
{
#region Class Functions
[DllImport("advapi32.dll", EntryPoint="RegEnumValue")]
public static extern int RegEnumValueA(int hKey, int dwIndex, string
lpValueName, ref int lpcbValueName, int lpReserved, ref int lpType, ref byte
lpData, ref int lpcbData);
[DllImport("advapi32.dll", EntryPoint="RegOpenKey")]
public static extern int RegOpenKeyA(int hKey, string lpSubKey, ref int
phkResult) ;
#endregion
public int APIConvertHKey(string hkey)
{
switch(hkey.ToUpper())
{
case "HKEY_CLASSES_ROOT":
case "HKCR":
return System.Convert.ToInt32("80000000",16);
case "HKEY_CURRENT_CONFIG":
case "HKCC":
return System.Convert.ToInt32("80000005",16);
case "HKEY_CURRENT_USER":
case "HKCU":
return System.Convert.ToInt32("80000001",16);
case "HKEY_DYN_DATA":
case "HKDD":
return System.Convert.ToInt32("80000006",16);
case "HKEY_LOCAL_MACHINE":
case "HKLM":
return System.Convert.ToInt32("80000002",16);
//case "HKEY_PERFORMANCE_DATA":
//case "HKPD":
// return System.Convert.ToInt32("80000000",16);
case "HKEY_USERS":
case "HKU":
return System.Convert.ToInt32("80000003",16);
default:
return 0;
}
}
}
Here is my function:
public struct_RegValue GetRegValue(string path)
{
APIImportDLLs apiclass = new APIImportDLLs();
int ret;
int hresult = 0;
int index = 0;
//string curval = " ";
string curval = "";
char[] chr = new char[255];
byte data = 0;
int namelen = 255;
int datelen = 255;
int type = 0;
struct_RegValue regval = new struct_RegValue();
for(int i=0;i<255;i++)
{
curval=curval + " ";
}
string hkey = path.Substring(0,path.IndexOf(@"\"));
string keypath = path.Substring(path.IndexOf(@"\")+1);
ret = APIImportDLLs.RegOpenKeyA(apiclass.APIConvertHKey(hkey),keypath + '\0'
,ref hresult);
if(ret!=0)
{
throw(new Exception("Error openning key using API function 'RegOpenKeyA'"));
}
while(APIImportDLLs.RegEnumValueA(hresult,index,curval,ref namelen,0,ref
type,ref data,ref datelen)!=259)
{
Console.WriteLine(type.ToString());
index++;
}
string temp = path.Substring(0,path.IndexOf(@"\"));
regval.RegKey = GetRegKey(path);
string[] valnames = regval.RegKey.GetValueNames();
foreach (string str in valnames)
{
Console.WriteLine(str);
}
return regval;
}