monir
asked on
Protecting Text Files in Windows 2000 Server using Mutex for CGI and Normal application
Hi,
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(ni l, False, 'MUTEX_LOCK_DATA_TEXT_FILE ');
if (FLockMutex=0) then exit;
try
if (WaitForSingleObject(FLock Mutex, 90000)<>WAIT_OBJECT_0)
then exit; // Time out
// now the file is protected
// open it for read or write
// then close the file
finally
ReleaseMutex(FLockMutex);
CloseHandle(FLockMutex);
end;
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.
Monir.
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(ni
if (FLockMutex=0) then exit;
try
if (WaitForSingleObject(FLock
then exit; // Time out
// now the file is protected
// open it for read or write
// then close the file
finally
ReleaseMutex(FLockMutex);
CloseHandle(FLockMutex);
end;
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.
Monir.
why not open the file exclusive?
ASKER
I gave a file as an example only, I need to protect a portion of program code, which deals with too many files and database.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Mr. Russell
I used the name that you suggested 'Global\MUTEX_LOCK_DATA_TE XT_FILE'. When a user tries to connect to CGI using IE, he get "You are not authorized to view this page
You do not have permission to view this directory or page using the credentials you supplied......".
Buy inside the server IE prompts a dialog asking for user name/Passord, when I give it a valid information it wokrs fine, I mean CGI works with the small program and they are waiting each others. same effects if I used 'Local\MUTEX_LOCK_DATA_TEX T_FILE'
So how can solve this for my users?
Monir
I used the name that you suggested 'Global\MUTEX_LOCK_DATA_TE
You do not have permission to view this directory or page using the credentials you supplied......".
Buy inside the server IE prompts a dialog asking for user name/Passord, when I give it a valid information it wokrs fine, I mean CGI works with the small program and they are waiting each others. same effects if I used 'Local\MUTEX_LOCK_DATA_TEX
So how can solve this for my users?
Monir
ASKER
ok, 'Global\MUTEX_LOCK_DATA_TE XT_FILE' is good, so to solve the users issue, I wrote a client program, I used TIdHTTP (Indy10) to connect to CGI so I can now give it the user name and password.
When I used 'MUTEX_LOCK_DATA_TEXT_FILE ' inside CGI, I successfully connected to CGI and passed the info using parameter string and get back the results.
But when I used 'Global\MUTEX_LOCK_DATA_TE XT_FILE' inside CGI, I got exception, so my question now is how to use pass the user name & password to TIdHTTP?
I notes that the event OnSelectAuthorization is called but (I did not write a handler for it), then an excetion is raised (EAccessViolation).
Thank you for your help
When I used 'MUTEX_LOCK_DATA_TEXT_FILE
But when I used 'Global\MUTEX_LOCK_DATA_TE
I notes that the event OnSelectAuthorization is called but (I did not write a handler for it), then an excetion is raised (EAccessViolation).
Thank you for your help
Is the call to CreateMutex actually raising the exception? (not likely), or is it the fact that the program logic has now changed, and thus the code is raising an exception further in?
Russell
ASKER
IdHTTP is raising the exception.
Sorry, wish I could help you with that one, but I was only attempting to assist with the Mutex problem.
Russell
ASKER
Mr. Russell, I must thank you for your help and give you points for your help, I am trying to get the latest version of Indy maybe this will solve the exception, or maybe I can play with the IIS security settings on server for my CGI prgram, or what if I switch user at the begining of CGI program to a user with admin rights instead of I_USER?
Thank you for your help.
Mnoir
Thank you for your help.
Mnoir