Protecting Text Files in Windows 2000 Server using Mutex for CGI and Normal application

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

why not open the file exclusive?
monirAuthor Commented:

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.
Russell LibbySoftware Engineer, Advisory Commented:
I have a feeling it has to do with the global/local session namespaces... from the MSDN:

A Terminal Services environment has a global name space for events, semaphores, mutexes, waitable timers, file-mapping objects, and job objects. In addition, each Terminal Services client session has its own separate name space for these objects. Terminal Services client processes can use object names with a "Global\" or "Local\" prefix to explicitly create an object in the global or session name space.

Windows 2000 or later: If Terminal Services is not running, the "Global\" and "Local\" prefixes are ignored. The remainder of the name can contain any character except the backslash character.


So, you might try the following mutex name to see if it corrects the issue:





Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

monirAuthor Commented:
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?

monirAuthor Commented:
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

Russell LibbySoftware Engineer, Advisory Commented:

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?

monirAuthor Commented:
IdHTTP is raising the exception.
Russell LibbySoftware Engineer, Advisory Commented:

Sorry, wish I could help you with that one, but I was only attempting to assist with the Mutex problem.

monirAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.