Solved

Configuration File?

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
object modeling - load and save functions 5 61
Dynamically allocate memory 9 58
Microsoft C++ code failing in executable that worked 9 98
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 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