在阅读了这篇关于SO的文章后,我试图编写一个我需要读取和写入隐藏注册表项/值的小应用程序。
我检查了注册表操作使用NT原生api和创建"隐藏"注册表值链接。
第一个给了我一些工作,但它是用c++写的,第二个是一个Delphi项目,运行良好。
我不能首先转换,我可以尝试转换第二,但我需要找到一些代码来读取键/值。出于这个原因,我想知道是否有什么东西"准备好"并在c#中进行了测试。我还下载了proceshacker v1.11源代码,并使用它来部分转换Delphi示例如下所示,但隐藏的注册表项是可访问的(而在Delphi中它不是),并且没有api来写值。
static void Main(string[] args)
{
string KeyNameBuffer = @"RegistryUserS-1-5-21-3979903645-2167650815-2353538381-1001SOFTWARE";
string NewKeyNameBuffer = "Systems Internals";
string HiddenKeyNameBuffer = "Can't touch me ";
string HiddenValueNameBuffer = "Hidden Value";
// Apro la chiave di registro
IntPtr SoftwareKeyHandle = CreateKey(KeyNameBuffer, IntPtr.Zero);
if (SoftwareKeyHandle != IntPtr.Zero)
{
IntPtr SysKeyHandle = CreateKey(NewKeyNameBuffer, SoftwareKeyHandle);
if (SysKeyHandle != IntPtr.Zero)
{
// This key shouldn't be accessible, but it is
IntPtr HiddenKeyHandle = CreateKey(HiddenKeyNameBuffer, SysKeyHandle);
if (HiddenKeyHandle != IntPtr.Zero)
{
// I don't have APIs to write values
}
}
}
}
static IntPtr CreateKey(string keyName, IntPtr rootKey)
{
IntPtr res;
KeyCreationDisposition disp;
ObjectAttributes attributes = new ObjectAttributes(keyName,
ObjectFlags.CaseInsensitive,
new NativeHandle(rootKey));
NtStatus st = Win32.NtCreateKey(out res, KeyAccess.All,
ref attributes, 0,
IntPtr.Zero, RegOptions.NonVolatile, out disp);
return st == NtStatus.Success ? res : IntPtr.Zero;
}
最后:从Vista开始,如果你不以管理员身份运行你的应用程序,你就不能写RegistryMachine
部分,所以在这个例子中我使用了我的用户注册表项。如果我需要存储每台机器的值,是否有一种方法可以让我们使用本机api来编写注册表的那一部分?
如果你想在HKLM中使用它,而特权不允许你使用,不管你使用哪个API层,Nt* ones的Reg*函数-它不会让你在访问拒绝错误时这样做。