Link to home
Start Free TrialLog in
Avatar of schild
schild

asked on

WindowsNT system's information

When user press Ctrl-Alt-Del, user can see both user full name and logged name.
How can I retrieve this information, using the API's functions within WinNT enviroment?

Thanks
Reuven
Avatar of jkr
jkr
Flag of Germany image

You can get the user's logon name by calling 'GetUserName()'.

The user's full name can be retrieved by using the LAN Manager API 'NetUserGetInfo()', an exampl follows...
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winnt.h>

#include <tchar.h>
#include <lmcons.h>
#include <lmerr.h>
#include <lmaccess.h>
#include <lmapibuf.h>

#pragma comment ( lib, "netapi32.lib")

    char                acBuf   [   1024];

    char                acLogName   [   LM20_UNLEN  +   1];
    ULONG               ul          =   LM20_UNLEN  +   1;
    NET_API_STATUS      rc;
    USER_INFO_2*        pui2;
    DWORD               dwParmErr   =   0;

    ULONG ul;

    if  (   !GetUserName    (   acLogName,  &ul))
        {
            //  error
        }

    rc  =   NetUserGetInfo  (   NULL,
                                pwstrName,
                                2,
                                ( LPBYTE*) &pui2
                            );

    if  (   NERR_Success    !=  rc)
        {
            //  error
        }

    wsprintf    (   acBuf,
                    "Current user is '%s', the full name is '%S'\n",
                    acLogName,
                    pui2->usri2_full_name // UNICODE, thus the capital '%S' above!
                );

    printf  (   acBuf);

    NetApiBufferFree    (   pui2);


Feel free to ask if you need more information!
Ooops, sorry, have to correct myself - as the 1st argument to 'NetUserGetInfo()' is expected to be a UNICODE string, too, it's better to do all that using UNICODE:

(Please forget the above code ;-)

#define UNICODE

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winnt.h>

#include <tchar.h>
#include <lmcons.h>
#include <lmerr.h>
#include <lmaccess.h>
#include <lmapibuf.h>

#pragma comment ( lib, "netapi32.lib")

    wchar_t             awcBuf   [   1024];

    wchar_t             awcLogName  [   LM20_UNLEN  +   1];
    ULONG               ul          =   LM20_UNLEN  +   1;
    NET_API_STATUS      rc;
    USER_INFO_2*        pui2;
    DWORD               dwParmErr   =   0;

    ULONG ul;

    if  (   !GetUserName    (   acLogName,  &ul))
        {
            //  error
        }

    rc  =   NetUserGetInfo  (   NULL,
                                awcLogName,
                                2,
                                ( LPBYTE*) &pui2
                            );

    if  (   NERR_Success    !=  rc)
        {
            //  error
        }

    wsprintf    (   awcBuf,
                    "Current user is '%s', the full name is '%s'\n",
                    awcLogName,
                    pui2->usri2_full_name
                );

    wprintf  (   awcBuf);

    NetApiBufferFree    (   pui2);
Avatar of schild
schild

ASKER

Hello jkr

First I want to thak you about hellping me about this request, and except few minor fixs, you gave me exactly what I needed.
The only reason I reject your answer is that I have another question that have connection with this one, and don't and I'm prommise you that if you don't wan't or you can't answer me the second one, you will have Excellent!! points for this question immediately.
If you will be kind to answer me the second question as well I will add more 200 points.

So this is your fixed code and and it is work when I'm logged in using a local user, and gaine, thank you very much. But when I'm login using Domain login, the following senario occure:
the GetUserName() retreive successfully the domain user name I type. then when I'm using this user name within NetUserGetInfo()'s function it return me error number 2221, which means "wrong user/acount name". because the local station doesn't familiar with this user account.
My question are
1. how can I know if you enter the local machine or as domain's user.
2. If user enter as a domain's user how can retrieve its full name and his/her last login time.


and for the last time, thaks you for helping.

Reuven Schild



#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <winnt.h>

#include <tchar.h>
#include <lmcons.h>
#include <lmerr.h>
#include <lmaccess.h>
#include <lmapibuf.h>

#pragma comment ( lib, "netapi32.lib")

int main ()
{

    wchar_t             awcBuf   [1024];
    wchar_t             awcLogName  [LM20_UNLEN  +   1];
    ULONG               ul          =   LM20_UNLEN  +   1;
    NET_API_STATUS      rc;
    USER_INFO_2        *pui2;
    DWORD               dwParmErr   =   0;


      if ( NetApiBufferAllocate(sizeof(USER_INFO_2),(LPVOID *) &pui2) != NERR_Success )
      {
            printf("Can't allocate memory\n");
      }

    if  (   !GetUserName    (   awcLogName,  &ul))
    {
        printf("Can't get user name\n");
    }
      wprintf(TEXT("awcLogName = %s\n"), awcLogName);

    rc  =   NetUserGetInfo(NULL, awcLogName, 2, ( LPBYTE*) &pui2);

    if (NERR_Success !=  rc)
    {
            printf("Can't cget info\n");
    }
      wprintf(TEXT("pui2->usri2_full_name = %s\n"), pui2->usri2_full_name);


//    wsprintf    (   awcBuf,
//                    TEXT("Current user is '%s', the full name is '%s'\n"),
//                    awcLogName,
//                    TEXT(pui2->usri2_full_name)
//                );

//    wprintf(awcBuf);

    NetApiBufferFree(pui2);
      getch();

      return 0;

}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schild

ASKER

Adjusted points to 400
Avatar of schild

ASKER

Hello jkr


Thank you very much


Reuven Schild