How to Read from and Write to Registry in WIN32
Suppose you want your application to be able to save the position and size of its main window when ending and restore it in startup. First, let me show how to write its position to the registry:
( Both for reading from and writing to the registry, you must include <winreg.h>)
Inside the WM_CLOSE message handler, you could put:
HKEY hParentKey; /* handle to the parent key */
RECT rect = {0, 0, 0, 0};
SECURITY_ATTRIBUTES sa = {sizeof(sa), 0,1}; /*since it will be ignored in some OSs, lets set it to the default setting */
DWORD state = 0; /* this will hold an integer that will say if a new key was created or an existing key was opened */
GetWindowRect(hwnd, &rect); /* get the position of the window */
RegCreateKeyEx ( /* creates a new key, or opens an existing key */
HKEY_CURRENT_USER, /* handle to the base key; could also be a handle to an opened key */
“Software\\MyCompany\\MyApp”, /* name of the subkey to open or create */
0, /* reserved; should be set to 0 */
“”, /* name of the class or object type of the key. You can also specify an empty string */
KEY_ALL_ACCESS, /* level of access desired; this allows any access*/
0, /* if 0, the key will be saved to the registry; if 1, the key will disappear when window shut down */
&sa, /* security attributes; will be ignored in win 9x */
&hParentKey, /* will receive the handle to the key */
&state /* will receive 1 if the key was created, or 2 if it was opened */
);
RegSetValueEx( /* save the values of left, top, right and bottom */
hParentKey, /* handle to the key under which lies the subkey */
“Left”, /* name of the subkey */
0, /* reserved - must be 0 */
REG_DWORD, /* we are reading a 32-bit integer */
(CONST BYTE*) &rect.left, /* address of whatever we want to write */
sizeof
(rect.left) /* size of whatever was passed in above argument */
);
/* lets do the same with the other 3 values */
RegSetValueEx(hParentKey, “Top”, 0, REG_DWORD, (CONST BYTE*) &rect.top, sizeof(rect.top) );
RegSetValueEx(hParentKey, “Bottom”, 0, REG_DWORD, (CONST BYTE*) &rect.bottom, sizeof(rect.bottom) );
RegSetValueEx(hParentKey, “Right”, 0, REG_DWORD, (CONST BYTE*) &rect.right, sizeof(rect.right) );
/* now lets close the handle to the parent key */
RegCloseKey(hParentKey);
Good. Now we have learnt how to save the values. But, how to read them and move the window appropriately? That’s a way of doing that:
Inside the WM_CREATE message handler, put:
HKEY hParentKey;
RECT rect = {0, 0, 0, 0};
SECURITY_ATTRIBUTES sa = {sizeof(sa), 0, 1};
DWORD size = 0, state = 0, type = 0;
RegCreateKeyEx ( /* creates a new key, or opens an existing key */
HKEY_CURRENT_USER, /* handle to the base key; could also be a handle to an opened key */
“Software\\MyCompany\\MyApp”, /* name of the subkey to open or create */
0, /* reserved should be set to 0 */
“”, /* name of the class or object type of the key. You can also specify an empty string */
KEY_ALL_ACCESS, /* level of access desired; this allows any access*/
0, /* if 0, the key will be saved to the registry; if 1, the key will disappear when window shut down */
&sa, /* security attributes; will be ignored in win 9x */
&hParentKey, /* will receive the handle to the key */
&state /* will receive 1 if the key was created, or 2 if it was opened */
);
if
( state == 2) /* opened existing key */
{
size = sizeof(rect.left);
RegQueryValueEx( /* read the value from registry */
hParentKey, /* handle to tha parent key */
“Left”, /* name of the value to read */
0, /* reserved - must be 0 */
&type, /* this will receive the type of the value read; should receive REG_DWORD /*
(BYTE*) &rect.left, /* this will receive the data read */
&size /* set this to the size of what was passed above; will also receive the size of what was actually read */
);
/* lets do the same with the other 3 values */
size = sizeof(rect.top);
RegQueryValueEx(hParentKey, “Top”, 0, &type, (BYTE*) &rect.top, &size);
size = sizeof(rect.bottom);
RegQueryValueEx(hParentKey, “Bottom”, 0, &type, (BYTE*) &rect.bottom, &size );
size = sizeof(rect.right);
RegQueryValueEx(hParentKey, “Right”, 0, &type, (BYTE*) &rect.right, &size)
}
else
/* created a new one */
{
rect.top = rect.left = 0; /* default position */
rect.right = rect.bottom = 200; /* default height and width */
/* save them to the registry */
RegSetValueEx(hParentKey, “Left”, 0, REG_DWORD, (CONST BYTE*) &rect.left, sizeof(rect.left) );
RegSetValueEx(hParentKey, “Top”, 0, REG_DWORD, (CONST BYTE*) &rect.top, sizeof(rect.top) );
RegSetValueEx(hParentKey, “Bottom”, 0, REG_DWORD, (CONST BYTE*) &rect.bottom, sizeof(rect.bottom) );
RegSetValueEx(hParentKey, “Right”, 0, REG_DWORD, (CONST BYTE*) &rect.right, sizeof(rect.right) );
}
/* now that we’ve read the values, move the window appropriately */
SetWindowPos( /* move the window and set its size */
hwnd, /* window to move */
0, /* position in Z-order to insert the window */
rect.left, /* left coordinate of the upper left corner*/
rect.top, /* top coordinate of the upper left corner*/
rect.right, /*left coordinate of the lower right corner*/
rect.bottom, /* top coordinate of the lower right corner*/
SWP_NOACTIVATE | SWP_NOZORDER /* do not activate the window unless it was already active; do not change its position in Z-order. */
);
That’s it. Hope I could help you.