Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Current Network Username 2

Status
Not open for further replies.

SpankYou

Programmer
Feb 24, 2003
211
0
0
GB
Hey,

I was just wondering how you might obtain the current network User's username, I have an example for an application but not for the network...

e.g ... Application.CurrentUser

Thanks for any help

Sam




"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
Cheers for the help Ben,

I managed to find what I needed, not sure what I typed in differently in my search but I found this...

thread96-613910 for your help

Cheers

Sam

"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
when you say network name, do you mean you want a network login username? if so i got some code off the web for that - i've also attached it below:

Declare Function GetNTName_from_file Lib "advapi32.dll" Alias "GetUserNameA" (ByVal IpBuffer As String, nsize As Long) As Long
Public strUser As String

'Get the current user (Login name)
Function GetNTName() As Variant
Dim strUName As String
Dim IngLen As Long
Dim IngRes As Long

'Sets up the buffer
strUName = String$(255, 0)
IngLen = 255

'Makes the call
IngRes = GetNTName_from_file(strUName, IngLen)

'Assigns the value
GetNTName = strUName

End Function

'Convert above response into text
Function ReturnNTName() As String

Dim num As Long
Dim start As Long
Dim final As String
Dim tmpStr As String

tmpStr = GetNTName()
num = 1
start = Len(GetNTName())
final = ""

Do Until Asc(Mid$(tmpStr, num, 1)) = 0
final = final & Mid$(tmpStr, num, 1)
num = num + 1
Loop

strUser = final

ReturnNTName = final

End Function
 
I do not know if this is the best way to do this but I use very simple code. I use the network name and machine name to log access to various areas in my front end so I can see where they have been if something gets a discrepancy.

Here is my code:

Dim UserName As String
...
...

UserName = Environ("UserName")
...
...

Very simple but I dont know if this is what you want. This gets the logon id. IE, if I use schmoej to logon on to my workstation then it gives UserName = "schmoej"

HTH
 
My methode for this is very similar to the last post but with more code required:
Code:
Public Function f_getUser() As String
    f_getUser = f_Network("user")
End Function

Public Function f_getComputer() As String
    f_getComputer = f_Network("computer")
End Function

Private Function f_Network(szType) As String
   Dim oNet As Object
   Set oNet = CreateObject("WScript.Network")
   Select Case szType
        Case "user"
            f_Network = oNet.UserName
        Case "computer"
            f_Network = oNet.computername
        Case Else
        f_Network = "Error encountered. '" & szType & "' not found."
    End Select
    Set oNet = Nothing
End Function
 
The following works for me:

UserName = Right(Environ(3), 8)
ComputerName = Environ$("ComputerName")
 
Hey Guys,

Cheers for that I'll give it a try when the company decides to give me a computer that works! I feel being a systems analyst/software engineer it could be a minimum requirement!

Anyways thanks and I'll let you know what happens

Cheers

Sam

"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
Hey,

I've tested that Environ declaration bit out n it doesn't work at all for solas 989's example, and for Oliver 2003's eample it returns OWS/TEM, which I am Assuming is the file name WINDOWS/TEMP, which has completely baffeled me.

Thanks everyone for your help, I have managed to get it working, but I wondered If anyone knew off hand how to obtain the password of the user so basically, when they log into my program they need their username and password to login? Cheers for any help

Sam

"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
You can't get the users password (just think of the security implications!) but you can use the NT challenge Process to verify a users details.
Have a look at for the code you need. I you need any help converting it to VBA, let me know.

Cheers

Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Hey Ben,

yeah I thought that might be the case, I've tried to implement the code and it runs but as it is not NT version5 or later it by passes all the code and so doesn't work. I took out the version check n it still doesn't work,

Any ideas?

Thanks for your help

Sam

"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
what os are you using?
B

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Hey,

I'm using Windows ME, which i just realised does not meet the requirements of the code, but thankfully after much searching of the net I have found some code that has enabled me to implement the idea...


Thanks for all your help everyone, I'm off to enjoy a semi earned rest!

Cheers

Sam

"You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine." - Homer
 
Hey,

I might start this as a new thread cos it's really a new problem but with the code I have, anyways I'll try it here first. Basically when the code below checks the windows password (from the Id already obtained), if the password matches then it works in under a second, if the password doesn't match then it crashes.

Public Function VerifyWindowsLoginUserPassword _
(ByVal Password As String) As Boolean
Dim rtn As Long, Match As Long
rtn = WNetVerifyPassword(Password, Match) 'reads this line and crashes
If rtn Then
VerifyWindowsLoginUserPassword = False
Else
VerifyWindowsLoginUserPassword = (Match <> 0)
End If
End Function


If you have any Ideas I'd be very appreciative. Until about an hour ago this worked fine and now zilch!

Cheers for your help

Sam

&quot;You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine.&quot; - Homer
 
Hello again don't worry about the last post, seems like the computer was playing up, and wouldn't allow me to confirm the password, who knows why?? crazzzzy damn computers, anyways...

Cheers

Sam

&quot;You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine.&quot; - Homer
 
I guess that is one of the benifits of working with WinNT and WinXP Pro that the environment variables exist since it is meant to operate on a network. Now I know that does not exist for Win9x series of OS's.

Sorry it didnt help or work :(

Hopefully you can get it to work smoothly under WinME
 
Hey Solas,

Funnily enough since I've sorted this problem on ME i've been given a brand new spankin XP computer, and guess what XP doesn't like the code, go figure it's microsoft, seems that they have updated the MPR.dll file and they have removed the WNetVerifyPassword function, why? I don't know, but it means I have been coding an XP version all day, as other alterations have also been made as well. Now I have two versions I have to figure out how to detect the different versions, hopefully soon all comps will have XP on them, until then my job will continue with variations, especially as some computers still have version 1 of Windows 95 am I ever glad this is a temporary position!

Toodle Pip

Cheers for all your help

Sam

&quot;You couldn't fool your mother on the foolingest day of your life if you had an electrified fooling machine.&quot; - Homer
 
I found this code very helpful in identifying who is logging on, then adapating it to insert that information into a log file. However, I have a new problem:

Currently, there is a user logged somewhere in our company, and I can't track down who it is. Is there any way to adapt this code to list all users currently using a particular file?

Thanks,
PS
 
Check out

for a list of ways to help you.
Probably the most usefule is the AppUser file at
which will give you a list of everyone in a file.

Good luck

Ben

----------------------------------------------
Ben O'Hara &quot;Where are all the stupid people from...
...And how'd they get so dumb?&quot;
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top