?
Solved

WindowsNT system's information

Posted on 2000-02-16
7
Medium Priority
?
265 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:schild
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2528161
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...
0
 
LVL 86

Expert Comment

by:jkr
ID: 2528190
#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!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2528206
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);
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 2

Author Comment

by:schild
ID: 2539639
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;

}
0
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 2539733
You don't have to reject an (obviously correct) answer if you need more information1

>>1. how can I know if you enter the local machine or as
>>domain's user.

The easiest way is to compare the values of the environment variable 'USERDOMAIN' with the local computer name, e.g.

wchar_t awcEnv [ LM20_DNLEN + ,1];
wchar_t awcMachine [ MAX_COMPUTERNAME_LENGTH + 1];
DWORD dwLength = MAX_COMPUTERNAME_LENGTH + 1;

GetEnvironmentVariableW ( L"USERDOMAIN", awcEnv, LM20_DNLEN);

GetComputerNameW ( awcMachine, &dwLength);

if ( !wcsicmp ( awcMachine, awcEnv))
{
 // local account
}
else
{
 // domain account
}

(error checking omitted for brevity))

>>2. If user enter as a domain's user how can retrieve its
>>full name and his/her last login time.

This can be done by calling 'NetUserGetInfo()' at infolevel 3 and using a 'NET_USER_INFO_3' struct, as it has a member named  'usri3_last_logon' which will provide this information. Additionally, you'll have to provide the DC as the 1st parameter to this API - and this information can be retrieved by calling 'NetGetDCName()' providing the environment variable's value from above as the DC name.

(sorry for not being able to offer more code, but being at home, I have no compiler ;-)

0
 
LVL 2

Author Comment

by:schild
ID: 2539885
Adjusted points to 400
0
 
LVL 2

Author Comment

by:schild
ID: 2539914
Hello jkr


Thank you very much


Reuven Schild

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question