- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近编写了一个模拟用户帐户的应用程序,获取 CURRENT_USER 注册表项的句柄(使用 PInvoke“LoadUserProfile”检索 ProfileInfo.hProfile 对象)并使用 RegistryKey.FromHandle 创建注册表项。
引用代码:
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(hToken))
{
using (SafeRegistryHandle safeHandle = new SafeRegistryHandle(hProfile, true))
{
using (RegistryKey impersonatedUserHkcu = RegistryKey.FromHandle(safeHandle, RegistryView.Default))
{
// Do something with registry
}
}
}
这段代码运行良好(在 Windows 7 中运行),但使用了仅受 .NET 4.0 及更高版本支持的对象/方法(SafeRegistryHandle、RegistryKey.FromHandle()、RegistryView 枚举)。
现在,我需要使这个应用程序与 .NET 3.5 兼容,以便在装有 Windows XP 的机器上使用它,并且不可能安装 .NET Framework 4.0。
是否有任何等效对象可以与 .NET 3.5 一起使用来实现相同的结果? (即,为模拟用户修改注册表项)。还是仅存在 .NET 4 对象的某种源代码?
最佳答案
经过几天的研究,and help from MSDN community to the same question ,我找到了满足我的需求的方法。
最初的建议是使用 Win Api 函数 RegOpenKeyEx
(有关信息和示例,请参阅 P/Invoke website);但根据this MSDN article , 我发现了
If your service or application impersonates different users, do not use this function with HKEY_CURRENT_USER. Instead, call the RegOpenCurrentUser function.
最后,要走的路是 RegOpenCurrentUser
函数。 (不幸的是,在 P/Invoke 网站上仍然没有关于此功能的踪迹,但您可以找到一些信息 on MSDN )
我目前是这样定义它的:
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int RegOpenCurrentUser(int samDesired, out IntPtr phkResult);
public enum RegistrySecurity
{
KEY_ALL_ACCESS = 0xF003F,
KEY_CREATE_LINK = 0x0020,
KEY_CREATE_SUB_KEY = 0x0004,
KEY_ENUMERATE_SUB_KEYS = 0x0008,
KEY_EXECUTE = 0x20019,
KEY_NOTIFY = 0x0010,
KEY_QUERY_VALUE = 0x0001,
KEY_READ = 0x20019,
KEY_SET_VALUE = 0x0002,
KEY_WOW64_32KEY = 0x0200,
KEY_WOW64_64KEY = 0x0100,
KEY_WRITE = 0x20006,
}
public IntPtr GetImpersonateUserRegistryHandle(RegistrySecurity _access)
{
IntPtr safeHandle = new IntPtr();
int result = RegOpenCurrentUser((int)_access, out safeHandle);
return safeHandle;
}
/// <summary>
/// Get a registry key from a pointer.
/// </summary>
/// <param name="hKey">Pointer to the registry key</param>
/// <param name="writable">Whether or not the key is writable.</param>
/// <param name="ownsHandle">Whether or not we own the handle.</param>
/// <returns>Registry key pointed to by the given pointer.</returns>
public RegistryKey _pointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle)
{
//Get the BindingFlags for private contructors
System.Reflection.BindingFlags privateConstructors = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
//Get the Type for the SafeRegistryHandle
Type safeRegistryHandleType =
typeof(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
//Get the array of types matching the args of the ctor we want
Type[] safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) };
//Get the constructorinfo for our object
System.Reflection.ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(
privateConstructors, null, safeRegistryHandleCtorTypes, null);
//Invoke the constructor, getting us a SafeRegistryHandle
Object safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle });
//Get the type of a RegistryKey
Type registryKeyType = typeof(RegistryKey);
//Get the array of types matching the args of the ctor we want
Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(bool) };
//Get the constructorinfo for our object
System.Reflection.ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(
privateConstructors, null, registryKeyConstructorTypes, null);
//Invoke the constructor, getting us a RegistryKey
RegistryKey resultKey = (RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable });
//return the resulting key
return resultKey;
}
这就是我使用它获取注册表的方式:
IntPtr localRegistryHandle = GetImpersonateUserRegistryHandle(TestRegistryAccess.RegistrySecurity.KEY_ALL_ACCESS);
using(RegistryKey localRegistry = _pointerToRegistryKey(localRegistryHandle, true, true))
{
// do something with local registry
}
关于c# - 访问模拟用户的 CurrentUser 注册表项 - 与 .NET 3.5 的兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477370/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
如何打开注册表 Win98/98SE/Me 运行中输入regedit.exe 无权限限制 Win2000/XP 1·开始>>运行.中输入regedit 2.运行中
目录 前言 使用简单工厂改进 使用注册表改进 参考文章 前言 在上一篇文章中我们提到了抽象工厂模式初版代码的一些缺点:①客户端违反开闭原则②提供方违反开
目录 工厂方法模式的瑕疵 注册表 工厂方法模式的瑕疵 在前一篇笔记中我们介绍了工厂方法模式,示例的类图如下: 考虑一种情况:现在要在程序运行时,根据外部资源
我在一个带有一个主节点和两个节点的 AWS 集群中安装了 mesosphere,分别称为主节点、节点 1 和节点 2。主节点是可见的,节点是私有(private)的。 我得到一个默认运行的马拉松实例和
我想用 PHP 制作一个注册表,将他们的用户名和密码注册到我的 SQL 数据库中。这是我所拥有的: 配置.php: 索引.php: Deltalus Account Regi
我正在尝试将我的软件添加到注册表中,我找到了一些我可以使用的代码,但不是完整的工作代码 C/C++ 对我来说是新的,无法自己创建它。但基本思想是:检查 reg key 是否已设置(如果未创建)。 我能
我尝试创建一个注册 php 代码,这样当我不在计算机旁时我就可以注册人们,但由于某种原因,它不会创建表,也不会创建文件夹,即使不存在。 输出是: ( ! ) Notice: Undefined var
我使用 HKLM\Software\etc\etc 来存储我的基本设置,这基本上是我的程序及其相关数据库的位置。 我现在遇到的问题是我已经创建了一个 Windows 服务并且需要读取这些位置。我无法将
我正在尝试将我的软件添加到注册表中,我找到了一些我可以使用的代码片段,但不是完整的工作代码 C/C++ 对我来说是新的,我无法自己创建它。但这里是基本的想法:如果没有创建它,请检查是否设置了注册键。
1、保存以下代码到:c:\windows\system32\vdm.vbs 复制代码 代码如下: Dim Args Set Args = WScript.Argum
不懂注册表、VBS、批处理不要紧,只要会学、会吸收别人的精华,会查找资料,|论坛|百度|GOOGLE|,然后能用好,就非常不错了 贴一些搜集的文字放到这吧。(绿色是标题, 蓝色的是原作者
(绿色是标题, 蓝色的是原作者,红色的部分就是需要修改的部分,根据你自己的实际情况修改) ======打开迅闪菜单的同时连接虚拟磁盘的VBS====== Set WshShell = WScrip
简单使用 复制代码 代码如下: <?php require_once("Zend/Loader.php"); Zend_Loader::loadCl
但并不是每次都打开同一个键值,在每多时候我们还是习惯于Windows 98那样每次打开都在根目录中。 通过修改注册表可以达到这样的效果。打开HKEY_CURRENT_USER\Software\M
何谓REG文件 REG文件实际上是一种注册表脚本文件,双击REG文件即可将其中的数据导入到注册表中。利用REG文件我们可以直接对注册表进行任何修改操作,它对注册表的操作可以不受注册表编辑器被禁用的
我应该将哪些数据和位置添加到 Windows 注册表中以正确创建文件关联? 我问这个问题主要是因为我多年前编写的一些旧应用程序使用 Visual Studio .NET 的 Windows Insta
我想阻止对默认 docker.io 注册表的访问。为了安全/IP 保护,我们需要阻止对公共(public) Docker 集线器的推/拉访问。 已有许多尝试将此作为配置选项,但所有 PR 都不断被拒绝
Lua 提供了一个 "registry" “任何 C 代码都可以使用它来存储它需要存储的任何 Lua 值”。正如 here 所说,线程“与原始线程共享其全局环境” .但是我找不到任何地方说它是否共享注
我的软件在 HKCU\Software\mysoftware 中存储了一些值,这从来都不是问题。但是,我现在还在 HKCU\Software\mysoftware\Licenses 中存储了一些值 -
我是一名优秀的程序员,十分优秀!