CreateFile failed with Error code C0000005

Hi All,

I am writing a non pnp driver to handle IOCTL requests from my application.
I have a below callback function registered in the driver to handle CreateFile routine from application when does request.

EVT_WDF_DEVICE_FILE_CREATE MyDeviceFileCreate;
//

VOID
MyEvtDeviceFileCreate (
IN WDFDEVICE Device,
IN WDFREQUEST Request,
IN WDFFILEOBJECT FileObject
)
{
//-------------------------------------
//---------------------------------------

status = ZwCreateFile (
&devExt->FileHandle,
SYNCHRONIZE | GENERIC_WRITE | GENERIC_READ,
&fileAttributes,
&ioStatus,
NULL,// alloc size = none
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN_IF,
FILE_SYNCHRONOUS_IO_NONALERT |FILE_NON_DIRECTORY_FILE,
NULL,// eabuffer
0// ealength
);
//-------------------------------------
//---------------------------------------
}
//

From application side it is required to create two different handles for the same file, one for Synchronous IO and another for Asynchronous IO as shown below.

Void init()
{

//-------------------------------------
//------------------------------------------------------
//Calling createfile to get a handle for Synchronous IO
//------------------------------------------------------

m_hDevice = CreateFile(szDriverPath,
GENERIC_READ | GENERIC_WRITE,// read and write enabled
0, // share mode
NULL, // security
OPEN_EXISTING, // fails if does not exist
FILE_ATTRIBUTE_NORMAL, // attributes
NULL // template file
);

//------------------------------------------------------
//Calling createfile to get a handle for Asynchronous IO
//------------------------------------------------------

m_hDeviceAsync = CreateFile(szDriverPath,
GENERIC_READ| GENERIC_WRITE,// read and write enabled
0, // share mode
NULL, // security
OPEN_EXISTING, // fails if does not exist
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // attributes
NULL // template file
);

//------------------------------------------------------
//------------------------------------------------------

}

When the application started and when it tries to call first CreateFile is getting succeded but the second time CreateFile fails with GetLastError code C0000005.
Please help and provide me your thoughts on this.

Thanks & Regards,
Shivaprakash

xxxxx@gmail.com wrote:

I am writing a non pnp driver to handle IOCTL requests from my application.
I have a below callback function registered in the driver to handle CreateFile routine from application when does request.

When the application started and when it tries to call first CreateFile is getting succeded but the second time CreateFile fails with GetLastError code C0000005.
Please help and provide me your thoughts on this.

Usually, this means you set Exclusive=TRUE when you called
IoCreateDevice. Possible?

There is no need for an application to create separate handles for
synchronous and non-synchronous I/O. It is absolutely trivial to write
a “ReadFileSync” that uses the asynchronous handle but behaves exactly
like a non-overlapped ReadFile.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I would expect that to cause something like a sharing violation (c0000043) rather than an access violation (c0000005).

The C0000005 status almost always means a bad pointer dereference in the user space that was handled (bad references in the kernel space are typically fatal). If this can be reproduced reliably you can set a breakpoint on the CreateFile call that is going to fail, then enable 1st chance breaks on access violations by typing “sxe av” into debugger. Then hit ‘g’ and you should stop at the next access violation exception that occurs. Enter “sxr” to reset exception handling settings back to defaults.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Tim Roberts
Sent: Friday, September 13, 2013 9:37 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] CreateFile failed with Error code C0000005

xxxxx@gmail.com wrote:

I am writing a non pnp driver to handle IOCTL requests from my application.
I have a below callback function registered in the driver to handle CreateFile routine from application when does request.

When the application started and when it tries to call first CreateFile is getting succeded but the second time CreateFile fails with GetLastError code C0000005.
Please help and provide me your thoughts on this.

Usually, this means you set Exclusive=TRUE when you called IoCreateDevice. Possible?

Better to create or open a file in a separate Thread at DISPATCH_LEVEL.
issue a call from usermode and send it to your driver’s dispatch control.

You can’t make Zw file creation or io calls at dispatch_level

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Friday, September 13, 2013 2:20 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] CreateFile failed with Error code C0000005

Better to create or open a file in a separate Thread at DISPATCH_LEVEL.
issue a call from usermode and send it to your driver’s dispatch control.


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

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

Hi All,

Thanks so much for your expert suggestions!!

“Usually, this means you set Exclusive=TRUE when you called
IoCreateDevice. Possible?”

The problem was with my WdfDeviceInitSetExclusive(DeviceInit, TRUE); in NonPnpDeviceAdd routine.
So, I changed it now,l to WdfDeviceInitSetExclusive(DeviceInit, FALSE); Now it resolves my issue.

Thanks all…

I suspect the actual GetLastError was not C0000005, but simply 5, which is ERROR_ACCESS_DENIED. This error is returned for both security access denied and sharing violation.