Click here to Skip to main content
Licence 
First Posted 22 Feb 2006
Views 24,807
Bookmarked 17 times

Adding User Name to Events

By maththaios | 22 Feb 2006
How to add user names to the Event Viewer.

1

2
1 vote, 20.0%
3
1 vote, 20.0%
4
3 votes, 60.0%
5
4.40/5 - 5 votes
μ 4.40, σa 1.57 [?]

The Event Viewer

Introduction

This article will explain how to add a user name to the Events that are logged in to the Event Viewer.

Background

I 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 ReportEvent function. It did not tell how to get the SID. After much more investigation, I found something written in another programming language that got the user SID, so I translated it into C and combined it with what I was doing.

Using the code

I 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 Interest

That's all there is to it. The GetTokenInformation function has to be called twice; if you have too much or too little allocated for your SID, the function will fail.

The Event View with our entry:

The Event Viewer

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

maththaios

Software Developer (Senior)

United States United States

Member
I am a computer programmer in Florida.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUser is strange code rather than username PinmemberPatje0:04 22 Feb '11  
GeneralNice example but do you have vb.net version. PinmemberMember 491154817:31 25 Mar '08  
QuestionWhat about using LookupAccountName ? Pinmemberlumoryel5:22 12 Apr '06  
AnswerRe: What about using LookupAccountName ? Pinmembermaththaios6:26 12 Apr '06  
GeneralNice but i have a question Pinmembernesculcas1:23 6 Mar '06  
GeneralRe: Nice but i have a question Pinmembermaththaios10:04 6 Mar '06  
if I understand your question, so seem to be having a problem with the message itself, and not in reguards to adding the user name to the message. I did a search on The Code Project for the ReportEvent function, and this one, http://www.codeproject.com/system/xeventlog.asp, seems to be on target for the problems you are having. I hope this helps.
Matt

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 22 Feb 2006
Article Copyright 2006 by maththaios
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid