How can I get "Full Computer Name"

ghudson

Registered User.
Local time
Today, 18:12
Joined
Jun 8, 2002
Messages
6,193
Is it possible to get the users Computer Name and Full Computer Name? I am using Windows XP with Access 97.

Thanks in advance for your help!
 
Obtaining a Network User's Full Name and Comments

Thanks for the reply Travis. I had already tried that code yet it errors when I try to use it. I keep getting runtime error # 2185 "You can't reference a property or method for a control unless the control has the focus."

I tried adding .SetFocus in the code but there are a few lines of code testing two text boxes and I can not figure out how to fix the code to work for me using Access 97 with Windows XP.

I have attached my example db using the code for "Obtaining a Network User's Full Name and Comments" @ http://www.mvps.org/vbnet/ if anybody is willing to look at it and determine what is wrong with the original code.

Thanks in advance for anybody willing to take the time and look at this db and help me solve my problem. THANKS! :)
 

Attachments

Remove the .text from the code in the Load event, try this instead.

' Me.Text1.SetFocus 'ghudson added
Text1 = rgbGetUserName()
' Me.Text2.SetFocus 'ghudson added
Text2 = rgbGetComputerName()
 
That helped so that the first two fields are populated but the Command1 button still does not work. I get the same error message as before. I then removed all .Text in the code and now the Command1 button code does not work at all (but now no errors yet no results either).

Any suggestions? Thanks in advance for your help!
 
I ran your file after removing all the .Text and it worked perfectly. I am using Office XP which could be the difference (but I didn't upgrade the file). Might be that one of the API functions isn't working for Access 97 quite as it should.

Two things you might want to try. Put a break point in the GetUserNetworkInfo function to make sure the If statement returns true.

If your code runs all the way through without a break then that would be where your problem is. It's not successfully completing the call to NetUserGetInfo().

I think that is what's happening. But if it does actually go into the if statement then place a debug.print after each of the Member Extracts:

GetUserNetworkInfo.name = GetPointerToByteStringW(usrapi.usr10_name)
Debug.Print GetUserNetworkInfo.name


Anyway, I wanted you to at least know that someone was able to get it to work on their system.
 
Something is missing on my computer. I added an Else with a MsgBox to the GetUserNetworkInfo function to test if it is returning True and it is NOT returning True.

I then copied the code into VB6 (I am a novice with VB6) and I am still encountering the same problem (GetUserNetworkInfo is not returning true).

In my Access 97 example, I removed the reference to DAO 3.5 and added DAO 3.6 but nothing changed.

Any suggestions? Thanks in advance for your help!
 
Well, the function that your having problems with is held in the netapi32.dll you could be missing this file (but it seems like that's file that should be on your computer). Check your \System32 folder for this file.
 
I'm running Windows 2000 and A97 and the code will not work as is for me either. In stepping through the code it returns false for the NetUserGetInfo.

Part of the problem seems to be that it 'bServername' is using the current computer name rather than the server name. I changed the code to use the server name and it will now return the user name but not the fullname or comments.

To get the server name you need the following

Code:
Declare Function apiNetGetDCName Lib "netapi32.dll" Alias "NetGetDCName" _
(ByVal servername As Long, ByVal DomainName As Long, bufptr As Long) As Long

Function fGetDCName() As String
  Dim pTmp As Long
  Dim lngRet As Long
  Dim abytBuf() As Byte
 
  lngRet = apiNetGetDCName(0, 0, pTmp)
  If lngRet = 0 Then
    fGetDCName = fStrFromPtrW(pTmp)
  End If
  Call apiNetAPIBufferFree(pTmp)
    
End Function

bServername = fGetDCName & vbNullChar

As I said this will get a True for NetUserGetInfo but still it will not return all the values, just the name, the User_Info doesn't seem to populate fully.

I use the fullname in a database and below is the function I use to obtain it, it can easily be adapted to send back any of the network info. Some of the API calls are named differently than your own but should be easy to identify for changing.
Code:
Function GetUserFullName(Optional strUserName As String) As String
'
' Returns the full name for a given UserID
'   NT/2000 only
' Omitting the strUserName argument will try and
' retrieve the full name for the currently logged on user
'
On Error GoTo ErrHandler
Dim pBuf As Long
Dim dwRec As Long
Dim pTmp As USER_INFO_2
Dim abytPDCName() As Byte
Dim abytUserName() As Byte
Dim lngRet As Long
Dim i As Long
 
    ' Unicode
    abytPDCName = fGetDCName() & vbNullChar
    If (Len(strUserName) = 0) Then strUserName = GetUserName()
    abytUserName = strUserName & vbNullChar
 
    ' Level 2
    lngRet = apiNetUserGetInfo(abytPDCName(0), abytUserName(0), 2, pBuf)
    If (lngRet = ERROR_SUCCESS) Then
        Call sapiCopyMem(pTmp, ByVal pBuf, Len(pTmp))
        ' can easily change the return value in the line below
        GetUserFullName = fStrFromPtrW(pTmp.usri2_full_name)
    End If
 
    Call apiNetAPIBufferFree(pBuf)
ExitHere:
    Exit Function
ErrHandler:
    GetUserFullName = vbNullString
    Resume ExitHere
End Function
 
I have the netapi32.dll file in my C:\WINDOWS\system32\ folder. The file is dated 08/23/2001.

I would imagine that I would get an error message if the file was missing or corrupt. Could you try converting your sample db to Access 2002 and then back to Access 97 and post that file? Do you think that would help or is there something on my end that is not quite "right"?

This is not a do or die project but it sure would be nice if I could use these functions in my db's.

Thanks for your help!
 
Sorry had left off the declaration code for the USER_INFO_2 data type.

Code:
Public Type USER_INFO_2
    usri2_name As Long
    usri2_password  As Long  ' Null, only settable
    usri2_password_age  As Long
    usri2_priv  As Long
    usri2_home_dir  As Long
    usri2_comment  As Long
    usri2_flags  As Long
    usri2_script_path  As Long
    usri2_auth_flags  As Long
    usri2_full_name As Long
    usri2_usr_comment  As Long
    usri2_parms  As Long
    usri2_workstations  As Long
    usri2_last_logon  As Long
    usri2_last_logoff  As Long
    usri2_acct_expires  As Long
    usri2_max_storage  As Long
    usri2_units_per_week  As Long
    usri2_logon_hours  As Long
    usri2_bad_pw_count  As Long
    usri2_num_logons  As Long
    usri2_logon_server  As Long
    usri2_country_code  As Long
    usri2_code_page  As Long
End Type

Thus you could change the line in GetUserFullName from
GetUserFullName = fStrFromPtrW(pTmp.usri2_full_name)
to
GetUserFullName = fStrFromPtrW(pTmp.usri2_comment)
or
GetUserFullName = fStrFromPtrW(pTmp.usri2_usr_comment)
to return the values of comment and user comment.
 
I've done as you requested, changing the '97 file to 2002 and then back to '97.

Hope it works! (though I must admit I'm not overly optimistic) :(
 

Attachments

Drevlin,

Thanks for taking the time to convet my file. As we expected, there was no change but it leaset we tried.

The odd thing is it does not work on my Windows XP computer using Access 97 and it does not work with Visual Basic 6. I just wish I knew what the common "problem" was. I agree that it is is the GetUserNetworkInfo() function that will not test True.

Thanks for your help!
 
antomack,

I was trying your code but I am missing the GetUserName() function. Can you please post it? Thanks!
 
Code:
Function GetUserName() As String
' Returns the network login name

  Dim lngLen As Long, lngRet As Long
  Dim strUserName As String
    
  strUserName = String$(254, 0)
  lngLen = 255
  lngRet = apiGetUserName(strUserName, lngLen)
  If lngRet Then
    GetUserName = Left$(strUserName, lngLen - 1)
  End If
  
End Function
 
Try the enclosed. I know it works on 98 & 2K. You were kind enough to help me the other day with OS version which works a treat for what I needed.

John
 

Attachments

John,

Thanks for the sample db. It works.

My goal was to get the users FULL computer name. In Windows XP, my computer name is J254123HUDSONG but my FULL Computer Name is J254123HUDSONG.xyz.corp.net

Any suggetions on how I can get this with my Windows XP computer using Access 97? The previous suggestions do not work with my programs so I am guessing it is an Access 97 problem.

Thanks in advance for your help!
 

Users who are viewing this thread

Back
Top Bottom