32-bit programs access the Windows registry on a 64-bit system

Source: Internet
Author: User
Tags mssqlserver

Today, in the course of the work encountered a strange problem, that is, through C # to get the registry key, the problem is generally relatively simple:

 stringSQLPath =string.                        Empty; RegistryKey HKML=Registry.localmachine; RegistryKey Mssqlserverkey= Hkml. OpenSubKey (@"Software\microsoft\mssqlserver"); if(Mssqlserverkey! =NULL)            {                string[] keys =Mssqlserverkey.getsubkeynames (); RegistryKey Setupkey= Mssqlserverkey.opensubkey ("Setup"); if(setupkey!=NULL) {SQLPath= Setupkey.getvalue ("SQLPath").                ToString (); }}

But I want to get the value is not get, debug, find Setupkey return to NULL, open the registry of the machine is some ah, odd strange ...

What's going on???? Asked colleagues, he also felt strange. Later I found that is not the number of registry problems Ah, suddenly find out why the system returned is missing those key values.

What we found below Wow6432node is the return value we have at debug time.

This feeling has found the way to solve the problem. is to resolve a problem with a 32-bit program accessing the Windows registry on a 64-bit system.

Finally found on the internet two tall man's solution, I put it here

C # 32-bit programs access 64-bit system registry

Use the method provided by the. netFx4.0 to resolve the 64-bit registry for 32-bit programs accessing 64-bit systems

Finally I tried using the latter method to solve the problem. Thanks to the solution of the Wave Valley.

   Public Static stringGetsqlserverinstallpath () {stringSQLPath =string.                        Empty; RegistryKey HKML=Registry.localmachine; RegistryKey Mssqlserverkey= Hkml. OpenSubKey (@"Software\microsoft\mssqlserver"); if(Mssqlserverkey! =NULL)            {                string[] keys =Mssqlserverkey.getsubkeynames (); RegistryKey Setupkey= Mssqlserverkey.opensubkey ("Setup"); if(setupkey!=NULL) {SQLPath= Setupkey.getvalue ("SQLPath").                ToString (); }              Else{SQLPath= Get64bitregistrykey (Registryhive.localmachine,@"Software\microsoft\mssqlserver\setup","SQLPath", registryview.registry64); }            }            returnSQLPath; }        /// <summary>        ///This method was use for the 32appliction to get the 64bit Regestkey/// </summary>        /// <param name= "Parentkeyname" ></param>        /// <param name= "SubkeyName" ></param>        /// <param name= "KeyName" ></param>        /// <returns></returns>         Public Static stringGet64bitregistrykey (RegistryHive Hive,stringKeyName,stringValuename,registryview View) {Microsoft.Win32.SafeHandles.SafeRegistryHandle handle=NewMicrosoft.Win32.SafeHandles.SafeRegistryHandle (Gethivehandle (Hive),true); RegistryKey subkey=Registrykey.fromhandle (handle, view).            OpenSubKey (KeyName); RegistryKey Key=Registrykey.fromhandle (subkey.            Handle, view); returnkey. GetValue (ValueName).        ToString (); }        StaticIntPtr gethivehandle (registryhive hive) {IntPtr Preexistinghandle=IntPtr.Zero; INTPTR HKEY_CLASSES_ROOT=NewINTPTR (-2147483648); INTPTR HKEY_CURRENT_USER=NewINTPTR (-2147483647); INTPTR HKEY_LOCAL_MACHINE=NewINTPTR (-2147483646); IntPtr HKEY_USERS=NewINTPTR (-2147483645); INTPTR Hkey_performance_data=NewINTPTR (-2147483644); INTPTR Hkey_current_config=NewINTPTR (-2147483643); INTPTR Hkey_dyn_data=NewINTPTR (-2147483642); Switch(hive) { CaseRegistryHive.ClassesRoot:preexistingHandle = HKEY_CLASSES_ROOT; Break;  CaseRegistryHive.CurrentUser:preexistingHandle = HKEY_CURRENT_USER; Break;  CaseRegistryHive.LocalMachine:preexistingHandle = HKEY_LOCAL_MACHINE; Break;  CaseRegistryHive.Users:preexistingHandle = HKEY_USERS; Break;  CaseRegistryHive.PerformanceData:preexistingHandle = Hkey_performance_data; Break;  CaseRegistryHive.CurrentConfig:preexistingHandle = Hkey_current_config; Break;  CaseRegistryHive.DynData:preexistingHandle = Hkey_dyn_data; Break; }            returnPreexistinghandle; }
View Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.