如何在 C# 中导入 ZwDeleteKey:"External component has thrown an exception"
How to import ZwDeleteKey in C#: "External component has thrown an exception"
我正在尝试使用 ZwDeleteKey
删除注册表符号 link。
我是这样导入的:
[DllImport("NtosKrnl.exe", CharSet = CharSet.Ansi, EntryPoint = "ZwDeleteKey", SetLastError = true)]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
但是在我调用它之后,我收到一个异常:
"External component has thrown an exception"
我的完整代码:
[DllImport("NtosKrnl.exe", CharSet = CharSet.Ansi, EntryPoint = "ZwDeleteKey", SetLastError = true)]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey);
}
static void Main(string[] args)
{
RegistryKey key;
key = OpenSubKeySymLink(Microsoft.Win32.Registry.CurrentUser, @"SOFTWARE\Microsoft\Windows\ABC", RegistryRights.ReadKey, 0);
ZwDeleteKey(key.Handle);
}
ZwDeleteKey 的官方文档确实提到“NtosKrnl.exe”是“DLL”,但这是错误的,导入定义应该是这样的(而且没有涉及字符串,条目点没有歧义并且函数没有设置最后一个错误):
[DllImport("ntdll")]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
我正在尝试使用 ZwDeleteKey
删除注册表符号 link。
我是这样导入的:
[DllImport("NtosKrnl.exe", CharSet = CharSet.Ansi, EntryPoint = "ZwDeleteKey", SetLastError = true)]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
但是在我调用它之后,我收到一个异常:
"External component has thrown an exception"
我的完整代码:
[DllImport("NtosKrnl.exe", CharSet = CharSet.Ansi, EntryPoint = "ZwDeleteKey", SetLastError = true)]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey);
}
static void Main(string[] args)
{
RegistryKey key;
key = OpenSubKeySymLink(Microsoft.Win32.Registry.CurrentUser, @"SOFTWARE\Microsoft\Windows\ABC", RegistryRights.ReadKey, 0);
ZwDeleteKey(key.Handle);
}
ZwDeleteKey 的官方文档确实提到“NtosKrnl.exe”是“DLL”,但这是错误的,导入定义应该是这样的(而且没有涉及字符串,条目点没有歧义并且函数没有设置最后一个错误):
[DllImport("ntdll")]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);