Protecting Text Files in Windows 2000 Server using Mutex for CGI and Normal application
Posted on 2006-04-25
I have a CGI web service runing on Windows 2000 server, users are using IE to read data from and modify data in a text file, to protect the text file, I uesd a Mutex which worked perfect as follow:
FLockMutex:=CreateMutex(nil, False, 'MUTEX_LOCK_DATA_TEXT_FILE');
if (FLockMutex=0) then exit;
if (WaitForSingleObject(FLockMutex, 90000)<>WAIT_OBJECT_0)
then exit; // Time out
// now the file is protected
// open it for read or write
// then close the file
Now if more than one users tries to use this web service, the Mutex will organize them to access the file one by one.
I wrote a small Delphi program which read and modify the same Text file, so I used the same code above to protect the file, same Mutex name.
I run this small program on the same server using (Remote Desktop connection) but I found by experiment that Mutex techniques is not working between this small program and CGI.
In other words, this small program can access the text file even if the CGI is locking it !!! And if this small program is loacking the text file the CGI will be able access it also!!.
If I run the CGI in local machine (XP pro) the behavour is different, createMutex gives handle 0 (FLockMutex is zero) which is good, so I now that someone is using the text file then I will try again later.
But I need to run the file in Windows 2000 server please help me in finding the solution, I used Semaphore instead of Mutex which gives exactly same behavior.
Thank you for the help.