A question about OBJ_EXCLUSIVE!

Hi!

I want to hold an exclusive to a file, so I initialize object attributes with the flag OBJ_EXCLUSIVE set. But I can not open a handle to my file. My code is listed as below:

RtlInitUnicodeString( &fileName, HD_HMMA_HWINFO );

InitializeObjectAttributes( &gHmmaHwObj,
&fileName,
OBJ_EXCLUSIVE|OBJ_KERNEL_HANDLE,
NULL,
NULL
);
status = ZwCreateFile( &gHmmaHwHandle,
GENERIC_READ | GENERIC_WRITE,
&gHmmaHwObj,
&ioStatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OPEN_IF,
FILE_NON_DIRECTORY_FILE,
NULL,
0
);
if( !NT_SUCCESS(status) )
return status; //code here always be executed!!

Can someone tell me why I always fail in this way? Thanks!

> I want to hold an exclusive to a file, so I initialize object attributes with the flag OBJ_EXCLUSIVE set
Can the following be the reason (see highlighted)?

From http://msdn2.microsoft.com/en-US/library/aa491657.aspx:

OBJ_EXCLUSIVE If this flag is set and the OBJECT_ATTRIBUTES structure is passed to a routine that creates an object, then the object can optionally be accessed exclusively. That is, once a process opens such a handle to the object, no other processes can open handles to this object.

If this flag is set and the OBJECT_ATTRIBUTES structure is passed to a routine that creates an object handle, the caller is requesting exclusive access to the object for the process context that the handle was created in. This request can be granted only if the OBJ_EXCLUSIVE flag was set when the object was created.

----- Original Message -----
From:
To: “Windows File Systems Devs Interest List”
Sent: Sunday, November 12, 2006 8:02 AM
Subject: [ntfsd] A question about OBJ_EXCLUSIVE!

> Hi!
>
> I want to hold an exclusive to a file, so I initialize object attributes with the flag OBJ_EXCLUSIVE set. But I can not open a handle to my file. My code is listed as below:
>
> RtlInitUnicodeString( &fileName, HD_HMMA_HWINFO );
>
> InitializeObjectAttributes( &gHmmaHwObj,
> &fileName,
> OBJ_EXCLUSIVE|OBJ_KERNEL_HANDLE,
> NULL,
> NULL
> );
> status = ZwCreateFile( &gHmmaHwHandle,
> GENERIC_READ | GENERIC_WRITE,
> &gHmmaHwObj,
> &ioStatus,
> NULL,
> FILE_ATTRIBUTE_NORMAL,
> 0,
> FILE_OPEN_IF,
> FILE_NON_DIRECTORY_FILE,
> NULL,
> 0
> );
> if( !NT_SUCCESS(status) )
> return status; //code here always be executed!!
>
> Can someone tell me why I always fail in this way? Thanks!
>
> —
> Questions? First check the IFS FAQ at https://www.osronline.com/article.cfm?id=17
>
> You are currently subscribed to ntfsd as: xxxxx@comcast.net
> To unsubscribe send a blank email to xxxxx@lists.osr.com

Thanks, Alex! But I still do not know why my code always faisl to create an exclusive handle.

> But I still do not know why my code always faisl to create an exclusive

handle.
You already have 0 in “shared acsess”, that should be enough. From the DDK,
under ZwCreateFile:

“ShareAccess … Device and intermediate drivers usually set ShareAccess to
zero, which gives the caller exclusive access to the open file.”

You may want to look at the error code, it really helps.

My guesses:

  1. File does not exist, and you try to FILE_OPEN_IF it, which should fail in
    this case
    (FILE_OPEN_IF = “open existing files only, do not create a new one”);

  2. This file has been already opened exclusively by another process;

  3. You use UM-styled file name. Is it like L"\??\C:\myfile.zzz" ?

----- Original Message -----
From:
To: “Windows File Systems Devs Interest List”
Sent: Sunday, November 12, 2006 8:13 PM
Subject: RE:[ntfsd] A question about OBJ_EXCLUSIVE!

> Thanks, Alex! But I still do not know why my code always faisl to create
> an exclusive handle.
>
> —
> Questions? First check the IFS FAQ at
> https://www.osronline.com/article.cfm?id=17
>
> You are currently subscribed to ntfsd as: xxxxx@comcast.net
> To unsubscribe send a blank email to xxxxx@lists.osr.com