Announcement

Collapse
No announcement yet.

Determine Windows computer name and current logged-in user in Win9x

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Determine Windows computer name and current logged-in user in Win9x

    Answered my own question. Here is how to get username and machine/computername across all versions of Windows
    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
     
    FUNCTION GetloggedOnUser() AS STRING
        DIM acUserName AS ASCIIZ * 100
        DIM nUserName AS LONG
        nUserName = SIZEOF(acUserName)
        IF (GetUserName(acUserName, nUserName) = 0) THEN
            STDOUT "Failed to lookup user name, error code " + STR$(GetLastError)
        ELSE
            STDOUT TRIM$(acUserName)
        END IF
    END FUNCTION
    
    FUNCTION GetloggedOnComputer() AS STRING
        DIM acUserName AS ASCIIZ * 100
        DIM nUserName AS LONG
        nUserName = SIZEOF(acUserName)
        IF (GetComputerName(acUserName, nUserName) = 0) THEN
            STDOUT "Failed to lookup computer name, error code " + STR$(GetLastError)
        ELSE
            STDOUT TRIM$(acUserName)
        END IF
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
     STDOUT "User=" & GetLoggedonUser
     STDOUT "Comp=" & GetLoggedonComputer
     WAITKEY$
    END FUNCTION

    [This message has been edited by Wayne Diamond (edited August 15, 2001).]
    -

  • #2
    Wayne --

    FYI,

    Code:
    %MAX_COMPUTERNAME_LENGTH = 15
    ...and MSDN says this about the string that is passed to GetComputerName...

    This value should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.

    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>

    [This message has been edited by Eric Pearson (edited August 15, 2001).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      Wayne,

      I'm using WNetGetUser, to get the username. Is there any difference?

      ------------------
      mailto:[email protected][email protected]</A>
      www.basicguru.com/zijlema/

      Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
      http://zijlema.basicguru.eu
      *** Opinions expressed here are not necessarily untrue ***

      Comment


      • #4
        Egbert, Im not sure sorry! All i know is that GetUserName is in advap32.dll, whereas WNetGetUserName is in mpr.dll ... (buggered if i know what that means tho!)
        Just did a quick hunt around on google.com and found this... http://www.pbdr.com/pbtips/ap/usrlogid.htm -
        You can obtain the users network login id for most popular network clients with a call to the windows API call WNetGetUserName. This call works for Netware, Windows for Workgroups, Windows NT, Windows 95 and LanManager. For 32 bit applications you will need to make a different call to the API function GetUserNameA().
        So... WNetGetUserName is for Win 3.x/16-bit apps, GetUserName is for 32-bit apps??? I'm not sure...


        ------------------
        -

        Comment


        • #5
          Winlog For Windows 2000:
          Been doing this for 3 years without failure.
          Regardless of OS.
          Code:
          Function CCSGetUserName() Export As String
          Local lResult As Long
          Local buff    As Asciiz * 48
          lResult = GetUserName(buff, SizeOf(buff))
          Function = buff
          End Function
          '------------------------------------------------------------------------------------------
          Function CCSGetComputerName()Export As String
          Local lResult As Long
          Local buff    As Asciiz * 17
          lResult = GetComputerName(buff, SizeOf(buff))
          Function = buff
          End Function
          ------------------
          Scott
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            PS, if memory serves me correctly, MAX_COMPUTERNAME_LENGTH can only be 15 plus one null, plus one character for the string null ? (or just 16 plain outright).
            I put 17 just to be safe...

            Username I am not sure of however, I forgot what the limit on it is but 48 seemed generous enough...

            ------------------
            Scott
            Scott Turchin
            MCSE, MCP+I
            http://www.tngbbs.com
            ----------------------
            True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

            Comment

            Working...
            X