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