Sign in to follow this  
spree

How to lock a file in windows?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
CreateMutex already works between processes.
You would only need to go so far as holding a file on disk in use if you wanted an mutex between multiple PCs, which is incredibly unlikely.

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