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 How to Packetize a TCP Stream >>
Last modified on 19 October 2001 at 15:55 UTC-7 Please send corrections to tangent@cyberport.com.
< Go to the main FAQ page << Go to the Home Page