Print Page | Close Window

Saving To an INI

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: General Discussion
Forum Description: Topics Related to Visual C++ MFC Development in General
URL: http://forum.codejock.com/forum_posts.asp?TID=307
Printed Date: 09 May 2024 at 7:22pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Saving To an INI
Posted By: JamesC
Subject: Saving To an INI
Date Posted: 18 December 2003 at 11:18am
Does anyone know how to save to the INI file. SaveCommandBars Description says that lpszProfileName 'Points to a null-terminated string that specifies the name of a section in the initialization file or a key in the Windows registry where state information is stored.' I would like to know what is the initialization file and how do you specify it. My app has different users and each user will have a different layout. I need to be able to specify different INI file to save and load from. But I dont see how to specify the INI file to get the data from. Documentation states to hand it the Section name of the INI file.



Replies:
Posted By: JamesC
Date Posted: 06 January 2004 at 4:23pm
Can ANYONE help me with this issue. It is rather important for our app to be able to save to multiple INI files. It appears that it will only save to the WIN.INI file. Is there any other way of saving the data.


Posted By: Oleg
Date Posted: 02 February 2004 at 12:06am

These functions decode/encode state to string. You can save this sting to INI or registry for each user.  see: WritePrivateProfileString("CommandBars", "State", str, strPath);

 

void LoadStateFromString(CXTPCommandBars* pCommandBars, CString str, BOOL bSerializeControls)
{
 int nLen = str.GetLength();
 if ((nLen == 0) || (nLen % 2 != 0) )
  return; 
 
 int nBytes = nLen/2;
 BYTE* pData = new BYTE[nBytes];

 for (int i = 0; i < nLen; i += 2)
 {
  (pData)[i/2] = (BYTE)
   (((str[i+1] - 'A') << 4) + (str - 'A'));
 }

 CMemFile memFile(pData, nBytes);
 CArchive ar (&memFile,CArchive::load);
 
 try
 { 
  pCommandBars->SerializeBarState(ar, bSerializeControls);
 }
 catch (COleException* pEx)
 {
  pEx->Delete ();
 }
 catch (CArchiveException* pEx)
 {
  pEx->Delete ();
 }

 delete[] pData;

}

CString SaveStateToString(CXTPCommandBars* pCommandBars, BOOL bSerializeControls)
{
 CMemFile memFile;
 CArchive ar (&memFile,CArchive::store);

 try
 {
  pCommandBars->SerializeBarState(ar, bSerializeControls);
 }
 catch (COleException* pEx)
 {
  pEx->Delete ();
 }
 catch (CArchiveException* pEx)
 {
  pEx->Delete ();
 }
 
 ar.Flush();

 DWORD nBytes = (DWORD)memFile.GetPosition();
 BYTE* pData = memFile.Detach();

 CString str;
 LPTSTR lpsz = str.GetBufferSetLength(nBytes * 2);
 
 // convert to string and write out
 for (UINT i = 0; i < nBytes; i++)
 {
  lpsz[i*2] = (TCHAR)((pData & 0x0F) + 'A'); //low nibble
  lpsz[i*2+1] = (TCHAR)(((pData >> 4) & 0x0F) + 'A'); //high nibble
 }
 
 ar.Close();
 memFile.Close();
 free(pData); 

 return str;
}



-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: timhards
Date Posted: 19 August 2004 at 6:52am

The LoadStateFromString and SaveStateToString functions that Oleg has given are incorrect (and don't compile). To correct them make the following changes.

In LoadStateFromString, change:
  (pData)[i/2] = (BYTE)
   (((str[i+1] - 'A') << 4) + (str - 'A'));
to:
  (pData)[i/2] = (BYTE)
   (((str[i+1] - 'A') << 4) + (str[i] - 'A'));

In SaveStateToString, change:
  lpsz[i*2] = (TCHAR)((pData & 0x0F) + 'A'); //low nibble
  lpsz[i*2+1] = (TCHAR)(((pData >> 4) & 0x0F) + 'A'); //high nibble
to:
  lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble
  lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble

Tim




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net