Locate the user currently logged on to the network on the computer

Source: Internet
Author: User
Tags network function sprintf

Some programmers want to be able to display the user name that the user is currently logged on to. This allows the programmer to identify the user who is currently attempting to perform an action, or to notify the user that the machine has been logged in with a user name other than a user. How do I use the Windows 9x network function to do this?

Use the Windows API function Wnetgetuser to determine which user is currently logged on. In the WIN32 system (Windows 9x and Windows NT), this function accepts the local drive name and returns the user name and its size (the size of the username buffer).

Follow these steps to implement an example program. Run this example program, select menu item User Names from menu NetWorks, a dialog box will pop up to display the drive installed on this machine. When you select a drive from the list, the text field is changed to the user name or string "None" that is connected to this drive.

The specific steps to implement the example program are as follows:

1. In Visual C + +, use AppWizard to create a new project file and name this project file as LD17. MAK.

2. Enter the Appstudio and create a new dialog box, in the dialog box, add a title of Drive Ltter: and network Name: and the User name: three text fields. Add a combo box with the style set to Drop down List. Add two static text fields with identifiers: Id_net_name and Id_net_user.

3. Change the dialog box ID to Id_network_user and set the title to View network Users to save the dialog box.

4. Enter ClassWizard, select button Add Class. Type the name Cnetworkuserdlg in the Name field, the base class is CDIALOG, and the dialog box ID is Id_network_user.

5. Select Cnetworkuserdlg from the ClassWizard drop-down list, select Message wm_initdialog from the list of messages, and click the button Edit Code.

6. Enter the following code in the Cnetworkuserdlg method OnInitDialog:

BOOL CNetworkUserDlg::OnInitDialog()
{
CDialog::OnInitDialog();
char buf[20];
CComboBox *combo=(CComboBox *)GetDlgItem(IDC_COMBO1);
for(int i=0;i<26;i++)
{
sprintf(buf,"%c:\\",'A'+i);
WORD type="GetDriveType(buf);"
switch(type)
{ case DRIVE_FIXED: sprintf(buf,"%c:",'A'+i); combo->AddString(buf);
break;
case DRIVE_REMOTE:
sprintf(buf,"%c:",'A'+i);
combo->AddString(buf);
break;
case DRIVE_CDROM:
sprintf(buf,"%c:",'A'+i);
combo->AddString(buf);
break;
}
}
combo->SetCurSel(0);
OnDriveChange();
return TRUE; // return TRUE unless you set the focus to a control
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.