title: NtOpenSymbolicLinkObject doesn't succeed to get symbolic link handle
body: <p>I am trying to remove a symbolic link that I created.<br />
  You can see it in the Object manager (WinObj):<br />
  <a href="https://i.stack.imgur.com/V9z3L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V9z3L.png" alt="enter image description here" /></a></p>
  <p>I am trying to delete the folder. I saw <a href="https://stackoverflow.com/a/63567810/2153777">here</a> that I need to open a handle to object with <code>NtOpenSymbolicLinkObject</code> and then use <code>NtMakeTemporaryObject</code> to delete it.</p>
  <p>I tried to do it with number of different calls to <code>DeleteSymbolicLink</code>, and the function <code>NtOpenSymbolicLinkObject</code> doesn't succeed to get handle. Here is the code I used:</p>
  <pre><code>[DllImport(&quot;ntdll.dll&quot;)]
  public static extern int NtOpenSymbolicLinkObject(
  out SafeFileHandle LinkHandle,
  uint DesiredAccess,
  ref ObjectAttributes ObjectAttributes);
  
  public enum NtStatus : uint
  {
      Success = 0x00000000
  }
  
  [DllImport(&quot;ntdll.dll&quot;)]
  public static extern NtStatus NtMakeTemporaryObject(SafeFileHandle Handle);
  
  
  [DllImport(&quot;ntdll.dll&quot;)]
  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(@&quot;\RPC Control&quot;, inputDir, true);
      CreateSymbolicLink(null, @&quot;\RPC Control\&quot; + 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(&quot;C:\tmp\MyFile.txt&quot;, fileToDelete);
      DeleteSymbolicLink(null, @&quot;\RPC Control\&quot; + &quot;\??\C:\Windows\win2.ini&quot;);
      DeleteSymbolicLink(null, @&quot;\RPC Control\&quot; + &quot;MyFile.txt&quot;);
      DeleteSymbolicLink(null, @&quot;\RPC Control\&quot; + &quot;C:\tmp\MyFile.txt&quot;);
      DeleteSymbolicLink(null, &quot;MyFile.txt&quot;);
      DeleteSymbolicLink(null, &quot;C:\tmp\MyFile.txt&quot;);
      DeleteSymbolicLink(null, &quot;\??\C:\Windows\win2.ini&quot;);
  }
  
  
  </code></pre>