CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old August 23rd, 2006, 09:49 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    how to get the current user id?

    Hi,

    my application needs to get the user id of the current user. I'm searching since 30 minutes but i just can't find a function name - there's GetUserName or GetCurrentProcessId or GetCurrentThreadId, but there's no GetCurrentUserId!

    Can anybody help me? Thanks a lot

    Chris

    PS: i use C and the SDK, not C++. usually that doesn't make a difference, but please don't suggest C++ classes
    Reply With Quote
      #2    
    Old August 23rd, 2006, 10:26 AM
    ovidiucucu's Avatar
    ovidiucucu ovidiucucu is offline
    Moderator/Reviewer
    Power Poster
     
    Join Date: Feb 2003
    Location: Iasi - Romania
    Posts: 6,518
    ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)ovidiucucu has a reputation beyond repute (3000+)
    Re: how to get the current user id?

    Is GetUserNameEx what you are searching for?
    __________________
    Ovidiu Cucu
    Reply With Quote
      #3    
    Old August 24th, 2006, 03:31 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    Re: how to get the current user id?

    I don't think it is. I found that function, but according to the parameters it's not really able to give me a user-id, just a "NameUniqueId", which is a GUID. I'm pretty sure that there's a numerical value, but i don't know how to get it.
    Reply With Quote
      #4    
    Old August 24th, 2006, 05:09 AM
    pengch pengch is offline
    Member
     
    Join Date: Aug 2003
    Posts: 181
    pengch is on a distinguished road (10+)
    Re: how to get the current user id?

    What is user id ? SID??
    __________________
    Best Api Monitor tool.
    Trace the target program automatically and monitor the parameters of all API and COM interfaces.

    Auto Debug for Windows 4.0
    Auto Debug for .Net
    http://www.autodebug.com/
    Reply With Quote
      #5    
    Old August 24th, 2006, 05:17 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    Re: how to get the current user id?

    good question.

    i'm a unix programmer. so it's clear what a user ID is for me. But i'm not sure if microsoft or windows have the same idea...

    My problem is this: i have to port unix software to windows. The user-id (numeric, 4 bytes) is used for identifying the user. So i need a comparable id for windows.

    And i know that somehow this must be possible, since cygwin has an "id" command, just on unix, and it prints

    uid=40197(Christoph) gid=10545(mkgroup-l-d) groups=0(root),544(Administrators),5
    45(Users),10545(mkgroup-l-d)

    So either they completely fake it or they found a way to get a user id on windows...
    Reply With Quote
      #6    
    Old August 24th, 2006, 05:23 AM
    pengch pengch is offline
    Member
     
    Join Date: Aug 2003
    Posts: 181
    pengch is on a distinguished road (10+)
    Re: how to get the current user id?

    If you want to get the SID, please use these API:

    LookupAccountName to get the SID
    ConvertSidToStringSid to convert sid to string.
    __________________
    Best Api Monitor tool.
    Trace the target program automatically and monitor the parameters of all API and COM interfaces.

    Auto Debug for Windows 4.0
    Auto Debug for .Net
    http://www.autodebug.com/
    Reply With Quote
      #7    
    Old August 24th, 2006, 05:31 AM
    pengch pengch is offline
    Member
     
    Join Date: Aug 2003
    Posts: 181
    pengch is on a distinguished road (10+)
    Re: how to get the current user id?

    from MSDN

    SID Components
    A SID value includes components that provide information about the SID structure and components that uniquely identify a trustee. A SID consists of the following components:

    The revision level of the SID structure
    A 48-bit identifier authority value that identifies the authority, such as a Windows NT server domain, that issued the SID.
    A variable number of subauthority or relative identifier (RID) values that uniquely identify the trustee relative to the authority that issued the SID.
    The combination of the identifier authority value and the subauthority values ensures that no two SIDs will be the same, even if two different SID-issuing authorities issue the same combination of RID values. Each SID-issuing authority issues a given RID only once.

    SIDs are stored in binary format in a SID structure. To display a SID, you can call the ConvertSidToStringSid function to convert a binary SID to string format. To convert a SID string back to a valid, functional SID, call the ConvertStringSidToSid function.

    These functions use the following standardized string notation for SIDs, which makes it simpler to visualize their components:

    S-R-I-S-S...

    In this notation, the literal character S identifies the series of digits as a SID, R is the revision level, I is the identifier-authority value, and S... is one or more subauthority values.

    The following example uses this notation to display the well-known domain-relative SID of the local Administrators group:

    S-1–5-32-544

    In this example, the SID has the following components. The constants in parentheses are well-known identifier authority and RID values defined in WINNT.H.

    A revision level of 1
    An identifier-authority value of 5 (SECURITY_NT_AUTHORITY)
    A first subauthority value of 32 (SECURITY_BUILTIN_DOMAIN_RID)
    A second subauthority value of 544 (DOMAIN_ALIAS_RID_ADMINS)
    __________________
    Best Api Monitor tool.
    Trace the target program automatically and monitor the parameters of all API and COM interfaces.

    Auto Debug for Windows 4.0
    Auto Debug for .Net
    http://www.autodebug.com/
    Reply With Quote
      #8    
    Old August 24th, 2006, 05:45 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    Re: how to get the current user id?

    i looked in the sourcecode of cygwin, and it was a bit hard to track it down, but it seems that they completely emulate the user ID stuff. it was not helpful for me.

    LookupAccountName looks like a pain in the ***, compared to getuid(), but thanks nevertheless. i'll have a look.
    Reply With Quote
      #9    
    Old August 24th, 2006, 07:34 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    Re: how to get the current user id? (solved)

    here is the solution...

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        HANDLE hToken = NULL;
        DWORD dwBufferSize = 0;
        PTOKEN_USER pTokenUser = NULL;
        int uid;
    
        /* Open the access token associated with the calling process. */
        if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
            printf("OpenProcessToken failed. GetLastError returned: %d\n",
                    GetLastError());
            return -1;
        }
    
        /* get the size of the memory buffer needed for the SID */
        (void)GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize);
    
        pTokenUser = (PTOKEN_USER)malloc(dwBufferSize);
        memset(pTokenUser, 0, dwBufferSize);
    
        /* Retrieve the token information in a TOKEN_USER structure. */
        if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwBufferSize,
                              &dwBufferSize)) {
            printf("2 GetTokenInformation failed. GetLastError returned: %d\n"
                    GetLastError());
            return -1;
        }
    
        CloseHandle(hToken);
    
        if (!IsValidSid(pTokenUser->User.Sid)) {
            printf("The owner SID is invalid.\n");
            free(pTokenUser);
            return -1;
        }
    
        uid = *GetSidSubAuthority(pTokenUser->User.Sid,
                        *GetSidSubAuthorityCount(pTokenUser->User.Sid) - 1);
        printf("uid = %u\n", uid);
    
        free(pTokenUser);
        CloseHandle(hToken);
    
        return 0;
    }
    that was no fun at all.
    Reply With Quote
      #10    
    Old August 24th, 2006, 07:44 AM
    crupp crupp is offline
    Member
     
    Join Date: Jan 2001
    Location: Germany, Bavaria
    Posts: 61
    crupp is an unknown quantity at this point (<10)
    Re: how to get the current user id?

    @pengch: i missed your last post, thanks a lot for your help. it's working now.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ and WinAPI


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:17 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009