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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check if the user has logged into the network

Status
Not open for further replies.

R17

Programmer
Jan 20, 2003
267
PH


i know how to map a drive to the network. but i'd like to know if my user has logged on to the network. and if not, i should be able to give him a message that he needs to log on to the network first.

when i try to connect a drive to the network without a user logged in, i get an error drive not found.

what should i do?
 
R17

First, you must know who is the current user in for that particular workstation. In other words, the user must logged into windows. Also the username for workstation must be the same with username on the network.

If not then you have to hardcode the username and network username, or you can figure out yourself :)

Here is the code if the user is logged, and the username on the net is the same.

* ----------------------
Declare Long WNetGetUser in MPR ;
String cUserName, String @cReturnName, Long @nLen

nLen = 80
cBuffer = replicate(chr(0), nLen)
nError = WNetGetUser(Null, @cBuffer, @nLen) && Get current user
If (nError == 0)
cUser = left(cBuffer, at(chr(0), cBuffer)-1)
cBuffer = replicate(chr(0), nLen)
** Get the network username for current user
nError = WNetGetUser(cUser, @cBuffer, @nLen)
If (nError == 0)
? 'Net username: ' + left(cBuffer, nLen-1)
else
? 'Error: ', nError
If (nError == 1245)
? 'User is not connected to network'
endif
endif
else
? 'Error: ', nError
** ? 'User is not logged on in this computer'
endif
* -------------------

Hope it helps

-- AirCon --
 
I suggest that you trap the error, like this:

oNet = CREATEOBJECT("wscript.network")
ON ERROR bError=.T.
oNet.MapNetworkDrive("H:","\\Server\date",.t.,"user","pass")
if bError
messagebox('An error occurred.'+chr(13)+' If you are not logged to the network, please do')
endif
ON ERROR DO MyErrorHandler (or ON ERROR)

Another method (should work but is not working every time, and I'm not sure it works on Win9x)
oUser = GetObject("WinNT://domain/username")
if oUser.LastLogin-oUser.LastLogoff<0
messagebox('Please log in!')
endif
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top