<<

Winsock Programmer's FAQ
Example: How to Get the Username

>>

The Win32 API has a simple function called GetUserName() that works under Windows 95 and Windows NT. The GetUserName() function is very easy to use:


getusername.cpp

// Borland C++ 5.0: bcc32.cpp getusername.cpp
// Visual C++ 5.0: cl getusername.cpp advapi32.lib 

#include <iostream.h>
#include <windows.h>

int main()
{
    char acUserName[100];
    DWORD nUserName = sizeof(acUserName);
    if (GetUserName(acUserName, &nUserName) == 0) {
        cerr << "Failed to lookup user name, error code " << GetLastError()
            << "." << endl;
    }
    cout << "User name is " << acUserName << "." << endl;

    return 0;
}

<< Get MAC Address Maximum Sockets Tester >>
Last modified on 29 April 2000 at 15:52 UTC-7 Please send corrections to tangent@cyberport.com.
< Go to the main FAQ page
<< Go to my Programming pages
<<< Go to my Home Page