How do i load and save settings, like where the window was last placed, to an .ini file?
Printable View
How do i load and save settings, like where the window was last placed, to an .ini file?
Use WritePrivateProfileString to write to the INI file and use GetPrivateProfileString to read from the INI file.
I cant seem to get it to work. Could you post some example code using WritePrivateProfileStruct and GetPrivateProfileString. Thanks.
[code]
WritePrivateProfileString("dimensions","x","102","c:\\myinf.inf");
WritePrivateProfileString("dimensions","y","200","c:\\myinf.inf");
[code]
This is how your ini will look.
Then you can load these values using GetPrivateProfileStringCode:[dimensions]
x=102
y=200
[Edited by Vlatko on 10-11-2000 at 08:28 AM]
The problem with that is that im using numbers, so i couldnt save them like that, and if i loaded them like that, they couldnt be used because they would be loaded into a string.
Use atoi and itoa.
Why do you need an INI file, anyway? Saving into the Registry is a much better and safer way of storing settings.
Well, how do i do that?
This will wrire to the reg.
Code:HKEY hRegistryKey;
char *sBuffer;
sBuffer = "500";
RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Size\\Example", 0, NULL, 0, KEY_WRITE, NULL, &hRegistryKey, NULL);
RegSetValueEx(hRegistryKey, NULL, 0, REG_SZ, (LPBYTE)sBuffer, strlen(sBuffer)+1);
--- Registry.cpp ---
--- Registry.h ---Code:#include "Registry.h"
Registry::Registry() {
}
Registry::~Registry() {
}
unsigned long Registry::ReadLong(HKEY hKey, string sPath, string sValue) {
unsigned long lData = 0;
HKEY hMyKey;
unsigned long lType = 0;
unsigned char pcData[1025];
unsigned long lSize = 1024;
// Open registry key
if(RegOpenKeyEx(hKey, sPath.c_str(), 0, KEY_ALL_ACCESS, &hMyKey) != ERROR_SUCCESS)
return 0;
// Set the value
if(RegQueryValueEx(hMyKey, sValue.c_str(), 0, &lType, pcData, &lSize) != ERROR_SUCCESS)
return 0;
if(lType != REG_DWORD)
return 0;
// Close the key again
RegCloseKey(hMyKey);
// First problem...what is in pcData?
lData = pcData[0] + (pcData[1] << 8) + (pcData[2] << 16) + (pcData[3] << 24);
return lData;
}
bool Registry::SaveLong(HKEY hKey, string sPath, string sValue, long lData) {
HKEY hMyKey;
unsigned long lOpenType;
unsigned char pcTemp[4];
// Open or create registry key
if(RegCreateKeyEx(hKey, sPath.c_str(), 0, "NTRegKey32", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMyKey, &lOpenType) != ERROR_SUCCESS)
return false;
// Set the value
// Endian-ness problems abound...
pcTemp[0] = lData && 0xFF;
pcTemp[1] = (lData >> 8) && 0xFF;
pcTemp[2] = (lData >> 16) && 0xFF;
pcTemp[3] = (lData >> 24) && 0xFF;
if(RegSetValueEx(hMyKey, sValue.c_str(), 0, REG_DWORD, pcTemp, 4) != ERROR_SUCCESS)
return false;
// Close the key again
if(RegCloseKey(hMyKey) != ERROR_SUCCESS)
return false;
return true;
}
string Registry::ReadString(HKEY hKey, string sPath, string sValue) {
string sData;
HKEY hMyKey;
unsigned long lType = 0;
unsigned char pcData[1025];
unsigned long lSize = 1024;
// Open registry key
if(RegOpenKeyEx(hKey, sPath.c_str(), 0, KEY_ALL_ACCESS, &hMyKey) != ERROR_SUCCESS)
return string("");
// Set the value
if(RegQueryValueEx(hMyKey, sValue.c_str(), 0, &lType, pcData, &lSize) != ERROR_SUCCESS)
return string("");
if(lType != REG_SZ)
return string("");
// Close the key again
RegCloseKey(hMyKey);
sData = (char*)pcData;
return sData;
}
bool Registry::SaveString(HKEY hKey, string sPath, string sValue, string sData) {
HKEY hMyKey;
unsigned long lOpenType;
// Open or create registry key
if(RegCreateKeyEx(hKey, sPath.c_str(), 0, "NTRegKey32", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMyKey, &lOpenType) != ERROR_SUCCESS)
return false;
// Set the value
if(RegSetValueEx(hMyKey, sValue.c_str(), 0, REG_SZ, (const unsigned char*)sData.c_str(), sData.length()+1) != ERROR_SUCCESS)
return false;
// Close the key again
if(RegCloseKey(hMyKey) != ERROR_SUCCESS)
return false;
return true;
}
Code:#ifndef __REGISTRY_H__
#define __REGISTRY_H__
#include <windows.h>
#include <string>
using namespace std;
class Registry {
public:
Registry();
virtual ~Registry();
bool SaveString(HKEY hKey, string sPath, string sValue, string sData);
string ReadString(HKEY hKey, string sPath, string sValue);
bool SaveLong(HKEY hKey, string sPath, string sValue, long lData);
unsigned long ReadLong(HKEY hKey, string sPath, string sValue);
};
#endif // __REGISTRY_H__
Like i said earlier, i dont use MFC.
Well this is not MFC!
Code:HKEY hRegistryKey;
char *sBuffer;
sBuffer = "500";
RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Size\\Example", 0, NULL, 0, KEY_WRITE, NULL, &hRegistryKey, NULL);
RegSetValueEx(hRegistryKey, NULL, 0, REG_SZ, (LPBYTE)sBuffer, strlen(sBuffer)+1);
I meant parksie.
It's not MFC!!!!!
Just because it's in a class doesn't automatically mean I used MFC. Just slap it into your program.