How To Use and Call the GetUserName API

Last reviewed: October 17, 1996
Article ID: Q139625
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 5.0

SUMMARY

Windows NT and Windows 95 may require that users log on to a computer or network. On occasion, you may want to use the login user id for your application. By using the GetUserName API call, you can discover the name of the user who is currently logged on to the system. This article shows by example how to use and call the Win32 GetUserName API.

MORE INFORMATION

Having the user id of the currently logged in user can be helpful in multiuser programming for situations such as updating a table field with the user id of a user who has a record locked.

The GetUserName API call is specific to Windows 95 and Windows NT and cannot be used in Windows or Windows for WorkGroups.

The return value held in the variable lpUserIDBuffer is null terminated, so to get just the user ID, the following sample code trims the actual return value by using the LEFT, LEN, and ALLTRIM functions.

Sample Code

**--------------------------------------------------------**

** Program: Getuser.prg                                   **
** Purpose: Demonstrates how to use and call the Win32    **
** GetUserName API.                                       **
**--------------------------------------------------------** PUBLIC lpUserIDBuffer, ;
        nBufferSize, ;
        RetVal

RetVal         = 0
lpUserIDBuffer = SPACE(25) && Return buffer for user ID string
nBufferSize    = 25        && Size of user ID return buffer

DECLARE INTEGER GetUserName IN Win32API AS GetName ;
        STRING  @lpUserIDBuffer, ;
        INTEGER @nBufferSize

RetVal=GetName(@lpUserIDBuffer, @nBufferSize)

DEFINE WINDOW ShowInfo FROM 0,0 TO 5,70 ;

        FLOAT CLOSE ;
        TITLES "User ID Information" ;
        FONT "Courier",10

ACTIVATE WINDOW ShowInfo MOVE WINDOW ShowInfo CENTER

@ 0,1 SAY "User ID : " + ;

  LEFT(lpUserIDBuffer,nbuffersize-1)

General Information

For an alternative to this method that will work on any of the platforms, including Windows 3.1 and Windows for Workgroups, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q99921
   TITLE     : Obtaining Network User IDs from Within FoxPro

REFERENCES

For additional information on this API function, please refer to the online Win32API help file under the "GetUserName" topic. Note that this help file is available only on the compact disc version of Visual FoxPro. It is not shipped with the floppy disk version.

For more information about the DECLARE DLL command, please refer to the Visual FoxPro online Help file.


Additional reference words: 5.00 3.00 WIN32API
KBCategory: kbprg kbhowto
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.