Creating registry key/subkey, their names and the respective data value

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Creating registry key/subkey, their names and data values

To show: Various registry C functions for Windows programming

 

 

// 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

// For Win Xp, change accordingly...

#define _WIN32_WINNT 0x0501

// #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))

{

printf("Could not create the registry key.");

return FALSE;

}

else

printf("SYSTEM\\CurrentControlSet\\Services\\EventLog\\%S\\%S 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

{

printf("Could not set the event message file.");

return FALSE;

}

else

printf("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

{

printf("Could not set the supported types.");

return FALSE;

}

else

printf("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

{

printf("Could not set the category message file.");

return FALSE;

}

else

printf("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

{

printf("Could not set the category count.");

return FALSE;

}

else

printf("The category count has been set successfully.\n");

// Close the key

RegCloseKey(hk);

return TRUE;

}

 

int main(int argc, wchar_t *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;

 

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...

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

return 0;

}

 

Output example:

 

SYSTEM\CurrentControlSet\Services\EventLog\MyCustLogTest\MyEventSrcName created successfully.

The event message file has been set successfully.

The supported types have been set successfully.

The category message file has been set successfully.

The category count has been set successfully.

The AddMyEventSource() return value is: 1

Press any key to continue . . .

 

 

Run registry editor (regedt/regedt32). Go to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog. You will find the MyCustLogTest and MyEventSrcName subkeys

 

 

C and C++ Programming Resources | C & C++ Code Example Index