FileInUse Problem!

My Application uses FdMemTable and load a file called users.xml. every thing is nice until I the application is used by local network.
Several users try to open the same file at the same time! An exception rises!
I found a function that might help me but "createFile" is  not recognized also GENERIC_READ or GENERIC_WRITE  by the compiler. Please Help!  

function TDataContainer.IsFileInUse(fName : string) : boolean;
var //fNAME is the adress of the file to check if is in use
HFileRes : Thandle;
 Result := false;
 if not FileExists(fName) then exit;
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
bish wakimAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Geert GOracle dbaCommented:
well yeah.
a file is like a narrow door, only 1 person can pass through it at a time

if you want multiple users to work on the same data at the same time.
Use a database, that's what those things were invented for

In essence, a database reads/writes from/to a set of files and coördinates all user actions to these files

a check for multiple users ...
place 2 copies of your code next to each other (in your mind), and run them line by line simultaneously.

first time:
first line:
result False;
second line:
> does the file exist? : answer no. >> Exit
Nothing gets past this ... if the file doesn't exist
third line, if the file exists, open it for writing ... multiple times

that third line doesn't work multiple times for multiple processes.
as i said a database coördinates this into 1 process.
bish wakimAuthor Commented:
Thank you for your response
however I imagined a way each time a call for this file is done to make a check if it is in use. if it is in use then wait until this file is freed.
Please look at the example(I gave in my first post) I could find for this  issue. it seems that it works but I don't understand why Delphi 10 seatle 10 vcl does not recognize creatfile function and some other flags.
Please I agree with you that DataBase is the ideal solution, but as a matter of fact I wish succeed with this with 10 users using my application    through local home network... they edit and update a single  file ...
Please Help
Geert GOracle dbaCommented:
have you ever considered writing in multiple places in the same file ?

there is no insert mode in a file ... unless you use a record structure (xml is not a record structure)
there is only overwrite mode in a file

an unpacked record structure reserves spaces for each entry
sample: first name: 50 char, last name 50 char
in the file, you would see x chars filled in the first name place and 50-x blancs
and so on for the last name

xml does not have this structure, every character you see on the file is physically next to the previous character
so if you were change a typo like "Cliton" to "Clinton",
your app would have to read the whole XML file,
and write everything into a new XML file
and then rename the new file into the old.

i know a few companies who have up to now failed at implementing a multi-user app even with an oracle database as the backend
they had vast locking problems from 6+ users, and this is a big company !

you obviously started designing your app with just you as the user: in other words: 1 user
and then found out it doesn't work in a multi-user environment

the first thing to look at
1: consistency
how to ensure the data remains consistent with multiple users
> indeed you need a locking mechanism, but it needs a little more depth than just a boolean value
2: storage
with multiple users, you need a shared storage space for the data
> if the user with the database on his pc switches it off ... complaints will follow rather swiftly

10 users might not seem much, but it is more than 1... and you only need 2 users for a deadlock

XML is not a database, it's a file with a specific structure and rules to pass between apps
any app following the rules can read the file and extract the data correctly

there are loads of free databases out there
look on google for the express versions

Delphi has the tools built in ... FireDac to connect with all these databases.

if you really want to stick with files ...
can go the n-tier way.

Every user connects to a intermediate layer.  This layer reads/writes to the file.
And all requests are typically processed sequentially

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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

bish wakimAuthor Commented:
Please I have my reasons to go on with my choice. Let me limite my question, the solution Iam looking for is presented by the code bellow which I found some where and it is supposed to work. My only problem for the moment is with the function "creatfile". I still dont understand why My compiler (delphi 10 seatled) does not accepte it , maybe  should I add some unit to make this work!?

Function FileInUse(fName:string):boolean;
var   HFileRes : Thandle;
 Result := false;
 if not FileExists(fName) then exit;
 Result := (HFileRes = INVALID_HANDLE_VALUE) ;
Geert GOracle dbaCommented:
If you havent implemented an insertedit correctly ... your app will never be for multiple users
bish wakimAuthor Commented:
Yes I think you are right. I should use DataBase...
Thank you!
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.