Solved

Using api calls to get user name and computer name sometimes doesn't work

Posted on 2010-11-26
13
477 Views
Last Modified: 2012-05-10
Hi all,

Hope you can help.

I've install functionality in one of my clients systems to let me know who is logged in on what computer name. I'm using the functions you see below to get these values.

The problem is on some computers it works and on others it doesn't and I don't know why?

Does anyone have any ideas where I could look to resolve this issue?

Thanks:

Code:

Private Declare Function apiGetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSMachineName() As String

'Returns the computername
    Dim lngLen As Long, lngX As Long
    Dim strCompName As String
    lngLen = 16
    strCompName = String$(lngLen, 0)
    lngX = apiGetComputerName(strCompName, lngLen)
    If lngX <> 0 Then
        fOSMachineName = Left$(strCompName, lngLen)
    Else
        fOSMachineName = ""
    End If
End Function

'User Name function


Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If lngX <> 0 Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = ""
    End If
End Function


0
Comment
Question by:eantar
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
In the case where it doesn't work, what exactly happens or does not happen?

These are ... the standard API calls for doing this, and I have used them for a long time across multiple different versions of Windows and Access.

mx
0
 
LVL 5

Author Comment

by:eantar
Comment Utility
Thanks for your reply. These are satellite clients to this one database. I'm not sure why it doesn't work for them. I just know that the db I use to track who is currently in the app isn't logging certain users.

I'll try and get more info for you.

E
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
ok
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Especially if Win 7 X64 bit system ... etc.

mx
0
 
LVL 5

Author Comment

by:eantar
Comment Utility
I'm win 7 64 bit and it works with me so I don't think that's the issue. will let you know.

Thanks again,
E
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Yeah, same for one of my laptops ... no issue.

mx
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 84
Comment Utility
Is this a standard networked application? Or are they working in a virutal machine, or a remote technology (like Terminal Services or Citrix)?
0
 
LVL 5

Author Comment

by:eantar
Comment Utility
Good question. the front and back end sit on a server and, as far as I know, everyone accesses the application through Remote Desktop, including myself AND the users that get logged.

Any thoughts?

E
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
Check this post, especially the comment from Ian Williams:

http://www.eggheadcafe.com/software/aspnet/32622964/computer-name-of-remote-desktop.aspx



0
 
LVL 4

Expert Comment

by:BrainB
Comment Utility
FWIW I have had some strange issues which are related to security settings (Win7 64bit). I know this because others have been solved by spending ages tweaking the settings. Everything worked on first installation on the new PC and then deteriorated as Windows updates were installed.

eg. in my home setup I have a shared drive from which I am unable to run an .exe file from the host, but can do so from another computer that accesses the drive over my newtwork(Windows XP 32bit). If I transfer the file to another unshared drive on the same PC I can run it.
0
 
LVL 5

Author Comment

by:eantar
Comment Utility
Hey LMSConsulting. Just copied and pasted the code in the link you sent me. I'm not on Terminal Services but it looks good. I'll test it through Remote Desktop tomorrow and let you know how it goes.

Thanks,
E
0
 
LVL 5

Author Closing Comment

by:eantar
Comment Utility
Sorry for the lateness on this. Function works really well. Thanks again.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now