473,422 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Getting registry value's data type (REG_SZ/REG_EXPAND_SZ)

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,cu rval,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;

}
Nov 17 '05 #1
1 5604
>[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);


Make lpValueName a System.Text.StringBuilder instead of a string.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jester | last post by:
'This is the code i have so far: Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As _ Long) As Long Private Declare Function RegOpenKey Lib "advapi32.dll" Alias _...
1
by: Jim Moon | last post by:
I'm setting up a Win Server 2003 for remote debugging. From this page, I see that I need to set two registry keys on the server:...
1
by: ChuckD_Duncan | last post by:
using Microsoft.Win32; RegistryKey key = Registry.LocalMachine; key = key.OpenSubKey("....."); ... key = key.OpenSubKey("PCF", true); double aDoubleValue = 0.25; key.SetValue("testkey",...
3
by: Bo | last post by:
I am using RegistryKey.GetValue() method to retrieve values from Windows Registry. I don't know how to read type REG_MULTI_SZ into a string array. I have tried string array = (string)...
3
by: David Gacek | last post by:
How do you write a new Reg_Expand_sz value to the resgistry with vb.net this used to work when you pad the string with a null. Anyone know how or what i'm doing wrong ? It just writes a new...
5
by: Dave | last post by:
I'd like to create a Registry key value where the Registry key value type is REG_QWORD. I'm using SetValue(name As String, value As Object), and I understand that the object type used in this...
9
by: Newbie Coder | last post by:
Hello Newsgroup Readers I would like to know how to go & do the following: I have a certain registry key that has sub values Example: Key1 http://www.microsoft.com Key2 ...
4
by: RhavoX | last post by:
Hi. This may be a very stupid question but I'll leave you to judge it ;) I know there were lots of questions about this but none of the answers suits me. I'm wondering how to get the BINARY type...
11
by: Unknown Hero | last post by:
Tim Golden wrote: The first link which points to the Python documentation for the _winreg module I already checked, even before coming here. I am wondering how I should do the loop I need (go...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.