Solved

WindowsNT system's information

Posted on 2000-02-16
7
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 400 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error email attachment VB2010 5 33
Read the sort values being used for tasks in a Microsoft Project schedule? 4 29
Need example 5 108
Explain Unit of Work pattern 2 59
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now