Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > General Discussion
  New Posts New Posts RSS Feed - Saving To an INI
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Saving To an INI

 Post Reply Post Reply
Author
Message
JamesC View Drop Down
Newbie
Newbie


Joined: 03 December 2003
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote JamesC Quote  Post ReplyReply Direct Link To This Post Topic: Saving To an INI
    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.
Back to Top
JamesC View Drop Down
Newbie
Newbie


Joined: 03 December 2003
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote JamesC Quote  Post ReplyReply Direct Link To This Post 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.

Edited by JamesC
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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;
}



Edited by oleg
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
timhards View Drop Down
Newbie
Newbie


Joined: 19 August 2004
Status: Offline
Points: 1
Post Options Post Options   Thanks (0) Thanks(0)   Quote timhards Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.145 seconds.