Link to home
Start Free TrialLog in
Avatar of monir
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(nil, False, 'MUTEX_LOCK_DATA_TEXT_FILE');
if (FLockMutex=0)  then exit;
try
  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

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.
Avatar of kretzschmar
kretzschmar
Flag of Germany image

why not open the file exclusive?
Avatar of monir
monir

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
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of monir

ASKER

Thank you Mr. Russell
I used the name that you suggested 'Global\MUTEX_LOCK_DATA_TEXT_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_TEXT_FILE'

So how can solve this for my users?

Monir
Avatar of monir

ASKER

ok, 'Global\MUTEX_LOCK_DATA_TEXT_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_TEXT_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


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
Avatar of monir

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
Avatar of monir

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