[C] Get the current logged user (win2k)

This is a discussion on [C] Get the current logged user (win2k) within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I've searched for this but I found just linux stuff Is there a quick API or something like that, ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    [C] Get the current logged user (win2k)

    Hi, I've searched for this but I found just linux stuff

    Is there a quick API or something like that, that returns a struct or just the name of the current logged user?

    I have to do it in C on win 2k

    Thanx to everyone.
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx
    This forum is the best one I've ever seen. Great ppl, great coders

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #define INFO_BUFFER_SIZE 32767
    
    int main()
    
    {
      TCHAR  infoBuf[INFO_BUFFER_SIZE];
      DWORD  bufCharCount = INFO_BUFFER_SIZE;
    
      // Becca e stampa il nome dell'utente loggato 
      bufCharCount = INFO_BUFFER_SIZE;
      if(!GetUserName(infoBuf,&bufCharCount))
          
          printf("Errore nel determinare il nome dell' utente");
          
          printf("User name: %s\n", infoBuf); // stampa a video
          system("PAUSE");
    }
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    is there a way to know if a user belongs to a given group?

    thanx
    This forum is the best one I've ever seen. Great ppl, great coders

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To enumerate the groups a user belongs to, you can either get group membership information from the thread token (for the current user only) or call NetUserGetLocalGroups. Each has advantages and disadvantages. For example, the thread token will return pseduo groups such as INTERACTIVE and Everyone. See Usnet thread: How to get the groups of a user for more information.

    If you don't need a list of groups the user belongs to and just want to check if the current user belongs to a specific group you can use the CheckTokenMembership function. Even easier, if you need to check if the current user is an admin, you can use the IsUserAnAdmin function.
    Last edited by anonytmouse; 03-06-2005 at 08:42 AM.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Ah thanx like allways, you are great
    I think NetUserGetLocalGroups can help me.
    This forum is the best one I've ever seen. Great ppl, great coders

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I spent a lot modifying the example here .. I know I'm a newbie, anyway.. maybe this can help someone..


    If you want to declare the username, then:

    wchar_t username[] = L"USERNAME";

    and if you want to compare it remember that it is a pointer to a Unicode string :

    Code:
    if ( !lstrcmpW(pTmpBuf->lgrui0_name, L"watheveryouwant") )
                printf("ok\n");
                else
                printf("not ok\n");
    This forum is the best one I've ever seen. Great ppl, great coders

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I have some problems.. at least one.. this programs is supposed to just print the group of the logged user
    Code:
    #include <stdio.h>
    #include <assert.h>
    #include <windows.h>
    #include "psapi.h"
    #include <string.h>
    #include <lm.h>
    
    #define INFO_BUFFER_SIZE 32767
    
    int main(void)
    {
       
       char infoBuf[INFO_BUFFER_SIZE];
       DWORD  bufCharCount = INFO_BUFFER_SIZE;
       bufCharCount = INFO_BUFFER_SIZE;
       
       LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;
       DWORD dwLevel = 0;
       DWORD dwFlags = LG_INCLUDE_INDIRECT ;
       DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
       DWORD dwEntriesRead = 0;
       DWORD dwTotalEntries = 0;
       NET_API_STATUS nStatus;
    
       GetUserName(infoBuf,&bufCharCount);
       
       nStatus = NetUserGetLocalGroups(NULL,
                                       infoBuf,
                                       dwLevel,
                                       dwFlags,
                                       (LPBYTE *) &pBuf,
                                       dwPrefMaxLen,
                                       &dwEntriesRead,
                                       &dwTotalEntries);
    
       if (nStatus == NERR_Success)
       {
          LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
          DWORD i;
          DWORD dwTotalCount = 0;
    
          if ((pTmpBuf = pBuf) != NULL)
          {
             for (i = 0; i < dwEntriesRead; i++)
             {
                assert(pTmpBuf != NULL);
    
                if (pTmpBuf == NULL)
                {
                   fprintf(stderr, "An access violation has occurred\n");
                   break;
                }
                
                wprintf(L"%s\n", pTmpBuf->lgrui0_name);
             }
          }
       }
       
       if (pBuf != NULL)
          NetApiBufferFree(pBuf);
       system("PAUSE");
       return 0;
    }
    while compiling my devcpp says :

    [Warning] passing arg 2 of `NetUserGetLocalGroups' from incompatible pointer type

    in effects it is waiting a wchar_t and i'm passing a char.. how can I convert it?
    This forum is the best one I've ever seen. Great ppl, great coders

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To convert a char string to a wchar_t string, you can use either the C library function mbstowcs or the Windows function MultiByteToWideChar. However, it is usually preferable to avoid the conversion by retrieving the string in unicode in the first place. In this case, this can be achieved by using the wide character version of GetUserName. Most Windows functions that take strings are available in three versions:
    Code:
    /* TCHAR version. This is a macro that is replaced with GetUserNameA or GetUserNameW,
     * depending on whether UNICODE is defined. */
    BOOL GetUserName(LPTSTR lpBuffer,  LPDWORD nSize);
    
    /* Ansi version. This version takes and returns ansi char strings. The function name
     * is followed by the capital letter A. */
    BOOL GetUserNameA(LPSTR lpBuffer,  LPDWORD nSize);
    
    /* Wide version. This version takes and returns unicode wchar_t strings. The function
     * name is followed by the capital letter W. Wide character versions of functions are
     * typically only available on Windows NT/2000/XP/2003, and not Windows 9x/ME.
     * However, the use of the Microsoft library for Unicode with your project makes
     * most of them available on Windows 9x/ME. */
    BOOL GetUserNameW(LPWSTR lpBuffer,  LPDWORD nSize);
    As you can see, we can use the GetUserNameW version to retrieve the user name as a wchar_t string. I have also simplified the code for printing out the groups.
    Code:
    int main(void)
    {
       
       WCHAR infoBuf[INFO_BUFFER_SIZE];
       DWORD  bufCharCount = INFO_BUFFER_SIZE;
       bufCharCount = INFO_BUFFER_SIZE;
       
       LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;
       DWORD dwLevel = 0;
       DWORD dwFlags = LG_INCLUDE_INDIRECT ;
       DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
       DWORD dwEntriesRead = 0;
       DWORD dwTotalEntries = 0;
       NET_API_STATUS nStatus;
    
       GetUserNameW(infoBuf,&bufCharCount);
       
       nStatus = NetUserGetLocalGroups(NULL,
                                       infoBuf,
                                       dwLevel,
                                       dwFlags,
                                       (LPBYTE *) &pBuf,
                                       dwPrefMaxLen,
                                       &dwEntriesRead,
                                       &dwTotalEntries);
    
       if (nStatus == NERR_Success && pBuf != NULL)
       {
          DWORD i;
    
          for (i = 0; i < dwEntriesRead; i++)
          {
             wprintf(L"%s\n", pBuf[i].lgrui0_name);
          }
       }
    
       if (pBuf != NULL)
          NetApiBufferFree(pBuf);
       system("PAUSE");
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Talking

    Quote Originally Posted by anonytmouse
    Most Windows functions that take strings are available in three versions
    Cool! That's gold for me I never heard about that and It's really useful!

    Thanx for you code too, I will include you in the credits hehe
    Right now I can't compile it, this evening I will try to finish this project.

    Thanx, have a nice day.
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Identifying Who Logged In & getlogin()
    By Crashgr in forum Linux Programming
    Replies: 12
    Last Post: 12-25-2004, 08:01 PM
  2. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM
  3. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 AM
  4. Linked List Problem
    By animeaholic in forum C Programming
    Replies: 1
    Last Post: 12-09-2002, 05:36 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21