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, LSYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s, pszLogName, pszSrcName);
//********************************************
// Create the registry key
if(RegCreateKey(HKEY_LOCAL_MACHINE, szBuf, &hk) != ERROR_SUCCESS)
{
wprintf(LRegCreateKey() - Could not create the registry key.);
return FALSE;
}
else
{
wprintf(LWell, RegSetValueEx() is OK!\n);
wprintf(LKey is HKEY_LOCAL_MACHINE\n);
wprintf(LSubkeys is: \n);
wprintf(LSYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s\n was created successfully.\n, pszLogName, pszSrcName);
}
//********************************************
// Set the name of the message file
if(RegSetValueEx(hk, // subkey handle
LEventMessageFile, // 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(LRegSetValueEx() - Could not set the event message file.);
return FALSE;
}
else
wprintf(LRegSetValueEx() - 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
LTypesSupported, // 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(LRegSetValueEx() - Could not set the supported types.);
return FALSE;
}
else
wprintf(LRegSetValueEx() - The supported types have been set successfully.\n);
//********************************************************
// Set the category message file and number of categories.
if(RegSetValueEx(hk, // subkey handle
LCategoryMessageFile, // 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(LRegSetValueEx() - Could not set the category message file.);
return FALSE;
}
else
wprintf(LRegSetValueEx() - The category message file has been set successfully.\n);
//********************************************
if(RegSetValueEx(hk, // subkey handle
LCategoryCount, // 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(LRegSetValueEx() - The category count has been set successfully.\n);
else
{
wprintf(LRegSetValueEx() - Could not set the category count.);
return FALSE;
}
// Close the key
if(RegCloseKey(hk) == ERROR_SUCCESS)
wprintf(Lhk key was closed successfully!\n);
else
wprintf(LFailed 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 = LMyCustLogTest;
// The event source name
LPTSTR pszSrcName = LMyEventSrcName;
// 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(LThe AddMyEventSource() return value is: %u\n, test);
return 0;
}
Build and run the project. The following screenshot is a sample output.
Do a verification using Registry Editor. Please delete the created registry key and subkeys.