Solved

WindowsNT system's information

Posted on 2000-02-16
7
256 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
Comment Utility
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
Comment Utility
#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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Author Comment

by:schild
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 400
0
 
LVL 2

Author Comment

by:schild
Comment Utility
Hello jkr


Thank you very much


Reuven Schild

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

11 Experts available now in Live!

Get 1:1 Help Now