• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1157
  • Last Modified:

NtDeleteFile usage

I'm wondering how can I use NtDeleteFile to delete C:\file.txt
It's being called from a Native Application, and the actual return code is something like -10279875....
lpPathName = "\\??\\C:\\file.txt"

Win XP SP2 + WDK 2008
PathNameString.Length = 19;
   PathNameString.Buffer = (WCHAR *)lpPathName;
   PathNameString.MaximumLength = PathNameString.Length+sizeof(WCHAR);
   
   ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
   ObjectAttributes.RootDirectory = NULL;
   ObjectAttributes.ObjectName = &PathNameString;
   ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE| OBJ_INHERIT;
   ObjectAttributes.SecurityDescriptor = NULL;
   ObjectAttributes.SecurityQualityOfService = NULL;
   
   errCode = NtDeleteFile(&ObjectAttributes);

Open in new window

0
xRootEE
Asked:
xRootEE
  • 2
  • 2
  • 2
1 Solution
 
and235100Commented:
Is this being run locally - i.e. not over the network?
These types of errors are normally down to network access-related issues...
0
 
jkrCommented:
>>PathNameString.Length = 19;
>>PathNameString.MaximumLength = PathNameString.Length+sizeof(WCHAR);

That is incorrect. The docs on UNICODE_STRING are misleading here, the length is *not* the amount of characters, but the *buffer* size in bytes, i.e.
PathNameString.Length = wcslen(lpPathName) * sizeof(WCHAR);
PathNameString.Buffer = (WCHAR *)lpPathName;
PathNameString.MaximumLength = PathNameString.Length+sizeof(WCHAR);
 
InitializeObjectAttributes( &ObjectAttributes, &PathNameString, OBJ_CASE_INSENSITIVE, NULL, NULL );
 
errCode = NtDeleteFile(&ObjectAttributes);

Open in new window

0
 
and235100Commented:
Please ignore my comment.
This is more of jkr's field than mine...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
xRootEEAuthor Commented:
That just works =)
Thank you very much.
0
 
jkrCommented:
BTW, you can simplify that and make it more error proof using
UNICODE_STRING PathNameString;
OBJECT_ATTRIBUTES ObjectAttributes;
 
RtlCreateUnicodeString(&PathNameString,lpPathName);
 
InitializeObjectAttributes( &ObjectAttributes, &PathNameString, OBJ_CASE_INSENSITIVE, NULL, NULL );
 
errCode = NtDeleteFile(&ObjectAttributes);

Open in new window

0
 
xRootEEAuthor Commented:
Thank you =) That's much better.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now