Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WindowsNT system's information

Posted on 2000-02-16
7
Medium Priority
?
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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