DetailPage-MSS-KB

Knowledge Base

Article ID: 139625 - Last Review: February 28, 2014 - Revision: 3.4

This article was previously published under Q139625
This article has been archived. It is offered "as is" and will no longer be updated.

On This Page

SUMMARY

Windows NT and Windows 95 may require that users log on to a computer or a 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 4.0 and later Windows operating systems.

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 additional information about an alternative to this method that will work on any of the platforms, including Windows 3.1 and Windows for Workgroups, click the following article number to view the article in the Microsoft Knowledge Base:
99921  (http://support.microsoft.com/kb/99921/ ) How to obtain network user IDs from within FoxPro

REFERENCES

For additional information on this API function, see 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, see the Visual FoxPro online Help file.

APPLIES TO
  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • Microsoft Visual FoxPro 7.0 Professional Edition
  • Microsoft Visual FoxPro 8.0 Professional Edition
  • Microsoft Visual FoxPro 9.0 Professional Edition
Keywords: 
kbnosurvey kbarchive kbhowto KB139625
Delen
Extra ondersteuningsopties
Microsoft Community Support-forums
Neem rechtstreeks contact met ons op
Een door Microsoft gecertificeerde partner zoeken
Microsoft Store