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

xRootEEAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
and235100Commented:
Please ignore my comment.
This is more of jkr's field than mine...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.