Results 1 to 13 of 13

Thread: Saving settings

  1. #1
    Guest

    Post

    How do i load and save settings, like where the window was last placed, to an .ini file?

  2. #2
    Guest
    Use WritePrivateProfileString to write to the INI file and use GetPrivateProfileString to read from the INI file.

  3. #3
    Guest
    I cant seem to get it to work. Could you post some example code using WritePrivateProfileStruct and GetPrivateProfileString. Thanks.

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    [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]
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    Guest
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Guest
    Well, how do i do that?

  8. #8
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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);
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    --- 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

  10. #10
    Guest
    Like i said earlier, i dont use MFC.

  11. #11
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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 am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  12. #12
    Guest
    I meant parksie.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Unhappy <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
  •  



Click Here to Expand Forum to Full Width