Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Configuration File?

Posted on 2002-05-11
3
Medium Priority
?
224 Views
Last Modified: 2010-04-02
I have multiple files (dlls) in a single directory. They all need to set a variable with a value that should be user editable. I was thinking of using a configuration file and doing somthing like this:

first.dll 1
other.dll 8
library.dll 4

Then have each dll look for the line beginning with it's filename and read the value after the name into an int variable.

Does this seem like the way to go? Can someone offer some code that would give an example of how to do this (Win32/MFC)?
Thank you very much!
0
Comment
Question by:cocoabutter46
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 400 total points
ID: 7004004
A very simple solution would be to create and use an INI file.

The relevant Win32 API calls are:
   WritePrivateProfileString()
   GetPrivateProfileString()
   GetPrivateProfileInt()

For instance,

BOOL WritePrivateProfileString(
  "DllConfig",     // LPCTSTR lpAppName,  // section name
  "first.dll",  // LPCTSTR lpKeyName,  // key name
  "1",          //LPCTSTR lpString,   // string to add
  "c:\\myprog\\myinfo.INI" \\LPCTSTR lpFileName  // initialization file
);

BOOL WritePrivateProfileString(
  "DllConfig", "other.dll", "8", "c:\\myprog\\myinfo.INI" ;
BOOL WritePrivateProfileString(
  "DllConfig", "library", "4", "c:\\myprog\\myinfo.INI";

If you then use a text editor to open c:\myprog\myinfo.INI you will see somthing like:

[DllConfig]
first.dll=1
other.dll=8
library.dll=4

=-=-=-=-=-=-=-
That makes it easy to tweak with a text editor, etc.

The DLLs can read thier config settings using:

int n= GetPrivateProfileInt("DllConfig","first.dll", 0,"c:\\myprog\\myinfo.INI" );

You may want to make a section for each DLL:

[DLLs]
Dll_1=first.dll
Dll_2=other.dll]
Dll_3=library.dll

[first.dll]
ConfigVal=1
DataRate=1000
UserName=Thamas Q. Jones

[other.dll]
ConfigVal=8
DataRate=7000

[library.dll]
ConfigVal=4
DataRate=8000


-- Dan
0
 

Author Comment

by:cocoabutter46
ID: 7004045
that's perfect. Just curious, is there a way to manually insert a comment into an ini file [like with notepad]?
somthing like: // or /* or #
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7004197
Well, anything that is not below the specified
[section]
mark is ignored.  So lines at the top of the file will be ignored.  Also, if you start a line with, say a semocolon, then it won't match any 'key' value that you send in.  Also if you use GetPrivateProfileInt(), I think that only the first part of the line will have any effect, thus:

[section]
;this line is invalid so ignored
thisline=77
thisOther=99 ;non-numeric text will be ignored by GetPrivateProfileInt()

-- Dan
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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