How to read a REG_BINARY value from registry where installations of 32bit applications are found, but on a 64bit OS using C# -


i want value of particular node in registry using c# on win7 64bit machine:

enter image description here

this value resides in registry installations of 32bit applications store information. problem facing not reaching node returning meaningful value array (string or int).

the code using shown below , adapted

http://social.msdn.microsoft.com/forums/vstudio/en-us/378491d6-23a3-4ae7-a702-c52c5abb0e8d/access-to-both-32-and-64-bit-registry-using-c-and-regmultisz?forum=csharpgeneral

and

http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

so listing this:

public static uintptr hkey_local_machine = new uintptr(0x80000002u);  [dllimport("advapi32.dll")] public static extern int regqueryvalueex(int hkey, string lpvaluename, int lpreserved, ref uint lptype, byte[] lpdata, ref uint lpcbdata);  public enum regsam {     queryvalue = 0x0001,     setvalue = 0x0002,     createsubkey = 0x0004,     enumeratesubkeys = 0x0008,     notify = 0x0010,     createlink = 0x0020,     wow64_32key = 0x0200,     wow64_64key = 0x0100,     wow64_res = 0x0300,     read = 0x00020019,     write = 0x00020006,     execute = 0x00020019,     allaccess = 0x000f003f }  string path = @"software\microsoft\windows nt\currentversion";  registrykey rktest = registry.localmachine.opensubkey(path); try {        int hkey = 0;     uint lresult = regopenkeyex(hkey_local_machine, path, 0, (int)regsam.queryvalue | (int)regsam.wow64_32key, out hkey);     if (0 != lresult) hkey = 0;     else     {         uint lptype = 0;         uint lpcbdata = 1024;         byte[] buffer = new byte[1024];         regqueryvalueex(hkey, "digitalproductid", 0, ref lptype, buffer, ref lpcbdata);         ... @ point buffer returns empty.     } } catch (exception exc) { }; 

but when execute buffer empty.

enter image description here

the code have provided here can reach particular location registry shown in top image , read normal string values if replace byte[] stringbuilder class, provided value in registry of type reg_sz. can if of type reg_binary?

could point out me how can achieve this?

ps:

just show have been working on similar issue few days have converted reg_binary value readable array of integers registry using code snippet below, if value under wow6432 node.

byte[] bytes = (byte[])rktest.getvalue("digitalproductid"); string s = convert.tobase64string(bytes); list<int> int_array = new list<int>(); int temp_int = 0, = 0; (i = 0; < bytes.length; i++) { temp_int = bytes[i]; int_array.add(temp_int); } 

the problem want solve here area in registry 32bit applications stored information in reg_binary type.

thank you.

solution:

apparently 2nd link provided had needed @ last example shown on page. adapt solution in code had add method getregkey64asbytearray() this:

static public byte[] getregkey64asbytearray(uintptr inhive, string inkeyname, regsam in32or64key, string inpropertyname) {     int hkey = 0;     try {         uint lresult = regopenkeyex(inhive, inkeyname, 0, (int)regsam.queryvalue | (int)in32or64key, out hkey);         if (0 != lresult) return null;         registryvaluekind lptype = 0;         uint lpcbdata = 2048;         // make big buffer first time         byte[] bytebuffer = new byte[1000];         // first time, real size         regqueryvalueex(hkey, inpropertyname, 0, ref lptype, bytebuffer, ref lpcbdata);         // create correctly sized buffer         bytebuffer = new byte[lpcbdata];         // real value         regqueryvalueex(hkey, inpropertyname, 0, ref lptype, bytebuffer, ref lpcbdata);         return bytebuffer;       }     { if (0 != hkey) regclosekey(hkey); } } 

and reg_binary code in string so:

byte[] bytevalue64 = new byte[1024]; registrykey localkey = registry.localmachine; localkey = localkey.opensubkey(@"software\microsoft\windows nt\currentversion"); if (localkey != null) {     bytevalue64 = getregkey64asbytearray(hkey_local_machine, @"software\microsoft\windows nt\currentversion", regsam.wow64_64key, "digitalproductid");     string s = convert.tobase64string(bytevalue64);     //need following operation because array returns inverted!     list<int> int_array = new list<int>();     int temp_int = 0, = 0;                     (i = 0; < bytevalue64.length; i++) { temp_int = bytevalue64[i]; int_array.add(temp_int); }     ... int values here } 

at last line sending int array function gives me readable alphanumeric string.

thanks everyone.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -