CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    4

    Question Using API RegQueryValueEx with DllImport to read data from Registry

    Hello,

    I'm trying to read registry value using the API functions RegOpenKeyEx and RegQueryValueEx along with DllImport in C#. The results show that code runs fine but I'm not getting the desired data. Its returns 0 instead of the data. I'm including the code that I'm using

    //*** declarations in the class *****

    public static readonly UIntPtr HkeyLocalMachine = (UIntPtr)0x80000002;

    public const string lpSubKey = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";

    public const int KEY_QUERY_VALUE = 0x1;
    public const int REZ_SZ =0x00000002;

    [DllImport("advapi32.dll",EntryPoint="RegOpenKeyEx")]
    public static extern int RegOpenKeyEx_DllImport(UIntPtr hKey,string lpSubKey,uint ulOptions,int samDesired,out IntPtr phkResult);

    [DllImport("advapi32.dll",EntryPoint="RegQueryValueEx")]
    public static extern int RegQueryValueEx_DllImport(IntPtr hKey,string lpValueName,int lpReserved,out uint lpType, IntPtr lpData,out uint lpcbData);

    //****** declared on load or click event ****

    IntPtr hKeyVal;
    uint lpType;
    uint lpcbData;
    IntPtr q=new IntPtr();

    int valueRet=RegOpenKeyEx_DllImport(HkeyLocalMachine,lpSubKey,0,KEY_QUERY_VALUE,out hKeyVal);

    valueRet=RegQueryValueEx_DllImport(hKeyVal,"ProcessorNameString",0,out lpType, q,out lpcbData);


    Output when viewd using different variables :

    lpType= returns 1 which signify a string value
    q returns 0 *** this should return data but does not
    lpcbData returns 48 signifying size of the data being returned.

    The return values of the functions show 0 which means success

    Any help or insight on "Why I'm unable to get the data" would be highly appreciated.

    Thanks
    Rahul Sharma

  2. #2
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Using API RegQueryValueEx with DllImport to read data from Registry

    I can't get why you want to use those API functions while there are some classes for this task
    Code:
     //using Microsoft.Win32;
    RegistryKey rk = Registry.LocalMachine.OpenSubKey
    ("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
    string result=rk.GetValue("ProcessorNameString").ToString();
    Console.WriteLine(result);

  3. #3
    Join Date
    Jul 2005
    Posts
    4

    Re: Using API RegQueryValueEx with DllImport to read data from Registry

    Thanks a lot for the suggestion mehdi62b but as I'm working on an E-Learning Project I need to demonstrate reading of registry in C# by calling Win 32 API's using DllImport. My hands are tied in this respect. So any help with tha above code would be grately appreciated.

    Thanks,
    Rahul

  4. #4
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Using API RegQueryValueEx with DllImport to read data from Registry

    when you need a pointer to a string you should use a StringBuilder object,
    Code:
       //its Definition
       public static extern int RegQueryValueEx_DllImport(IntPtr hKey,string lpValueName,int lpReserved,out uint lpType,StringBuilder sb,out uint lpcbData);
    Code:
    	  //its size should be 100 for having a sufficent capacity for the buffer
       StringBuilder sb=new StringBuilder(100);
       int valueRet=RegOpenKeyEx_DllImport(HkeyLocalMachine,lpSubKey,0,KEY_QUERY_VALUE,out hKeyVal);
       valueRet=RegQueryValueEx_DllImport(hKeyVal,"ProcessorNameString",0,out lpType,sb,out lpcbData);

  5. #5
    Join Date
    Jul 2005
    Posts
    4

    Re: Using API RegQueryValueEx with DllImport to read data from Registry

    Hi,

    This is what is troubling me. If I use a StringBuilder object the value returned by the function is 234 which signifies more data is available. This is the case even if I initialize the StringBuilder with 1000.

    Even this doesn't help!!!!

    Any other suggestion please.

    Regards,
    Rahul.

  6. #6
    Join Date
    Jul 2005
    Posts
    4

    Resolved Re: Using API RegQueryValueEx with DllImport to read data from Registry

    Was finally able to resolve the issue.
    Here is the corrected code.

    public static readonly UIntPtr HkeyLocalMachine = (UIntPtr)0x80000002;
    public static readonly UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;

    public const string lpSubKey = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";

    public const long REG_OPTION_NON_VOLATILE = 0x00000000L;

    public const int KEY_QUERY_VALUE = 0x1;
    public const int REZ_SZ =0x00000002;
    public const int KEY_ALL_ACCESS = 0x3F;

    [DllImport("advapi32.dll",EntryPoint="RegOpenKeyEx")]
    public static extern int RegOpenKeyEx_DllImport(UIntPtr hKey,string lpSubKey,uint ulOptions,int samDesired,out UIntPtr phkResult);

    [DllImport("advapi32.dll",EntryPoint="RegQueryValueEx")]
    public static extern int RegQueryValueEx_DllImport(UIntPtr hKey,string lpValueName,int lpReserved,out uint lpType,StringBuilder lpData,ref int lpcbData);

    UIntPtr hKeyVal;
    int valueRet;
    uint lpType;

    StringBuilder sb=new StringBuilder(100);
    int lpcbData=sb.Capacity;

    valueRet=RegOpenKeyEx_DllImport(HkeyLocalMachine,lpSubKey,0,KEY_QUERY_VALUE,out hKeyVal);

    valueRet=RegQueryValueEx_DllImport(hKeyVal,"ProcessorNameString",0,out lpType,sb,ref lpcbData);

    MessageBox.Show("data type : " + lpType.ToString(),"Value",MessageBoxButtons.OK);

    MessageBox.Show("data size : " + lpcbData.ToString(),"Value",MessageBoxButtons.OK);

    MessageBox.Show("data : " + sb.ToString(),"Value",MessageBoxButtons.OK);

    The last parameter in the ReqQueryValueEx was to be Ref instead of out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured