article

How to Read from and Write to Registry using WIN32 API

Email
Submitted on: 1/3/2015 5:05:00 AM
By: LCensoni (from psc cd)  
Level: Beginner
User Rating: By 14 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 4059
 
     Some time ago, i was reading an article about registry here in PSC. It was all done in MFC, so I asked the author to please write one using WIN32 API. He never did, so I went and found the info myself. Here it is. Please leave comments (maybe even votes), they let me know if the article was good and make me write new ones. Thank you.

This article has accompanying files
 
				





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.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.