|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionThis article will explain how to add a user name to the Events that are logged in to the Event Viewer. BackgroundI needed to add user names to events that were being logged, and I could not find anything directly on target. Microsoft's website stated to simply add the SID to the Using the codeI wrote a standalone program first to test out what I wanted to do at work. I will provide all the relevant portions here so that you can simply paste into your project something that works. HANDLE hToken;
HANDLE g_eventHandle = NULL;
int rc;
DWORD dwLength = 0;
PTOKEN_USER pTokenUser = NULL;
TCHAR *params[1];
// in order to use ReportEvent we must first Register Event
g_eventHandle = RegisterEventSource(NULL, _T("SID_TEST"));
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
// Get required buffer size and allocate the PTOKEN_USER buffer.
if (!GetTokenInformation(
hToken, // handle to the access token
TokenUser, // get information about the token's groups
(LPVOID) pTokenUser, // pointer to TOKEN_USER buffer
0, // size of buffer
&dwLength // receives required buffer size
))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
goto Cleanup;
pTokenUser = (PTOKEN_USER)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, dwLength);
if (pTokenUser == NULL)
goto Cleanup;
}
// Get the token group information from the access token.
if (!GetTokenInformation(
hToken, // handle to the access token
TokenUser, // get information about the token's groups
(LPVOID) pTokenUser, // pointer to TOKEN_USER buffer
dwLength, // size of buffer
&dwLength // receives required buffer size
))
{
goto Cleanup;
}
params[0] = const_cast<TCHAR*>("test string");
// the actual call that places the event into the Event Viewer
rc = ReportEvent(g_eventHandle, EVENTLOG_INFORMATION_TYPE, 0, 0,
pTokenUser->User.Sid,// the sid goes here <-------
1, 0, (LPCTSTR *)params, NULL);
Cleanup:
// Free the buffer for the token .
if (pTokenUser != NULL)
HeapFree(GetProcessHeap(), 0, (LPVOID)pTokenUser);
// i am finished with the Event
DeregisterEventSource(g_eventHandle);
Points of InterestThat's all there is to it. The The Event View with our entry:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||