Error with ZWCreateFile

I am using ZWCreateFile to open a file on the drive. Code is below. I am
getting error “0xc0000043 (3221225539) - A file cannot be opened because
the share access flags are incompatible.” Any ideas why ?

NTSTATUS status = STATUS_SUCCESS;

UNICODE_STRING ustrOCPDrvPath ;

OBJECT_ATTRIBUTES oAttr;

IO_STATUS_BLOCK oIOstatus;

HANDLE phande;

DbgPrint(“OCPDRV:: Startup() Enter \n”);

RtlInitUnicodeString(&ustrOCPDrvPath,L"\DosDevices\C:\Driver\a.txt")
;

InitializeObjectAttributes (&oAttr,

&ustrOCPDrvPath,

(OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ),

NULL,

NULL );

DbgPrint(“OCPDRV:: InitializeObjectAttributes Succeeded
\n”);

status = ZwCreateFile(&phande,

(GENERIC_READ|SYNCHRONIZE ),

&oAttr,

&oIOstatus,

0, // allocation size

FILE_ATTRIBUTE_NORMAL, // file attributes

FILE_SHARE_READ, // share access

FILE_SUPERSEDE, // create disposition

FILE_SYNCHRONOUS_IO_NONALERT, // create options

NULL,
// ptr to extended attributes

0);

if(!NT_SUCCESS(status))

{

DbgPrint(“OCPDRV::ZWCreate failed status
= 0x%0x\n”, status);

}

DbgPrint(“OCPDRV:: Startup() Leave \n”);

Perhaps this will work for you:

status = ZwCreateFile( &phande,
SYNCHRONIZE | FILE_READ_DATA,
&oAttr,
&olOstatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0 );

P.S. - phande looks like a type-O, thinking it looks like it should be
phandle and not phande; if this spelling is consistent threw your
project then all is good here.

m.

Adnan Bhutta wrote:

I am using ZWCreateFile to open a file on the drive. Code is below. I
am getting error “*0xc0000043 (3221225539) - A file cannot be opened
because the share access flags are incompatible.” *Any ideas why ?

NTSTATUS status = STATUS_SUCCESS;

UNICODE_STRING ustrOCPDrvPath ;

OBJECT_ATTRIBUTES oAttr;

IO_STATUS_BLOCK oIOstatus;

HANDLE phande;

DbgPrint(“OCPDRV:: Startup() Enter \n”);

RtlInitUnicodeString(&ustrOCPDrvPath,L"\DosDevices\C:\Driver\a.txt");

InitializeObjectAttributes (&oAttr,

&ustrOCPDrvPath,

(OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ),

NULL,

NULL );

DbgPrint(“OCPDRV:: InitializeObjectAttributes Succeeded \n”);

status = ZwCreateFile(&phande,

(GENERIC_READ|SYNCHRONIZE ),

&oAttr,

&oIOstatus,

0, // allocation size

FILE_ATTRIBUTE_NORMAL, // file attributes

FILE_SHARE_READ, // share access

FILE_SUPERSEDE, // create disposition

FILE_SYNCHRONOUS_IO_NONALERT, // create options

NULL, // ptr to extended attributes

0);

if(!NT_SUCCESS(status))

{

DbgPrint(“OCPDRV::ZWCreate failed status = 0x%0x\n”, status);

}

DbgPrint(“OCPDRV:: Startup() Leave \n”);


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

oops, forgot to add the FILE_SUPERSEDE flag in that, and in your second
parameter (GENERIC_READ|SYNCHRONIZE ), SYNCHRONIZE is already part of
GENERIC_READ. That is probably what is pissing off zwCreate, however
that is a guess. I’ve never encountered that specific error, and the
zwcreate call I posted was a copy/paste job from a project I have that
does work…

m

MM wrote:

Perhaps this will work for you:

status = ZwCreateFile( &phande,
SYNCHRONIZE | FILE_READ_DATA,
&oAttr,
&olOstatus,
NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0 );

P.S. - phande looks like a type-O, thinking it looks like it should be
phandle and not phande; if this spelling is consistent threw your
project then all is good here.

m.

Adnan Bhutta wrote:

> I am using ZWCreateFile to open a file on the drive. Code is below. I
> am getting error “*0xc0000043 (3221225539) - A file cannot be opened
> because the share access flags are incompatible.” *Any ideas why ?
>
> NTSTATUS status = STATUS_SUCCESS;
>
> UNICODE_STRING ustrOCPDrvPath ;
>
> OBJECT_ATTRIBUTES oAttr;
>
> IO_STATUS_BLOCK oIOstatus;
>
> HANDLE phande;
>
> DbgPrint(“OCPDRV:: Startup() Enter \n”);
>
> RtlInitUnicodeString(&ustrOCPDrvPath,L"\DosDevices\C:\Driver\a.txt");
>
>
> InitializeObjectAttributes (&oAttr,
>
> &ustrOCPDrvPath,
>
> (OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ),
>
> NULL,
>
> NULL );
>
> DbgPrint(“OCPDRV:: InitializeObjectAttributes Succeeded \n”);
>
> status = ZwCreateFile(&phande,
>
> (GENERIC_READ|SYNCHRONIZE ),
>
> &oAttr,
>
> &oIOstatus,
>
> 0, // allocation size
>
> FILE_ATTRIBUTE_NORMAL, // file attributes
>
> FILE_SHARE_READ, // share access
>
> FILE_SUPERSEDE, // create disposition
>
> FILE_SYNCHRONOUS_IO_NONALERT, // create options
>
> NULL, // ptr to extended attributes
>
> 0);
>
> if(!NT_SUCCESS(status))
>
> {
>
> DbgPrint(“OCPDRV::ZWCreate failed status = 0x%0x\n”, status);
>
> }
>
> DbgPrint(“OCPDRV:: Startup() Leave \n”);
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

STATUS_SHARING_VIOLATION … my guess, exists handle for file, without
FILE_SHARE_READ?

“Adnan Bhutta” wrote in message
news:xxxxx@ntdev…
I am using ZWCreateFile to open a file on the drive. Code is below. I am
getting error “0xc0000043 (3221225539) - A file cannot be opened because the
share access flags are incompatible.” Any ideas why ?
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING ustrOCPDrvPath ;
OBJECT_ATTRIBUTES oAttr;
IO_STATUS_BLOCK oIOstatus;
HANDLE phande;
DbgPrint(“OCPDRV:: Startup() Enter \n”);
RtlInitUnicodeString(&ustrOCPDrvPath,L"\DosDevices\C:\Driver\a.txt");

InitializeObjectAttributes (&oAttr,

&ustrOCPDrvPath,

(OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ),

NULL,

NULL );

DbgPrint(“OCPDRV:: InitializeObjectAttributes Succeeded
\n”);

status = ZwCreateFile(&phande,

(GENERIC_READ|SYNCHRONIZE ),

&oAttr,

&oIOstatus,

0, // allocation size
FILE_ATTRIBUTE_NORMAL,
// file attributes
FILE_SHARE_READ,
// share access
FILE_SUPERSEDE,
// create disposition
FILE_SYNCHRONOUS_IO_NONALERT,
// create options
NULL,
// ptr to extended attributes
0);

if(!NT_SUCCESS(status))
{
DbgPrint(“OCPDRV::ZWCreate failed status =
0x%0x\n”, status);

}

DbgPrint(“OCPDRV:: Startup() Leave \n”);

You are trying to open a file for read while at the same time using
FILE_SUPERSEDE that might require more than read access.
Following is text from DDK on FILE_SUPERSEDE
*************
If the file already exists, replace it with the given file. If it does
not, create the given file.
************

Harish

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-253922-
xxxxx@lists.osr.com] On Behalf Of Lyndon J. Clarke
Sent: Monday, June 12, 2006 10:38 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Error with ZWCreateFile

STATUS_SHARING_VIOLATION … my guess, exists handle for file, without
FILE_SHARE_READ?

“Adnan Bhutta” wrote in message
> news:xxxxx@ntdev…
> I am using ZWCreateFile to open a file on the drive. Code is below. I
am
> getting error “0xc0000043 (3221225539) - A file cannot be opened
because
> the
> share access flags are incompatible.” Any ideas why ?
> NTSTATUS status = STATUS_SUCCESS;
> UNICODE_STRING ustrOCPDrvPath ;
> OBJECT_ATTRIBUTES oAttr;
> IO_STATUS_BLOCK oIOstatus;
> HANDLE phande;
> DbgPrint(“OCPDRV:: Startup() Enter \n”);
>
>
RtlInitUnicodeString(&ustrOCPDrvPath,L"\DosDevices\C:\Driver\a.txt")
;
>
> InitializeObjectAttributes (&oAttr,
>
> &ustrOCPDrvPath,
>
> (OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE ),
>
> NULL,
>
> NULL );
>
> DbgPrint(“OCPDRV:: InitializeObjectAttributes
Succeeded
> \n”);
>
> status = ZwCreateFile(&phande,
>
> (GENERIC_READ|SYNCHRONIZE ),
>
> &oAttr,
>
> &oIOstatus,
>
> 0, // allocation
size
>
> FILE_ATTRIBUTE_NORMAL,
> // file attributes
>
FILE_SHARE_READ,
> // share access
>
FILE_SUPERSEDE,
> // create disposition
>
> FILE_SYNCHRONOUS_IO_NONALERT,
> // create options
> NULL,
> // ptr to extended attributes
> 0);
>
> if(!NT_SUCCESS(status))
> {
> DbgPrint(“OCPDRV::ZWCreate failed
status =
> 0x%0x\n”, status);
>
> }
>
> DbgPrint(“OCPDRV:: Startup() Leave \n”);
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer