Sign in to follow this  
generic_name

CreateFile and locking files

Recommended Posts

I'm trying to use CreateFile (Win32 fuction) to open a file and cause it to be locked from writing by other processes but have them still be able to open and read it. It works fine in XP, but in 98 nothing seems to be able to read or write to it. Just wondering if its something to do with the Win98 operating system. This is the code I'm using to open the file:
file = CreateFile("test.txt", 
                  GENERIC_WRITE,          // open for reading
                  FILE_SHARE_READ,       // share for reading
		  NULL,                  // default security
		  OPEN_EXISTING,         // existing file only
		  FILE_ATTRIBUTE_NORMAL, // normal file
		  NULL);   

Share this post


Link to post
Share on other sites
I was playing around with it some more and changed GENERIC_WRITE to GENERIC_READ and it seems to be working. Anybody have any kind of clue as to why?

Share this post


Link to post
Share on other sites
In your call example you mixed the two

GENERIC_WRITE, // open for reading <- nope, open for writing
FILE_SHARE_READ, // share for reading

Share this post


Link to post
Share on other sites
I had it as GENERIC_READ before but I was doing something that made it not work and changed it to _WRITE and forgot to put it back.

With fopen() I'm fairly sure you could open a file in write mode and still be able to have other processes open the same file for reading/writting. I was wondering if there was something funny going on with CreateFile() that I didn't know about or couldn't find on MSDN.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this