|
-
Oct 10th, 2000, 03:28 PM
#1
How do i load and save settings, like where the window was last placed, to an .ini file?
-
Oct 10th, 2000, 04:59 PM
#2
Use WritePrivateProfileString to write to the INI file and use GetPrivateProfileString to read from the INI file.
-
Oct 10th, 2000, 11:04 PM
#3
I cant seem to get it to work. Could you post some example code using WritePrivateProfileStruct and GetPrivateProfileString. Thanks.
-
Oct 11th, 2000, 07:25 AM
#4
Frenzied Member
[code]
WritePrivateProfileString("dimensions","x","102","c:\\myinf.inf");
WritePrivateProfileString("dimensions","y","200","c:\\myinf.inf");
[code]
This is how your ini will look.
Code:
[dimensions]
x=102
y=200
Then you can load these values using GetPrivateProfileString
[Edited by Vlatko on 10-11-2000 at 08:28 AM]
-
Oct 11th, 2000, 11:40 AM
#5
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.
-
Oct 11th, 2000, 12:42 PM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 11th, 2000, 04:04 PM
#7
-
Oct 11th, 2000, 04:11 PM
#8
Frenzied Member
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);
-
Oct 12th, 2000, 12:10 PM
#9
Monday Morning Lunatic
--- Registry.cpp ---
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;
}
--- Registry.h ---
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__
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 12th, 2000, 12:22 PM
#10
Like i said earlier, i dont use MFC.
-
Oct 12th, 2000, 12:26 PM
#11
Frenzied Member
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);
-
Oct 12th, 2000, 12:32 PM
#12
-
Oct 12th, 2000, 12:43 PM
#13
Monday Morning Lunatic
<miffed>
It's not MFC!!!!!
Just because it's in a class doesn't automatically mean I used MFC. Just slap it into your program.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|