Example - How to Get the Local User Name

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;
}

Back to the Advanced Issues page...
Back to the Examples page...


Go to my home page Go to my Important RFC Lists page Go to the main Programming Resources page

Please send updates and corrections to <tangent@cyberport.com>.