We help IT Professionals succeed at work.

Getting the user's name on a local computer

Gesualdo
Gesualdo asked
on
It can be done, NetWatcher does it.
How do I get the user of a local computer?
On our network I have a name something like \\LOCALCOMPUTER but on my computer I have a logon name which is my full name.

Using the WNetGetUser function I am able to retrive my name but how do I get others'.

char username[256];
DWORD usernamelen = 255;
WNetGetUser(NULL, username, &usernamelen);

Now my logon name is in "username"

To retrive another computer's user it seems like it should work like this:

char username[256];
DWORD usernamelen = 255;
WNetGetUser("\\\\COMPUTERNAME\0", username, &usernamelen);

But this returns my logon name again.

The first return my name something like "My Name"
The second returns it as "MY NAME"

To clairify I want to get the logon name of another computer on the network instead of mine.

Please help.
Thank you.
Comment
Watch Question

did you include the domain/workgroup?

eg machine called CHIMP on domain MONKEY could be

WNetGetUser("\\\\MONKEY\\CHIMP", username, &usernamelen);
not to sure on the number of backslashes though :)
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Why don't you do it the way 'NetWatch' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/vcsmpnetwatch.asp) does it:

/*
 *     UsersEnum
 *
 *     Purpose:
 *          Enumerate the users on a sharepath and server
 *
 *     Arguments:
 *          lots o garbage
 *
 *     Returns:
 *          nas
 */
NET_API_STATUS
UsersEnum(HWND hwndTV, HTREEITEM hParent, LPTSTR szServerName, TCHAR *sharename,
     TCHAR *sharepath, TVDATASHARE *pTVDataShare)
{
     NET_API_STATUS          nas;
     TV_INSERTSTRUCT          tvis;
     HTREEITEM               hItem;
     CONNECTION_INFO_1     *pci1;
     CONNECTION_INFO_1     *ci1 = NULL;
     DWORD                    dwentriesread;
     DWORD                    dwtotalentries;
     static TCHAR          szFmtUser[cchSzMax + 1];
     TCHAR                    szUserT[NNLEN + UNLEN + cchSzMax + 1];

     // Some stuff doesn't change
     tvis.hInsertAfter = NULL;
     tvis.hParent = hParent;
     tvis.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;

     if(!szFmtUser[0])
          lstrcpy(szFmtUser, szFromIDS1(IDS_FMTUSER));

     nas = NetConnectionEnum(szServerName, sharename, 1, (LPBYTE *)&ci1,
          MAX_PREFERRED_LENGTH, &dwentriesread, &dwtotalentries, NULL);
     if(nas || !dwentriesread)
          goto err;

     qsort(ci1, dwentriesread, sizeof(ci1[0]), CompareUserNamesQSortCallback);
     for(pci1 = ci1; dwentriesread; pci1++, dwentriesread--)
     {
          TCHAR          *netname;
          TCHAR          *username;
          TVDATAUSER     *pTVDataUser;

          username = pci1->coni1_username;
          netname = pci1->coni1_netname;
          if(!username || !username[0])
               username = szNil;
          else
               CharLower(username);
          if(!netname || !netname[0])
               netname = szNil;
          else
               CharUpper(netname);

          if(lstrlen(netname) > CNLEN)
               netname[CNLEN] = 0;
          if(lstrlen(username) > UNLEN)
               username[UNLEN] = 0;

          wsprintf(szUserT, szFmtUser, username, netname);

          // Things to remember for properties, delete, etc.
          if(pTVDataUser = (TVDATAUSER *)GlobalAllocPtr(GHND, sizeof(TVDATAUSER)))
          {
               pTVDataUser->dwType = TYPE_USER;
               lstrcpy(pTVDataUser->szUserName, username);
               pTVDataUser->szNetName[0] = pTVDataUser->szNetName[1] = '\\';
               lstrcpy(&pTVDataUser->szNetName[2], netname);
               pTVDataUser->pTVDataShare = pTVDataShare;
          }

          tvis.item.lParam = (LPARAM)pTVDataUser;
          tvis.item.pszText = szUserT;
          tvis.item.iImage = tvis.item.iSelectedImage = BMP_USER;
          hItem = TreeView_TVDataInsert(hwndTV, &tvis);

          // If we haven't run into any errors and the user wants to see files
          if(pci1->coni1_num_opens &&
               unMenuFlags[IDM_SHOWFILES & 0xff] == MF_CHECKED && !nas)
          {
               // Enum the files
               nas = FilesEnum(hwndTV, hItem, szServerName, username,
                    sharepath, pTVDataUser);
               // make sure we're expanded
               TreeView_Expand(hwndTV, hItem, TVE_EXPAND);
          }
          else
          {
               // Delete all our children
               FTreeView_DeleteRange(hwndTV, hItem, NULL, NULL);
          }
     }

     // reset any filesenum errors we may have encountered
     nas = 0;

err:
     // Whack any stragglers hanging at the end
     FTreeView_DeleteRange(hwndTV, hParent, tvis.hInsertAfter, NULL);

     NetApiBufferFree(ci1);
     if(nas)
          AddErrorStringToTV(hwndTV, hParent, IDS_ERRENUMUSERS, nas);
     return nas;
}

Author

Commented:
I'm confused where you get the username in this function.
I have not been able NetConnectionEnum function to work for me, can I still get the user name using WNetEnumResource?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>I'm confused where you get the username in this function

From 'CONNECTION_INFO_1::coni1_username'. BTW, you cannot get the name of a user on a remote machine using 'WNetGetUser()' - as the docs state: 'The WNetGetUser function retrieves the current default user name or the user name used to establish a network connection'

You'll most likely receive your own logon name (unless yoou connected to a server using different credentials)
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Gesualdo, are you still with us?
Technical consultant & graphic design
CERTIFIED EXPERT
Top Expert 2004
Commented:
Hopefully you've already been helped with this question, but thought you'd appreciate knowing this.  It would be great if you could bring this question to a conclusion, awarding the experts above who helped you with points or a comment to them on your status today.

WindowsUpdate has new updates for .NET users; Details follow - Microsoft .NET Framework
The .NET Framework is a new feature of Windows. Applications built using the .NET Framework are more reliable and secure. You need to install the .NET Framework only if you have software that requires it.

For more information about the .NET Framework, see http://www.microsoft.com/net. (This site is in English.)

System Requirements
The .NET Framework can be installed on the following operating systems:
Windows 98
Windows 98 Second Edition (SE)
Windows Millennium Edition (Windows Me)
Windows NT 4.0® (Workstation or Server) with Service Pack 6.0a
Windows 2000 with the latest service pack installed (Professional, Server, Datacenter Server, or Advanced Server)
Windows XP (Home Edition and Professional)
You must be running Internet Explorer version 5.01 or later for all installations of the .NET Framework.

To install the .NET Framework, your computer must meet or exceed the following software and hardware requirements:

Software requirements for server operating systems:
MDAC 2.6
Hardware requirements:
For computers running only a .NET Framework application, Pentium 90 mHz CPU with 32 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 mHz CPU with 128 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
Recomended software:
MDAC 2.7 is recommended.
Recommended hardware: For computers running only a .NET Framework application, Pentium 90 MHz CPU with 96 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.
For server operating systems, Pentium 133 MHz CPU with 256 MB memory or the minimum CPU and RAM required by the operating system, whichever is higher.

How to use -> Restart your computer to complete the installation. No other action is required to run .NET Framework applications. If you are developing applications using the .NET Framework, you can use the command-line compilers or you can use a development environment, such as Visual Studio .NET, that supports using the .NET Framework.

How to uninstall
To uninstall the .NET Framework: Click Start, point to Settings, and then click Control Panel (In Windows XP, click Start and then click Control Panel.).
Click Add/Remove Programs.
Click Microsoft .NET Framework (English) v1.0.3705 and then click Change/Remove.
More here  http://www.microsoft.com/net/

The .NET topic is being considered for addition to our All Topics link soon, so this may interest you as well:
http://www.experts-exchange.com/newtopics/Q.20276589.html

EXPERTS POINTS are waiting to be claimed here:  http://www.experts-exchange.com/commspt/Q.20277028.html

":0)
Asta


jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
????????????????????????????????????????????????????????????
astaec sent me this link and asked that I help correct the error made here.  She merely wanted to bring old questions to your attention so you can award the experts who helped you with the technical topic at hand.  The explanation of why she used this process is here:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20287577

I have refunded these 300 points to you.  EXPERTS please guide me here as to who should have been awarded so I can take corrective actions here.

Moondancer - EE Moderator

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Well, 'NetConnectionEnum()' actually does what gesualdo asked for, though I think he/she never bothered to try it...
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Thank you!