ntopensymboliclinkobject没有成功获得符号链接句柄
我正在尝试删除我创建的符号链接。
您可以在对象管理器(WinoBJ)中看到它:
我正在尝试删除该文件夹。我看到在这里我需要打开一个用ntopensymboliclinkobject
,然后使用<代码> ntmaketemporaryObject 删除它。
我尝试使用deletesymboliclink
的许多不同调用进行操作,并且功能ntopensymboliclinkobject
无法成功获得处理。这是我使用的代码:
[DllImport("ntdll.dll")]
public static extern int NtOpenSymbolicLinkObject(
out SafeFileHandle LinkHandle,
uint DesiredAccess,
ref ObjectAttributes ObjectAttributes);
public enum NtStatus : uint
{
Success = 0x00000000
}
[DllImport("ntdll.dll")]
public static extern NtStatus NtMakeTemporaryObject(SafeFileHandle Handle);
[DllImport("ntdll.dll")]
static extern int NtClose(SafeFileHandle handle);
public static void DeleteSymbolicLink(SafeKernelObjectHandle directory, string path){
uint DELETE = 0x00010000;
SafeFileHandle handle;
ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null);
if (NtOpenSymbolicLinkObject( out handle, DELETE, ref obja) == 0x00000000)
{
NtMakeTemporaryObject( handle);
NtClose( handle);
}
}
public static void CreateSymlink(string inputPath, string outPath)
{
string inputFilename = Path.GetFileName(inputPath);
string inputDir = Path.GetDirectoryName(inputPath);
JunctionPoint.Create(@"\RPC Control", inputDir, true);
CreateSymbolicLink(null, @"\RPC Control\" + inputFilename, outPath);
}
static SafeKernelObjectHandle CreateSymbolicLink(SafeKernelObjectHandle directory, string path, string target)
{
using (ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null))
{
IntPtr handle;
StatusToNtException(NtCreateSymbolicLinkObject(out handle, GenericAccessRights.MaximumAllowed, obja, new UnicodeString(target)));
return new SafeKernelObjectHandle(handle, true);
}
}
static void Main(string[] args){
CreateSymlink("C:\tmp\MyFile.txt", fileToDelete);
DeleteSymbolicLink(null, @"\RPC Control\" + "\??\C:\Windows\win2.ini");
DeleteSymbolicLink(null, @"\RPC Control\" + "MyFile.txt");
DeleteSymbolicLink(null, @"\RPC Control\" + "C:\tmp\MyFile.txt");
DeleteSymbolicLink(null, "MyFile.txt");
DeleteSymbolicLink(null, "C:\tmp\MyFile.txt");
DeleteSymbolicLink(null, "\??\C:\Windows\win2.ini");
}
I am trying to remove a symbolic link that I created.
You can see it in the Object manager (WinObj):
I am trying to delete the folder. I saw here that I need to open a handle to object with NtOpenSymbolicLinkObject
and then use NtMakeTemporaryObject
to delete it.
I tried to do it with number of different calls to DeleteSymbolicLink
, and the function NtOpenSymbolicLinkObject
doesn't succeed to get handle. Here is the code I used:
[DllImport("ntdll.dll")]
public static extern int NtOpenSymbolicLinkObject(
out SafeFileHandle LinkHandle,
uint DesiredAccess,
ref ObjectAttributes ObjectAttributes);
public enum NtStatus : uint
{
Success = 0x00000000
}
[DllImport("ntdll.dll")]
public static extern NtStatus NtMakeTemporaryObject(SafeFileHandle Handle);
[DllImport("ntdll.dll")]
static extern int NtClose(SafeFileHandle handle);
public static void DeleteSymbolicLink(SafeKernelObjectHandle directory, string path){
uint DELETE = 0x00010000;
SafeFileHandle handle;
ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null);
if (NtOpenSymbolicLinkObject( out handle, DELETE, ref obja) == 0x00000000)
{
NtMakeTemporaryObject( handle);
NtClose( handle);
}
}
public static void CreateSymlink(string inputPath, string outPath)
{
string inputFilename = Path.GetFileName(inputPath);
string inputDir = Path.GetDirectoryName(inputPath);
JunctionPoint.Create(@"\RPC Control", inputDir, true);
CreateSymbolicLink(null, @"\RPC Control\" + inputFilename, outPath);
}
static SafeKernelObjectHandle CreateSymbolicLink(SafeKernelObjectHandle directory, string path, string target)
{
using (ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null))
{
IntPtr handle;
StatusToNtException(NtCreateSymbolicLinkObject(out handle, GenericAccessRights.MaximumAllowed, obja, new UnicodeString(target)));
return new SafeKernelObjectHandle(handle, true);
}
}
static void Main(string[] args){
CreateSymlink("C:\tmp\MyFile.txt", fileToDelete);
DeleteSymbolicLink(null, @"\RPC Control\" + "\??\C:\Windows\win2.ini");
DeleteSymbolicLink(null, @"\RPC Control\" + "MyFile.txt");
DeleteSymbolicLink(null, @"\RPC Control\" + "C:\tmp\MyFile.txt");
DeleteSymbolicLink(null, "MyFile.txt");
DeleteSymbolicLink(null, "C:\tmp\MyFile.txt");
DeleteSymbolicLink(null, "\??\C:\Windows\win2.ini");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论