读取64位系统注册表,判断操作系统
来源:互联网 发布:高端社交软件 编辑:程序博客网 时间:2024/06/05 19:50
转自:读取64位注册表 http://blog.sina.com.cn/s/blog_5f8817250100voon.html
C# 判断windows版本 位数 http://blog.csdn.net/zfd_007/article/details/5881269
参考:http://www.codeproject.com/Articles/51326/Net-Compilation-registry-accessing-and-applicatio
1. 读取64位操作系统注册表
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int sam, out IntPtr phkResult);[Flags]public enum eRegWow64Options : int{ None = 0x0000, KEY_WOW64_64KEY = 0x0100, KEY_WOW64_32KEY = 0x0200, // Add here any others needed, from the table of the previous chapter} [Flags]public enum eRegistryRights : int{ ReadKey = 131097, WriteKey = 131078,}public static RegistryKey OpenSubKey(RegistryKey pParentKey, string pSubKeyName, bool pWriteable, eRegWow64Options pOptions){ if (pParentKey == null || GetRegistryKeyHandle(pParentKey).Equals(System.IntPtr.Zero)) throw new System.Exception("OpenSubKey: Parent key is not open"); eRegistryRights Rights = eRegistryRights.ReadKey; if (pWriteable) Rights = eRegistryRights.WriteKey; System.IntPtr SubKeyHandle; System.Int32 Result = RegOpenKeyEx(GetRegistryKeyHandle(pParentKey), pSubKeyName, 0, (int)Rights | (int)pOptions, out SubKeyHandle); if (Result != 0) { System.ComponentModel.Win32Exception W32ex = new System.ComponentModel.Win32Exception(); throw new System.Exception("OpenSubKey: Exception encountered opening key", W32ex); } return PointerToRegistryKey(SubKeyHandle, pWriteable, false);} private static System.IntPtr GetRegistryKeyHandle(RegistryKey pRegisteryKey){ Type Type = Type.GetType("Microsoft.Win32.RegistryKey"); FieldInfo Info = Type.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance); SafeHandle Handle = (SafeHandle)Info.GetValue(pRegisteryKey); IntPtr RealHandle = Handle.DangerousGetHandle(); return Handle.DangerousGetHandle();} private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool pWritable, bool pOwnsHandle){ // Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic; Type safeRegistryHandleType = typeof( SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType( "Microsoft.Win32.SafeHandles.SafeRegistryHandle"); Type[] safeRegistryHandleConstructorTypes = new Type[] { typeof(System.IntPtr), typeof(System.Boolean) }; ConstructorInfo safeRegistryHandleConstructor = safeRegistryHandleType.GetConstructor(privateConstructors, null, safeRegistryHandleConstructorTypes, null); Object safeHandle = safeRegistryHandleConstructor.Invoke(new Object[] { hKey, pOwnsHandle }); // Create a new Registry key using the private constructor using the // safeHandle - this should then behave like // a .NET natively opened handle and disposed of correctly Type registryKeyType = typeof(Microsoft.Win32.RegistryKey); Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(Boolean) }; ConstructorInfo registryKeyConstructor = registryKeyType.GetConstructor(privateConstructors, null, registryKeyConstructorTypes, null); RegistryKey result = (RegistryKey)registryKeyConstructor.Invoke(new Object[] { safeHandle, pWritable }); return result;}
How to use the Code
The OpenSubKey will return the searched key, allowing you to specify reading from the normal registry, or from the alternative 32-bit, WOW64 registry. The following example reads from the 32-bit WOW64 registry:
try { RegistryKey key = OpenSubKey(Registry.LocalMachine,"Software\\[Key]",false, eRegWow64Options.KEY_WOW64_32KEY); }catch { // Parent key not open, exception found at opening (probably related to // security permissions requested)}
2.判断操作系统 位数
[StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { internal short wProcessorArchitecture; internal short wReserved; internal int dwPageSize; internal IntPtr lpMinimumApplicationAddress; internal IntPtr lpMaximumApplicationAddress; internal IntPtr dwActiveProcessorMask; internal int dwNumberOfProcessors; internal int dwProcessorType; internal int dwAllocationGranularity; internal short wProcessorLevel; internal short wProcessorRevision; } /// <summary> /// 操作系统操作类(获取系统版本、位数) /// </summary> public class WindowsHelper { /// <summary> /// Windows系统版本 /// </summary> public enum WindowsVersion { Unknown, Win95, Win98, WinME, NT351, NT40, Win2K, XP, Win2003, Vista, Win7 }; /// <summary> /// Windows系统操作系统位数 /// </summary> public enum WindowsBits { Unknown, OS16Bits, OS32Bits, OS64Bits }; private const short PROCESSOR_ARCHITECTURE_INTEL = 0; private const short PROCESSOR_ARCHITECTURE_IA64 = 6; private const short PROCESSOR_ARCHITECTURE_AMD64 = 9; private const int PROCESSOR_ARCHITECTURE_UNKNOWN = 0x00FFFF; [DllImport("kernel32.dll")] static extern void GetNativeSystemInfo(out SYSTEM_INFO lpSystemInfo); private static bool IsPlatformX64() { SYSTEM_INFO sysinfo = new SYSTEM_INFO(); bool s_OK = false; GetNativeSystemInfo(out sysinfo); switch (sysinfo.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_IA64: s_OK = true; break; case PROCESSOR_ARCHITECTURE_AMD64: s_OK = true; break; default: s_OK = false; break; } return s_OK; } /// <summary> /// 获取操作系统的版本 /// </summary> /// <returns></returns> public static WindowsVersion GetWindowsVersion() { WindowsVersion version = WindowsVersion.XP; OperatingSystem osInfo = Environment.OSVersion; switch (osInfo.Platform) { case System.PlatformID.Win32Windows: switch (osInfo.Version.Minor) { case 0: version = WindowsVersion.Win95; break; case 10: version = WindowsVersion.Win98; break; case 90: version = WindowsVersion.WinME; break; } break; case System.PlatformID.Win32NT: switch (osInfo.Version.Major) { case 3: version = WindowsVersion.NT351; break; case 4: version = WindowsVersion.NT40; break; case 5: switch (osInfo.Version.Minor) { case 0: version = WindowsVersion.Win2K; break; case 1: version = WindowsVersion.XP; break; default: version = WindowsVersion.Win2003; break; } break; case 6: switch (osInfo.Version.Minor) { case 0: version = WindowsVersion.Vista; break; default: version = WindowsVersion.Win7; break; } break; default: version = WindowsVersion.Win7; break; } break; } return version; } /// <summary> /// 获取操作系统的位数 /// </summary> /// <returns></returns> public static WindowsBits GetWindowsBits() { WindowsBits OSBits; if (IsPlatformX64()) { OSBits = WindowsBits.OS64Bits; } else { OSBits = WindowsBits.OS32Bits; } return OSBits; } }
- 读取64位系统注册表,判断操作系统
- Windows64位系统注册表读取
- x86程序读取64位系统注册表失败解决方案
- 32位QT程序读取64位系统的注册表中的64位安装软件
- How to:Installshield判断操作系统是否为64位,并且为操作注册表进行设置
- delphi 判断是64位系统还是32为操作系统
- delphi 判断是64位系统还是32为操作系统
- 关于64位Windows操作系统中的注册表
- 关于64位Windows操作系统中的注册表
- 64位Windows操作系统中的注册表
- 64位操作系统注册表与C#对注册表操作
- 64位的win7系统, 32位的应用程序读取64位模式的注册表值失败
- Windows7 64位系统中,读取注册表,文件路劲重定位解决方案
- bat批处理判断系统32位或64位以及操作系统类型
- 64位读取注册表与32位的区别
- 64位读取注册表与32位的区别
- 64位读取注册表与32位的区别
- 关于64位Windows操作系统中的注册表 zz
- Linux进程模型总结
- java 读取配置文件
- 22个Web 编辑器
- mtk android adb读取pmic寄存器
- 在VC中如何找到崩溃的源头(二)
- 读取64位系统注册表,判断操作系统
- json 数据格式 示例
- Sublime Text 2 注册码/破解方法
- C#中关于String.Equals(object,object)和(object==object )的比较
- YUV的图像格式
- ubuntu apache配置信息
- 高斯滤波 openCV实现
- Windows server 2008 HTTP.sys被禁用导致80端口被禁用和IIS服务无法启动解决办法
- Spring MVC 三 基础学习