Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading and writing to the registry...

Status
Not open for further replies.

BungoMan

Programmer
May 13, 2002
12
US
essentialy i am trying to store a program setting in the registry that is a bool, 1 if certain things have been done before and 0 if they have not. ive read up some on readign and writing to the registry and it makes sense, but the little i have read did not go into enough detail for me to understand what it was i was doing so i have no clue how to write a simple bool value, i have read the MS thing on editing the registry which is right here:

Creating or Opening a Registry Key
Before you can read or write to a key, you must first obtain a handle to it. To do this, use either the RegOpenRegKeyEx or RegCreateKeyEx Microsoft Win32® functions. In practice, you will almost always use RegCreateKeyEx, which will open the key if it exists or create it if it does not. Here is a typical scenario:

Declare an HKEY variable in your code.
Call RegCreateKeyEx, passing HKEY_LOCAL_MACHINE as the parent key and Software/YourCompany/YourApplication as the subkey.
For each setting you wish to read, call RegQueryValueEx, and if the setting is not found set a default value.
Close the handle to the key with RegCloseKey.
Sample code:


void CMyFirstMFCApplicationApp::LoadPreferences()
{
HKEY hkey;
DWORD dwDisposition;

DWORD dwType, dwSize;

// Set the default values
m_dwMaxFileSize = 16 * 1024; // 16k
_tcscpy(m_szLastFileName, TEXT("Datafile.TXT"));

if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\My Company\\My
Application"), 0, NULL, 0, 0, NULL, &hkey, &dwDisposition)==
ERROR_SUCCESS)
{
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
RegQueryValueEx(hkey, TEXT("MaxFileSize"), NULL, &dwType,
(PBYTE)&m_dwMaxFileSize, &dwSize);

dwType = REG_SZ;
dwSize = sizeof(m_szLastFileName);
RegQueryValueEx(hkey, TEXT("LastFileName"), NULL, &dwType,
(PBYTE)&m_szLastFileName, &dwSize);

RegCloseKey(hkey);
}
}


void CMyFirstMFCApplicationApp::SavePreferences()
{
HKEY hkey;
DWORD dwDisposition;

DWORD dwType, dwSize;

if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\My Company\\My
Application"), 0, NULL, 0, 0, NULL, &hkey, &dwDisposition)==
ERROR_SUCCESS)
{
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
RegSetValueEx(hkey, TEXT("MaxFileSize"), 0, dwType,
(PBYTE)&m_dwMaxFileSize, dwSize);

dwType = REG_SZ;
dwSize = (_tcslen(m_szLastFileName) + 1) * sizeof(TCHAR);
RegSetValueEx(hkey, TEXT("LastFileName"), 0, dwType,
(PBYTE)&m_szLastFileName, dwSize);

RegCloseKey(hkey);
}
}

that right there i think is supposed to write a string of text (id compile it now, but im at school and i dont think theyd want me editing the registry on these computers...). basicly i just need to know what all that stuff means. how i would go about reading a bool value from a specific key in the registry and returning it to a variable in the program and if it is 1 then skip some stuff, if it is 0 then do the stuff and when the stuff is done write a bool value to the registry to be read next time the program runs. im not totaly sure if this really matters... but im trying to do all this in a console program, not a win32 one or anything. i think all i need to include is windows.h, or is there something else i am forgetting? and if not can this be done in a console program. id do it in a win32 program but i dont know enough about that to make it do much of anything... (still learning right now).
 
Divide and Conquer !

Yes, you can read / write the registry from a console program - no problem !
You have found the right documentation.

It may look like a lot of information - and you may need to read even more to understand the stuff, but divide into smaller tasks, and I am pretty sure you will conquer !

Good luck !
/JOlesen
 
A tip I'll give you is to read/write the data to the registry as a REG_BINARY data type.

Then you can write it for the sizeof(BOOL), storing the boolean value in the registry exactly as it's stored in the variable.

When you read it back out, just cast the value back to a BOOL type, bVal = (BOOL)RegQueryVal..., which will put it right back into the boolean. Then check its return codes to know if it really read the data from the registry or not (for using a default if not set yet).

Also, there's a way to get the size of the entry from the registry before you read it. This is a very good programmign idea, because somebody might go in and change the value in the registry from your nice little 1 to a string saying "HaHaHaHah..." which will overwrite all your stack space if you just read that directory into a boolean (since it's larger than the size of your boolean).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top