2010-01-11 28 views
45

Tôi có một số mã đọc sổ đăng ký và tìm giá trị trong HKEY_LOCAL_MACHINE\Software\App\ nhưng khi chạy trên các phiên bản Windows 64 bit có giá trị dưới HKEY_LOCAL_MACHINE\Software\Wow6432Node\App\.Đọc đăng ký và khóa Wow6432Node

Tôi nên tiếp cận tốt nhất điều này như thế nào? Tôi có cần trình cài đặt 64 bit hay tôi nên viết lại mã của mình để phát hiện cả hai địa điểm?

+0

Chương trình nào quản lý khóa đăng ký trong HKEY_LOCAL_MACHINE \ Software \ App? Bạn đang cố gắng đọc các khóa registry do chương trình khác tạo ra? – wj32

+0

Xin chào, Không có ứng dụng nào của tôi đọc khóa, khóa được ghi trong sổ đăng ký của trình cài đặt Visual Studio 2008. –

Trả lời

46

Nếu bạn đánh dấu bạn chương trình C# là x86 (và không phải CPU nào) thì nó sẽ thấy HKEY_LOCAL_MACHINE\Software\Wow6432Node\AppHKEY_LOCAL_MACHINE\Software\App\.

Chương trình .NET cho CPU bất kỳ sẽ chạy dưới dạng quy trình 64 bit nếu cài đặt .NET 64 bit. Việc đăng ký 32-bit là theo Wow6432Node cho các chương trình 64-bit.

+2

@Arve: Tôi ghét phải là người mang tin xấu, nhưng mẹo này không làm gì cho các máy WinXP cũ hơn - công ty chúng tôi vẫn sử dụng hàng trăm cái này và khóa Wow6432Node không có trên chúng. – jp2code

+8

Khóa Wow6432Node chỉ tồn tại trên các máy 64 bit –

57

Trên một máy x64, đây là một ví dụ về làm thế nào để truy cập vào xem 32-bit của registry:

using (var view32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, 
              RegistryView.Registry32)) 
{ 
    using (var clsid32 = view32.OpenSubKey(@"Software\Classes\CLSID\", false)) 
    { 
    // actually accessing Wow6432Node 
    } 
} 

... so với ...

using (var view64 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, 
              RegistryView.Registry64)) 
{ 
    using (var clsid64 = view64.OpenSubKey(@"Software\Classes\CLSID\", true)) 
    { 
    .... 
    } 
} 
6

+ 1 để trả lời của Wally, nhưng giải pháp của ông làm việc cho .NET 4.0 và cao hơn.

Tôi đã tìm thấy một giải pháp, mà cũng làm việc cho NET 2,0 here

#region RegHelper 
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 
} 

static class RegHive 
{ 
    public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u); 
    public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u); 
} 

static class RegistryWOW6432 
{ 
    [DllImport("Advapi32.dll")] 
    static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out int phkResult); 

    [DllImport("Advapi32.dll")] 
    static extern uint RegCloseKey(int hKey); 

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

    static public string GetRegKey64(UIntPtr inHive, String inKeyName, string inPropertyName) 
    { 
     return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_64Key, inPropertyName); 
    } 

    static public string GetRegKey32(UIntPtr inHive, String inKeyName, string inPropertyName) 
    { 
     return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_32Key, inPropertyName); 
    } 

    static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, string inPropertyName) 
    { 
     //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002; 
     int hkey = 0; 

     try 
     { 
      uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey); 
      if (0 != lResult) return null; 
      uint lpType = 0; 
      uint lpcbData = 1024; 
      StringBuilder AgeBuffer = new StringBuilder(1024); 
      RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, AgeBuffer, ref lpcbData); 
      string Age = AgeBuffer.ToString(); 
      return Age; 
     } 
     finally 
     { 
      if (0 != hkey) RegCloseKey(hkey); 
     } 
    } 
} 
#endregion 

Cách sử dụng:

string value64 = RegistryWOW6432.GetRegKey64(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization"); 
string value32 = RegistryWOW6432.GetRegKey32(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization"); 
3

Dưới đây là một giải pháp tất cả-trong-một trong đó sẽ bao gồm x32/hệ thống x64 và các ứng dụng chụp được cài đặt trên máy cục bộ hoặc tài khoản người dùng.

public class InstalledProgramInfo 
    { 
     public string name; 
     public string path; 
    } 

     public static InstalledProgramInfo FindInstalledApp(string findname, bool dump = false) 
    { 
     if (String.IsNullOrEmpty(findname)) return null; 

     string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 

     RegistryHive[] keys = new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine }; 
     RegistryView[] views = new RegistryView[] { RegistryView.Registry32, RegistryView.Registry64 }; 

     foreach (var hive in keys) 
     { 
      foreach (var view in views) 
      { 
       RegistryKey rk = null, 
        basekey = null; 

       try 
       { 
        basekey = RegistryKey.OpenBaseKey(hive, view); 
        rk = basekey.OpenSubKey(uninstallKey); 
       } 
       catch (Exception ex) { continue; } 

       if (basekey == null || rk == null) 
        continue; 

       if (rk == null) 
       { 
        if (dump) Console.WriteLine("ERROR: failed to open subkey '{0}'", uninstallKey); 
        return null; 
       } 

       if (dump) Console.WriteLine("Reading registry at {0}", rk.ToString()); 

       foreach (string skName in rk.GetSubKeyNames()) 
       { 
        try 
        { 
         RegistryKey sk = rk.OpenSubKey(skName); 
         if (sk == null) continue; 

         object skname = sk.GetValue("DisplayName"); 

         object skpath = sk.GetValue("InstallLocation"); 
         if (skpath == null) 
         { 
          skpath = sk.GetValue("UninstallString"); 
          if (skpath == null) continue; 
          FileInfo fi = new FileInfo(skpath.ToString()); 
          skpath = fi.Directory.FullName; 
         } 

         if (skname == null || skpath == null) continue; 

         string thisname = skname.ToString(); 
         string thispath = skpath.ToString(); 

         if (dump) Console.WriteLine("{0}: {1}", thisname, thispath); 

         if (!thisname.Equals(findname, StringComparison.CurrentCultureIgnoreCase)) 
          continue; 

         InstalledProgramInfo inf = new InstalledProgramInfo(); 
         inf.name = thisname; 
         inf.path = thispath; 

         return inf; 
        } 
        catch (Exception ex) 
        { 
         // todo 
        } 
       }     
      } // view 
     } // hive 

     return null; 
    } 
Các vấn đề liên quan