Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does anyone have any idea why after locking a file I can still read and write it using a hex editor??? My code is below. LockFileEx doesn't fail. I have not written the Unlock code yet (so it is impossible to have been unlocked accidentally). The file seems to be still open when I read/write it. I try to lock the entire file.
I just wonder if someone can find any silly mistake I'm making (if any) and save me from a lot of search time. Thanks in advance.

LARGE_INTEGER LI;
memset(&LI, 0, sizeof(LI));
BOOL ret = ::GetFileSizeEx(hFile, &LI);
if(ret==0) goto ER;

OVERLAPPED ov;
memset(&ov, 0, sizeof(ov));
ov.hEvent = NULL;
ov.Offset = 0;
ov.OffsetHigh = 0;
ov.Pointer = NULL;
ov.Internal = 0;
ov.InternalHigh = 0;
ret = ::LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, LI.LowPart, LI.HighPart, &ov);
if(ret==0) goto ER;
Posted
Comments
Richard MacCutchan 11-Sep-11 12:23pm    
What does your program do after you locked it? Is it still holding the open HANDLE when you access it with your hex editor?
KEL3 15-Sep-11 14:09pm    
Yes it does.
David was right. I've found out what was going on now...
Thanks for your help too. :)

Hi,

If the user account has SeBackupPrivilege/SE_BACKUP_NAME and the hex editor is enabling the access token... then it is possible that your hex editor is opening the file with the FILE_FLAG_BACKUP_SEMANTICS flag set.

Or as Richard is implying... if you are closing the file handle... then the OS could be releasing the lock.

Best Wishes,
-David Delaune
 
Share this answer
 
Indeed. It seems it is because of the SeBackupPrivilege/SE_BACKUP_NAME. I've tried a different hex editor and it wouldn't read it. (it was a huge file and only a hex editor could read it without loading it onto the memory).
Thanks! :)
 
Share this answer
 

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