ClearBox Server™ v1.2 Developer's Guide

Step 3. Reading from INI File

In this step we'll create a class that encapsulates INI files that we use for storing server extension settings.

1. Create a new class. Select Project -> Add Class... in the main menu, and select Generic C++ Class from the available templates. Then click Open. Name the class CINI and click Finish.

Change class constructor to take HINSTANCE argument. In the Class View window expand CINI class and double-click CINI(void) node. You see class constructor definition. Change to the following:

CINI::CINI(HINSTANCE hInst)
{
	TCHAR fName[MAX_PATH];
	GetModuleFileName(hInst,fName,MAX_PATH);
	m_FilePath=fName;
	int pos=m_FilePath.ReverseFind(_T('\\'));
	m_FilePath=m_FilePath.Left(pos+1)+_T("settings.ini");
}
		

Then double-click CINI class root node in Class View, and in the class declaration change default constructor from

CINI(void);		
		

to

CINI(HINSTANCE hInst);	  
	

Then add private class field to the class. Right-click CINI class in Class View, select Add -> Add Variable... in the context menu. In the opened wizard dialog:

  • Set Access to 'private'.
  • Set Variable type to CString.
  • Set Variable name to m_FilePath.

So we've declared a variable that is initialized in the class constructor with the path to the settings file. It is located in the same folder with extension dll-module and is named settings.ini.

2. Let's add methods that read the data from an INI file. Right-click CINI class in Class View and in the context menu select Add -> Add Function... In the wizard window enter the following:

  • Return type - bool
  • Function name - GetString
  • Access - public

In the Parameter type box type CString, and in Parameter name type section. Click Add. Then type name in the Parameter name box and click Add again. Set Parameter type to CString&, Parameter name to retVal. Click Finish.

3. Repeat the second step, but set Function name to GetNumber, and type of retVal parameter to int& instead of CString&.

4. Double-click GetNumber(CString section, CString name) in the Class View window and replace return false; function body with the code:

	TCHAR sBuf[300];
	TCHAR* defS=_T("4770AC81-EB49-426a-B501-B2F7B3FAB96B");
	GetPrivateProfileString(section,name,
		defS,sBuf,300,m_FilePath);
	retVal=sBuf;
	
	return _tcscmp(defS,sBuf)!=0;

In this piece of code we use GUID to make sure that a string is read from ini-file. If sBuf is equal to defS, it means that no string was found in the specified section and with the name specified, and GetPrivateProfileString has copied defS to sBuf.

5. Double-click GetNumber(CString section, CString name, DWORD& retVal) in Class View and replace return false; function body with the following code:

	retVal=GetPrivateProfileInt(section,name,-1,m_FilePath);
	return retVal!=-1;
		

This method and the previous one return true or false depending on whether the requested key was found.

Go to the next step.


© 2001-2003 XPerience Technologies. www.xperiencetech.com

Created by chm2web html help conversion utility.