The Windows Registry 9

 

 

 

 

 

Creating the Registry Subkey and Value Program Example

 

In the following program example, we try to open the registry key, create subkeys and assign values to the subkeys.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

// WARNING!!!

// If you don't know what you are doing, please don't try

// this code...and don't forget to delete the key or use

// RegDeleteKey()...

 

// #define _WIN32_WINNT 0x0502   // Windows Server 2003 family

// Other Win OS, please change accordingly...

#define _WIN32_WINNT 0x0501         // For Win Xp

// #define _WIN32_WINNT 0x0500   // Windows 2000

// #define _WIN32_WINNT 0x0400   // Windows NT 4.0

// #define _WIN32_WINDOWS 0x0500 // Windows ME

// #define _WIN32_WINDOWS 0x0410 // Windows 98

// #define _WIN32_WINDOWS 0x0400 // Windows 95

 

#include <windows.h>

#include <stdio.h>

#include <aclapi.h>

 

BOOL AddMyEventSource(

 

   LPTSTR pszLogName, // Application log or a custom log

   LPTSTR pszSrcName, // event source name

   LPTSTR pszMsgDLL,  // path for message DLL

   DWORD  dwNum)      // number of categories

{

   HKEY hk;

   DWORD dwData;

   TCHAR szBuf[MAX_PATH];

 

   // Create the event source as a subkey of the log.

   wsprintf(szBuf, L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s", pszLogName, pszSrcName);

 

   //********************************************

   // Create the registry key

   if(RegCreateKey(HKEY_LOCAL_MACHINE, szBuf, &hk) != ERROR_SUCCESS)

   {

      wprintf(L"RegCreateKey() - Could not create the registry key.");

      return FALSE;

   }

   else

   {

         wprintf(L"Well, RegSetValueEx() is OK!\n");

         wprintf(L"Key is HKEY_LOCAL_MACHINE\n");

         wprintf(L"Subkeys is: \n");

         wprintf(L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s\n  was created successfully.\n", pszLogName, pszSrcName);

   }

  

   //********************************************

   // Set the name of the message file

    if(RegSetValueEx(hk,                   // subkey handle

           L"EventMessageFile",             // value name

           0,                               // must be zero

           REG_EXPAND_SZ,             // value type

           (LPBYTE) pszMsgDLL,        // pointer to value data

           (DWORD) lstrlen(szBuf)+1)  // length of value data

               != ERROR_SUCCESS)

   {

      wprintf(L"RegSetValueEx() - Could not set the event message file.");

      return FALSE;

   }

      else

      wprintf(L"RegSetValueEx() - The event message file has been set successfully!\n");

 

   // Set the supported event types.

   dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;

 

 

   //********************************************

   if(RegSetValueEx(hk,       // subkey handle

           L"TypesSupported", // value name

           0,                 // must be zero

           REG_DWORD,         // value type

           (LPBYTE) &dwData,  // pointer to value data

           sizeof(DWORD))     // length of value data

               != ERROR_SUCCESS)

 

   {

      wprintf(L"RegSetValueEx() - Could not set the supported types.");

      return FALSE;

   }

   else

      wprintf(L"RegSetValueEx() - The supported types have been set successfully.\n");

 

   //********************************************************

   // Set the category message file and number of categories.

   if(RegSetValueEx(hk,               // subkey handle

           L"CategoryMessageFile",    // value name

           0,                         // must be zero

           REG_EXPAND_SZ,             // value type

           (LPBYTE) pszMsgDLL,        // pointer to value data here we set same as "EventMessageFile"

           (DWORD) lstrlen(szBuf)+1) // length of value data

               != ERROR_SUCCESS)

   {

      wprintf(L"RegSetValueEx() - Could not set the category message file.");

      return FALSE;

   }

   else

       wprintf(L"RegSetValueEx() - The category message file has been set successfully.\n");

 

   //********************************************

   if(RegSetValueEx(hk,       // subkey handle

           L"CategoryCount"// value name

           0,                 // must be zero

           REG_DWORD,         // value type

           (LPBYTE) &dwNum,   // pointer to value data

           sizeof(DWORD))     // length of value data

               == ERROR_SUCCESS)

              

               wprintf(L"RegSetValueEx() - The category count has been set successfully.\n");

   else

   {

      wprintf(L"RegSetValueEx() - Could not set the category count.");

      return FALSE;

   }

 

   // Close the key

   if(RegCloseKey(hk) == ERROR_SUCCESS)

         wprintf(L"hk key was closed successfully!\n");

   else

         wprintf(L"Failed to close hk key!\n");

 

   return TRUE;

}

 

int wmain(int argc, WCHAR *argv[])

{

      // Application log or a custom log. Here we put a custom log just for learning!

      LPTSTR pszLogName = L"MyCustLogTest";

 

   // The event source name

   LPTSTR pszSrcName = L"MyEventSrcName";

 

   // The path for message dll, this dll or other executable file must exist lol!

   // here, mytestdll.dll just a dummy. You will know it when you restart

   // your computer if the created key does not deleted...:o)

   LPTSTR pszMsgDLL = L"%SystemRoot%\\System32\\mytestdll.dll";

 

   // number of categories.

   DWORD  dwNum = 0x00000003;

 

   BOOL test = AddMyEventSource(

                   pszLogName, // Application log or a custom log.  Custom log here...

                   pszSrcName, // event source name.

                   pszMsgDLL,  // path for message DLL.

                   dwNum           // number of categories.

                   );

 

   // Just to check the return value...

   // 0 - failed, non-zero should be  fine

   wprintf(L"The AddMyEventSource() return value is: %u\n", test);

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

Creating the Registry Subkey and Value Program Example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Do a verification using Registry Editor. Please delete the created registry key and subkeys.

 

Creating the Registry Subkey and Value Program Example: Verifying the created registry keys and subkeys

 

 

 

 

 

< Windows Registry 8 | Windows Registry Index | Win32 Programming | Windows Registry 10 >