Solved

Configuration File?

Posted on 2002-05-11
3
183 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
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now