MFC registry reading and writing

The registry is an internal database of Windows, which is a huge tree-like hierarchical database. It records the correlation between the software installed on the computer by the user and each program. It contains the computer’s hardware configuration information, including automatically configured plug-and-play devices and various existing devices.

The following example is the write and read processing of the registry:

Project—Create—Single Document—Finish

Add Menu —option;

Add a submenu in option:

RegWrite and RegRead and add submenu event handlers.

Share picture

Write registry event program:

 1 void< span style="color: #000000;"> CRegTrackingView::OnRegoptionRegwrite()

2 {
3 // TODO: Add your command handler code here
4 UINT i;
5 HKEY m_hKey;
6 i = RegCreateKey(HKEY_LOCAL_MACHINE, _T(" SOFTWARE\A21VC"), &m_hKey);
7 if (i == 0)
8 {
9 RegSetValue(m_hKey, TEXT("RegDemo"), REG_SZ, TEXT( "123"), strlen("123"));
10 MessageBox(_T("OK"));
11 }
12 else
13 {
14 MessageBox(_T("Failed."));
15 }
16 RegCloseKey(m_hKey);
17 }

Read the registry program:

1 void CRegTrackingView::OnRegoptionRegread()

2 {
3 // TODO: Add your command handler code here
4 LONG lRead;
5 RegQueryValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\A21VC\RegDemo"), NULL, &lRead);
6 char* pStr = new char[lRead];
7 RegQueryValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\A21VC\RegDemo"), pStr, &lRead);
8 MessageBox(pStr);
9 }

End.

The above example is normal in actual operation, but the sub-key parameter information can not be found in the registry. I don’t know if it is a system reason. This is for research…

Thank you.

 1 void CRegTrackingView::OnRegoptionRegwrite()

2 {
3 // TODO: Add your command handler code here
4 UINT i;
5 HKEY m_hKey;
6 i = RegCreateKey(HKEY_LOCAL_MACHINE, _T(" SOFTWARE\A21VC"), &m_hKey);
7 if (i == 0)
8 {
9 RegSetValue(m_hKey, TEXT("RegDemo"), REG_SZ, TEXT( "123"), strlen("123"));
10 MessageBox(_T("OK"));
11 }
12 else
13 {
14 MessageBox(_T("Failed."));
15 }
16 RegCloseKey(m_hKey);
17 }

1 void CRegTrackingView::OnRegoptionRegread()

2 {
3 // TODO: Add your command handler code here
4 LONG lRead;
5 RegQueryValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\A21VC\RegDemo"), NULL, &lRead);
6 char* pStr = new char[lRead];
7 RegQueryValue(HKEY_LOCAL_MACHINE, _T("SOFTWARE\A21VC\RegDemo"), pStr, &lRead);
8 MessageBox(pStr);
9 }

Leave a Comment

Your email address will not be published.