beyond GetUserName[Ex]: currently authenticated user - Programmer

This is a discussion on beyond GetUserName[Ex]: currently authenticated user - Programmer ; Hi, I need to retrieve the name and domain of the currently authenticated user (using VC++6.0). The problem is, I cannot use GetUserName or GetUserNameEx, since I have to find out the user information from within a DLL, that is ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: beyond GetUserName[Ex]: currently authenticated user

  1. beyond GetUserName[Ex]: currently authenticated user

    Hi,

    I need to retrieve the name and domain of the currently authenticated
    user (using VC++6.0).

    The problem is, I cannot use GetUserName or GetUserNameEx, since I have
    to find out the user information from within a DLL, that is loaded by a
    service. The domain returned by GetUserNameEx is okay, but the user I
    get back is something like SYSTEM and such.

    Changing the logon for the service that loads the DLL is not an option.

    Any suggestions are more than welcome.

    Thanks!!


  2. Re: beyond GetUserName[Ex]: currently authenticated user

    Hi,

    You can use the following API to retrieve information about
    a connection to a shared resource:

    NetUseGetInfo() using USE_INFO_2

    http://msdn.microsoft.com/library/de...usegetinfo.asp

    Hope these suggestions helps,

    Kellie.


  3. Re: beyond GetUserName[Ex]: currently authenticated user

    Thanks, Kellie, but that's not what I need. For that function, I
    already need the user name, I believe.

    If anyone is interested, here is the solution (which I got from a smart
    guy on http://www.experts-exchange.com/)

    1. Get PID of "explorer.exe" process. It apparently always runs under
    the currently authenticated user, not system account or anything like
    that - for details, check out
    http://support.microsoft.com/support.../Q175/0/30.ASP. The
    algorithm found there can be greatly simplified to get pid of a
    specified process.

    2. Get handle to the process above (use OpenProcess to get a HANDLE to
    the process PID, and then use OpenProcessToken with TOKEN_QUERY).

    3. Call GetTokenInformation for the above HANDLE and fetch SID of the
    user from PTOKEN_USER structure (one of out parameters)

    4. Do LookupAccountSid on the SID, which will give you the domain name
    and (more importantly) user name of the currently authenticated user.

    Ta-da!


  4. Re: beyond GetUserName[Ex]: currently authenticated user


    In article <1143740192.772835.264120@j33g2000cwa.googlegroups. com>, "sc" writes:
    >
    > If anyone is interested, here is the solution (which I got from a smart
    > guy on http://www.experts-exchange.com/)
    >
    > 1. Get PID of "explorer.exe" process. It apparently always runs under
    > the currently authenticated user, not system account or anything like
    > that - for details, check out
    > http://support.microsoft.com/support.../Q175/0/30.ASP. The
    > algorithm found there can be greatly simplified to get pid of a
    > specified process.


    And what if there are multiple explorer.exe processes, running under
    different accounts?

    There may be multiple interactive users logged into different
    WinStations, using Terminal Services or the like. There may be
    interactive processes run under different accounts, using runas or
    similar. (I often have multiple explorer.exe processes running
    under different accounts on my Windows boxes.)

    For that matter, what if no users are currently logged in?

    You can't reliably find the name of "the" logged-in user, because
    there may not be a single logged-in user. It's possible that doesn't
    matter for your application, but I'd be nervous about this design.


    --
    Michael Wojcik michael.wojcik@microfocus.com

    I'm not particularly funny, but I wanted to do something outrageous.
    And I'm Norwegian, so I wasn't going to go too far. -- Darlyne Erickson

  5. Re: beyond GetUserName[Ex]: currently authenticated user

    Hi Michael,

    Very valid points, that I haven't considered in my rush for a solution
    that isn't. I suppose there is no spoon, after all.

    Thanks for the reply (but not for sinking my leaky ship !
    Sandra


+ Reply to Thread