我正在使用 .NET 4 和新的 RegistryKey.FromHandle打电话,这样我就可以获取通过使用 RegLoadAppKey 打开软件注册表文件获得的 hKey并使用现有的托管 API 对其进行操作。
起初我认为这只是 DllImport 失败的问题,我的调用在参数中有无效类型或缺少 MarshalAs 或其他任何东西,但查看其他注册表函数及其 DllImport 声明(例如,在 pinvoke.net),我看不出还有什么可以尝试的(我已经将 hKey 作为 int 和 IntPtr 返回,它们都在 32 位操作系统上工作,但在 64 位操作系统上失败)
我已将其简化为尽可能简单的重现案例——它只是尝试创建一个“随机”子项,然后向其写入一个值。它在我的 Win7 x86 机器上运行良好,但在 Win7 x64 和 2008 R2 x64 上失败,即使它仍然是 32 位进程,甚至从 32 位 cmd 提示符运行。编辑:如果它是 64 位进程,它也会以同样的方式失败。 编辑:如果传入的文件为空,它工作正常 - 问题是针对现有的软件注册表配置单元。我从 2008 r2 (x64) 和 WHS v1 (x86) iso 中提取了“裸”软件注册表配置单元文件,它们都有同样的问题。
在 Win7 x86 上:
INFO: Running as Admin in 32-bit process on 32-bit OS
Was able to create Microsoft\Windows\CurrentVersion\RunOnceEx\a95b1bbf-7a04-4707-bcca-6aee6afbfab7 and write a value under it
在 Win7 x64 上,作为 32 位:
INFO: Running as Admin in 32-bit process on 64-bit OS
Unhandled Exception: System.UnauthorizedAccessException: Access to the registry key '\Microsoft\Windows\CurrentVersion\RunOnceEx\ce6d5ff6-c3af-47f7-b3dc-c5a1b9a3cd22' is denied.
at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKey.CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, Object registrySecurityObj, RegistryOptions registryOptions)
at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey)
at LoadAppKeyAndModify.Program.Main(String[] args)
在 Win7 x64 上,作为 64 位:
INFO: Running as Admin in 64-bit process on 64-bit OS
Unhandled Exception: System.UnauthorizedAccessException: Access to the registry key '\Microsoft\Windows\CurrentVersion\RunOnceEx\43bc857d-7d07-499c-8070-574d6732c130' is denied.
at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKey.CreateSubKeyInternal(String subkey, RegistryKeyPermissionCheck permissionCheck, Object registrySecurityObj, RegistryOptions registryOptions)
at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck)
at LoadAppKeyAndModify.Program.Main(String[] args)
来源:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("INFO: Running as {0} in {1}-bit process on {2}-bit OS",
new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) ? "Admin" : "Normal User",
Environment.Is64BitProcess ? 64 : 32,
Environment.Is64BitOperatingSystem ? 64 : 32);
if (args.Length != 1)
{
throw new ApplicationException("Need 1 argument - path to the software hive file on disk");
}
string softwareHiveFile = Path.GetFullPath(args[0]);
if (File.Exists(softwareHiveFile) == false)
{
throw new ApplicationException("Specified file does not exist: " + softwareHiveFile);
}
// pick a random subkey so it doesn't already exist
var existingKeyPath = @"Microsoft\Windows\CurrentVersion";
var keyPathToCreate = @"RunOnceEx\" + Guid.NewGuid();
var completeKeyPath = Path.Combine(existingKeyPath, keyPathToCreate);
var hKey = RegistryNativeMethods.RegLoadAppKey(softwareHiveFile);
using (var safeRegistryHandle = new SafeRegistryHandle(new IntPtr(hKey), true))
using (var appKey = RegistryKey.FromHandle(safeRegistryHandle))
using (var currentVersionKey = appKey.OpenSubKey(existingKeyPath, true))
{
if (currentVersionKey == null)
{
throw new ApplicationException("Specified file is not a well-formed software registry hive: " + softwareHiveFile);
}
using (var randomSubKey = currentVersionKey.CreateSubKey(keyPathToCreate))
{
randomSubKey.SetValue("foo", "bar");
Console.WriteLine("Was able to create {0} and write a value under it", completeKeyPath);
}
}
}
}
internal static class RegistryNativeMethods
{
[Flags]
public enum RegSAM
{
AllAccess = 0x000f003f
}
private const int REG_PROCESS_APPKEY = 0x00000001;
// approximated from pinvoke.net's RegLoadKey and RegOpenKey
// NOTE: changed return from long to int so we could do Win32Exception on it
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int RegLoadAppKey(String hiveFile, out int hKey, RegSAM samDesired, int options, int reserved);
public static int RegLoadAppKey(String hiveFile)
{
int hKey;
int rc = RegLoadAppKey(hiveFile, out hKey, RegSAM.AllAccess, REG_PROCESS_APPKEY, 0);
if (rc != 0)
{
throw new Win32Exception(rc, "Failed during RegLoadAppKey of file " + hiveFile);
}
return hKey;
}
}
最佳答案
最终打开了 Microsoft 支持的支持案例 - 问题特定于 1) 最新版本的 Windows 安装媒体上附带的配置单元和 2) 作为 API 的 RegLoadAppKey。切换到 RegLoadKey/RegUnLoadKey 而不是对完全相同的文件(甚至在相同的进程中)工作得很好,并且由于 RegLoadAppKey 中的错误不太可能得到修复(更不用说很快)来处理那些特定的文件,我只是切换到改为 RegLoadKey/RegUnLoadKey。
关于c# - RegLoadAppKey 在 32 位操作系统上运行良好,在 64 位操作系统上失败,即使两个进程都是 32 位的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2917309/