Solved

Lock an ini file

Posted on 2002-03-06
12
1,286 Views
Last Modified: 2008-03-10
I want to lock an INI file, so that only my program can write to it.
How can I do this, and still be able to use the GetPrivateProfileString API functions?
0
Comment
Question by:MakeItWork614
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 100 total points
ID: 6846611
You can't. There isn't any way to lock a particular file so only a particular program can use it--the best you can do is make the file read-only, then use code to make the file writable before performing any updates to it.

If you're using an OS with the NTFS file system you can set permissions on a file so only certain USERS can access it, but even then someone with administrative rights on the machine could come in and change the file's owner.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6846763
You could encrypt the file.  Then before reading it, you could decrypt it, access its data, then encrypt it again.

Of course the user could still delete it or make meaningless (random) changes to it, but maybe just preventing the user from making 'intelligent' changes might accomplish your goal.

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955284
Dear MakeItWork614

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "pjknibbs"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:MakeItWork614
ID: 6968561
griessh,
I object.
I feel that just because pjknibbs doesn't know of a way to do this, doesn't mean there isn't a method capable of doing it.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6970317
MakeItWork614

Thanks for your feedback. Maybe DanRollins (he also commented on this question) steps in here and tells us his opinion.
I have to fully agree with pjknibbs, there is no way to lock a file for a certain application. pjknibbs didn't say he doesn't know of a way, he said that there isn't any way to do it! Quite a difference.
BTW: as I said already, if the experts don't get any feedback, they won't come back with more comments ...

======
Werner
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6970534
There is one idea...
if your program is a service, or otherwise is always running, you could open the file, setting the sharing mode to
   CFile::shareDenyRead | CFile::shareDenyWrite
That would prevent any other programs from opening the file -- as long as your program is running.  As soon as your program quites, the O/S is likely to release all file locks that it held.

I know that is not a reasonable answer, but i mention it anyway because:

   There is no reasonable way to do what you want to do.

-- Dan
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6973505
Please update and finalize this.
Moondancer - EE Moderator
0
 

Author Comment

by:MakeItWork614
ID: 6973734
Moondancer,
My question is not ready to be finalized.
When I have worked through a satisfactory answer, I will finalize this question.

DanRollins,
I will give your method a try, and get back to you.

griessh,
>>he said that there isn't any way to do it!
Just because some saids that it can't be done, doesn't mean that it can't be done.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6973756
Thanks, makes total sense to me; sorry to rush you.  I just wanted to make sure you were aware of updates, since my Email notifs were delayed.
:) Moondancer - EE Moderator
0
 
LVL 11

Expert Comment

by:griessh
ID: 6974025
MakeItWork614

I don't know how many people you need to tell you: It can't be done!

I'm glad that Dan's suggestion gives you at least a hint how to do what you want to accomplish, but that changes the question a bit ...

======
Werner
0
 

Author Comment

by:MakeItWork614
ID: 6976878
Moondancer,
Could you please split the points between pjknibbs and DanRollins?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6977807
MakeItWork614 ->  Absolutely, happy to do this.  Thank you, points have been split.

Points for DanRollins -> http://www.experts-exchange.com/jsp/qShow.jsp?qid=20294982

Moondancer - EE Moderator
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 125
Dialogbox API leak? 18 94
Unable to start eclipse ? 17 141
Problem with SqlConnection 4 168
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question