Click here to Skip to main content
15,884,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, Sorry for the basic question. I need to create a file for write access in kernel mode and and allow other thread to read it, this can happen simultaneously. so This what I do when creating the file.

C++
    status = ZwCreateFile(&hfile,
                        GENERIC_WRITE,
                        &oa,
                        &iostatus,
                        NULL,
                        FILE_ATTRIBUTE_NORMAL,
                        FILE_SHARE_READ,
                        FILE_OVERWRITE_IF,
                        FILE_SYNCHRONOUS_IO_NONALERT,
                        NULL,
                        0);

}


works well, the file is created for write and later when I try to create another handle to read it I always get "STATUS_SHARING_VIOLATION"
here is how I do it.

C++
ntstatus = ZwCreateFile(&OriFileHandle,
							FILE_READ_DATA,
							&objOriAttr,
							&ioOriStatusBlock,
							NULL,
							FILE_ATTRIBUTE_NORMAL,
							FILE_SHARE_READ,
							FILE_OPEN,
							FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE,
							NULL,
							0);

Can anybody tell me what is it that I am doing wrong?

What I have tried:

I already read about ZwCreatefile and how to share a file for access by other thread but I am still getting that error.
Posted
Updated 4-Apr-16 7:33am
v2
Comments
Wshwilfried 22-Mar-16 23:58pm    
In my second call to ZwCreateFile I set the desired access to SYNCHRONIZE and it worked. Can anyone explain why? I thought since My goal is to simultaneously read the file I had to provide FILE_READ_DATA or GENERIC_READ.

1 solution

It looks like you are mixing access flags with other types of flags. Also, you omit FILE_SHARE_WRITE in the second ZwCreateFile.

Try this:

C++
ntstatus = ZwCreateFile(&OriFileHandle,
							GENERIC_READ,
							&objOriAttr,
							&ioOriStatusBlock,
							NULL,
							FILE_ATTRIBUTE_NORMAL,
							FILE_SHARE_WRITE,
							FILE_OPEN,
							FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, // I've never used these.
							NULL,
							0);
 
Share this answer
 
Comments
Wshwilfried 8-Apr-16 0:58am    
Thank you very much it solved my problem, programming drivers is really an art.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900