Solved

Configuration File?

Posted on 2002-05-11
3
195 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now