当前位置:编程学习 > C#/ASP.NET >>

WMI 远程注册表 访问

因为公司的要求,想要做一个软件信息收集工作。
想到了远程注册表获取软件一览的方式,如下:

//......
string strHost = "localhost"; //主机名
string regMain = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
string[] subKey = { "DisplayName" };
ManagementClass mClass = new ManagementClass(@"//" + strHost + @"/root/DEFAULT:StdRegProv");
const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
object[] method_args = new object[] { HKEY_LOCAL_MACHINE, regMain, null };
uint result = (uint)mClass.InvokeMethod("EnumKey", method_args);
string[] sAppList = (String[])method_args[2];
//......


以上代码在X86的windows上没有问题,但是获取X64系统时,
regMain被重新定位到了
" SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

请问如何修改代码以防止系统自动重定向注册表? --------------------编程问答-------------------- UP
谁能帮我解决一下这个问题?
谢谢了 --------------------编程问答-------------------- HELP --------------------编程问答--------------------

1 using System;  2  using System.Collections.Generic;  3  using System.Linq;  4  using System.Text;  5  using Microsoft.Win32;  6  using System.Runtime.InteropServices;  7   8  namespace OperateRegistrationTable  9 { 10     class Programe 11     { 12         static void Main(string[] args) 13         { 14             string myParentKeyName = "HKEY_LOCAL_MACHINE"; 15             string mySubKeyName = @"SOFTWARE\EricSun\MyTestKey"; 16             string myKeyName = "MyKeyName"; 17  18             string value = string.Empty; 19             value = Utility.Get64BitRegistryKey(myParentKeyName, mySubKeyName, myKeyName); 20             Console.WriteLine("The Value is: {0}", value); 21         } 22     } 23  24     public class Utility 25     { 26         #region 32位程序读写64注册表 27  28         static UIntPtr HKEY_CLASSES_ROOT = (UIntPtr)0x80000000; 29         static UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001; 30         static UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002; 31         static UIntPtr HKEY_USERS = (UIntPtr)0x80000003; 32         static UIntPtr HKEY_CURRENT_CONFIG = (UIntPtr)0x80000005; 33  34         // 关闭64位(文件系统)的操作转向 35          [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 36         public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); 37         // 开启64位(文件系统)的操作转向 38          [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 39         public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);     40     41         // 获取操作Key值句柄 42          [DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 43         public static extern uint RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions,                                   int samDesired, out IntPtr phkResult); 44         //关闭注册表转向(禁用特定项的注册表反射) 45         [DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 46         public static extern long RegDisableReflectionKey(IntPtr hKey); 47         //使能注册表转向(开启特定项的注册表反射) 48         [DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 49         public static extern long RegEnableReflectionKey(IntPtr hKey); 50         //获取Key值(即:Key值句柄所标志的Key对象的值) 51         [DllImport("Advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 52         private static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, 53                                                   out uint lpType, System.Text.StringBuilder lpData, 54                                                   ref uint lpcbData); 55  56         private static UIntPtr TransferKeyName(string keyName) 57         { 58             switch (keyName) 59             { 60                 case "HKEY_CLASSES_ROOT": 61                     return HKEY_CLASSES_ROOT; 62                 case "HKEY_CURRENT_USER": 63                     return HKEY_CURRENT_USER; 64                 case "HKEY_LOCAL_MACHINE": 65                     return HKEY_LOCAL_MACHINE; 66                 case "HKEY_USERS": 67                     return HKEY_USERS; 68                 case "HKEY_CURRENT_CONFIG": 69                     return HKEY_CURRENT_CONFIG; 70             } 71  72             return HKEY_CLASSES_ROOT; 73         } 74  75         public static string Get64BitRegistryKey(string parentKeyName, string subKeyName, string keyName) 76         { 77             int KEY_QUERY_VALUE = (0x0001); 78             int KEY_WOW64_64KEY = (0x0100); 79             int KEY_ALL_WOW64 = (KEY_QUERY_VALUE | KEY_WOW64_64KEY); 80  81             try 82             { 83                 //将Windows注册表主键名转化成为不带正负号的整形句柄(与平台是32或者64位有关) 84                 UIntPtr hKey = TransferKeyName(parentKeyName); 85  86                 //声明将要获取Key值的句柄 87                 IntPtr pHKey = IntPtr.Zero; 88  89                 //记录读取到的Key值 90                 StringBuilder result = new StringBuilder("".PadLeft(1024)); 91                 uint resultSize = 1024; 92                 uint lpType = 0; 93  94                 //关闭文件系统转向  95                 IntPtr oldWOW64State = new IntPtr(); 96                 if (Wow64DisableWow64FsRedirection(ref oldWOW64State)) 97                 { 98                     //获得操作Key值的句柄 99                     RegOpenKeyEx(hKey, subKeyName, 0, KEY_ALL_WOW64, out pHKey);100 101                     //关闭注册表转向(禁止特定项的注册表反射)102                     RegDisableReflectionKey(pHKey);103 104                     //获取访问的Key值105                     RegQueryValueEx(pHKey, keyName, 0, out lpType, result, ref resultSize);106 107                     //打开注册表转向(开启特定项的注册表反射)108                     RegEnableReflectionKey(pHKey);109                 }110 111                 //打开文件系统转向112                 Wow64RevertWow64FsRedirection(oldWOW64State);113 114                 //返回Key值115                 return result.ToString().Trim();116             }117             catch (Exception ex)118             {119                 return null;120             }121         }122 123         #endregion124     }125 }
--------------------编程问答-------------------- 发个地址给你自己看吧

地址 --------------------编程问答-------------------- 谢谢,先去看一下。 --------------------编程问答-------------------- 远程机器,非本机,怎么用?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,