CreateFile and locking files

Started by
3 comments, last by generic_name 16 years, 10 months ago
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);   

Advertisement
Have you looked into LockFile?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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?
In your call example you mixed the two

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

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.

This topic is closed to new replies.

Advertisement