How to lock a file in windows?

Started by
9 comments, last by iMalc 16 years, 3 months ago
Hey Guys, I"m developing using VS 2003 on c/c++ and I need to lock a file so I can use it as a mutex between threads and processes. I tried this code example from MSDN ( http://msdn2.microsoft.com/en-us/library/8w5bsb4f(VS.80).aspx ) but it cant compile this code, I get the following errors: : error C3861: 'fopen_s': identifier not found, even with argument-dependent lookup : error C3861: '_lock_file': identifier not found, even with argument-dependent lookup : error C3861: 'sprintf_s': identifier not found, even with argument-dependent lookup : '_unlock_file': identifier not found, even with argument-dependent lookup If you got any other method to lock a file under windows please advice. Thanks!
;)
Advertisement
Are you including cstdio?

#include <cstdio>
No, I use the exact example from MSDN
and I include:

#include <stdio.h>
#include <stdlib.h>

;)
Unfortunately, that sample is for MSVC 2005, and uses functions added to MSVC in 2005, so aren't available in 2003. In any case, if you want a mutex, then just use CreateMutex().
Quote:Original post by spree
Hey Guys,
I"m developing using VS 2003 on c/c++ and I need to lock a file so I can use it as a mutex between threads and processes.

I tried this code example from MSDN
( http://msdn2.microsoft.com/en-us/library/8w5bsb4f(VS.80).aspx )


You are using VS2003, but you are trying to compile an example written for VS2005 (I marked the parts of your post, that gives me this information). I'm not sure about the _lock_file and _unlock_file functions (I never used them), but I'm 100% sure that fopen_s and sprintf_s (and all other functions ending with _s) were added in VS2005. You can either try to install the latest version of Windows SDK (it comes with the latest version of the compiler and standard library), or you can get the newer version of VS (either 2005 or 2008 will do the trick).
Quote:Original post by SiCrane
Unfortunately, that sample is for MSVC 2005, and uses functions added to MSVC in 2005, so aren't available in 2003. In any case, if you want a mutex, then just use CreateMutex().


:/

I must use a file lock in my case, I need an inter process/thread mutex. Is there any other reference on this?
win32API?

;)
If you want a mutex, use a mutex (CreateMutex as shown above, it works between processes). If you want to lock a file your best bet is CreateFile with LockFile. Files aren't a good way of doing IPC under windows though.
Why must you lock the file? Simply using MapViewOfFile and friends is usually sufficient for your given purposes.

If the lock is necessary to prevent corruption from multiple instances of your process or threads then you have a design flaw (and should probably be using a semaphore). If the lock is intended to prevent the user from accidentally screwing with things then there's very little point - protecting people from their own stupidity on the whole is a vain pursuit - a dedicated idiot will manage to own themself under any design. If the lock is designed to prevent deliberate access from the user then this won't be sufficient: use some suitably light form of encryption.

I can't think of any other reason you could want to do this.
Ring3 Circus - Diary of a programmer, journal of a hacker.
You don't need a lock for accidental access, simply open the file without any sharing enabled.
I found what i needed using _locking method, both for threads and processes :)
;)

This topic is closed to new replies.

Advertisement