Solved

Configuration File?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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