Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
20
Erster Beitrag:
vor 3 Jahren, 9 Monaten
Letzter Beitrag:
vor 3 Jahren, 9 Monaten
Beteiligte Autoren:
JP, DerekT, Alexandre Leclerc, Paulo Oliveira, Arie, Stewart Crisler

[WD19] - Finding the user login name and description

Startbeitrag von JP am 05.11.2014 15:35

Hi All

When a user logs into windows he has, obviously, a username. But when you create a user on Windows there are in fact 3 items for each login that are of interest to me.

1) The login name
2) The login "Full Name"
3) The login "Description"

The first one I can easily find in WinDev using NetworkUser(). How can I find out the other 2 items?

In Visual FoxPro I can do this:

1) I can create an object reference to some sort of path to the users folder like this:

TOBJ_Windows_User = getobject( "WinNT://" + Getenv("USERDOMAIN") + "/" + ALLTRIM( SYS_TSVC->USER_NAME ) + ",User" )

2) Then the Full Name is returned with:

TOBJ_Windows_User.FullName

3) And the Description is returned with:

TOBJ_Windows_User.Description

Is there a way to find this info in WinDev? Perhaps using a function or API ?

Many thanks.

Antworten:

Using the LDAPxxx functions it's possible, using some system API it should be possible but i don't know how.

von Paulo Oliveira - am 05.11.2014 16:08
Hi Paulo,

Thanks for the reply. I am looking at these LDAPxxx functions but I not sure I undestand how to use them. For example, in the help for LDAPConnect() I see this:

// Initialization
LDAPReset()
LDAPSession.Host = "domain.local"
LDAPSession.Port = 389
LDAPSession.Version = 3
LDAPSession.User = "user@domain.local"
LDAPSession.Password = "pwd"


But what must i enter for these parameters? I am not familiar with LDAP at all ...

von JP - am 05.11.2014 16:22
To understand the use of the LDAP function check the exemple provided with windev in Examples\Complete examples\WD LDAP Explorer

In the host put your domain server name and use your windows user/password.

von Paulo Oliveira - am 05.11.2014 16:33
LDAP is the generic equivalent of Active Directory. If you are looking for a local machine account and name, I doubt you will find it via LDAP.

GETOBJ() is basically doing a OLE automation thing behind the scenes. I would suggest exploring all the WinDev help associated with OLE. If you can get the right object instantiated, then you should be able to noodle out the information you want.

Not an answer but maybe a rock to look under. Good luck.

Stewart Crisler

von Stewart Crisler - am 05.11.2014 17:59
JP,
try this for the username

FUNCTION GetWindowsLoginName()
ppBuffer is string ASCIIZ on 256
pBytesReturned is int = 128
API("advapi32.dll", "GetUserNameA" , &ppBuffer,&pBytesReturned)
RESULT ppBuffer

von Arie - am 05.11.2014 18:26
I also have this one. Not eactly what you need, but maybe you can learn from it


FUNCTION GetWindowsComputerName(bIncludeDomain is boolean = False)

ppBuffer is string ASCIIZ on 256
pBytesReturned is int = 128

API("kernel32.dll", "GetComputerNameA" , &ppBuffer,&pBytesReturned)
sResult is string = ppBuffer
IF bIncludeDomain THEN
sDomain is string = SysEnvironment("USERDNSDOMAIN")
IF sDomain "" THEN
sResult += "." + ExtractString(sDomain,lastRank,"=")
END
END
RESULT sResult


von Arie - am 05.11.2014 18:52
JP

Some snippets from the code I use which handles users from multiple offices (OU).
Basically a personalised copy for what I required based on the WD LDAP example project.
// Fill the connection structure
LDAPSession.Host = gclApplication:msLDAPHost
LDAPSession.User = gclApplication:msLDAPUser
LDAPSession.Password = gclApplication:msLDAPPW
// Connect to the LDAP server
lbConnRes = LDAPConnect("MySession")


//Build and execute the query
LBQueryRes = HExecuteSQLQuery(LDQueryData,hQueryDefault,LSQueryText)
IF LBQueryRes = True THEN
lsLDAPSession = "OU=Users,OU=%1,DC=%2,DC=local"
FOR EACH LDQueryData
//Set country (ISO2) code and company for the office
lsOUCountryCode = NoSpace(LDQueryData.ISO2Code)
lsOUCompanyCode = NoSpace(LDQueryData.Company)
//Replace OU country and company identifiers
lsLDAPSession = StringBuild(lsLDAPSession,lsOUCountryCode,lsOUCompanyCode)
//Return all LDAP records for the specified country/company combination
lsChildrenList = LDAPListChildren("MySession",lsLDAPSession)
IF lsChildrenList "" THEN
//Process if LDAP records are returned
FOR EACH STRING sChildVal OF lsChildrenList SEPARATED BY CR
//Set variables with the returned values
lsUserName = LDAPValue("MySession",sChildVal,"name")
lsNetworklogin = LDAPValue("MySession",sChildVal,"sAMAccountName")
lsEmailAddr = LDAPValue("MySession",sChildVal,"mail")

//Do stuff here

END
END
END


May give you some ideas, works very well for me

von DerekT - am 05.11.2014 20:04
HI Arie,

Thanks but that function returns the same as the built-in WD function NetworkUser() which returns just the login name only.

Thanks.

von JP - am 06.11.2014 07:56
Thanks again Arie but that function is also the same as the built-in WD function NetworkDomainName()

Thanks though :)

von JP - am 06.11.2014 07:57
Hi Derek,

I am trying out this LDAP code but I cant seem to even create a succesful connection. Here is what I am trying;

Running on Windows 8.1

// Setup LDAP connection name
sLDAP_Connection_Name is string = "MySession"

// Setup connection variables
sHost_Name is string = "JP-Laptop" // I am not sure what this should be? Is it NetworkDomainName(netDomainDNS) ?
sUser_Name is string = "JP" // This is my login username
sUser_Pwrd is string = "****" // This is my login password


// Fill the connection structure
LDAPReset()
LDAPSession.Host = sHost_Name
LDAPSession.User = sUser_Name
LDAPSession.Password = sUser_Pwrd


// Connecting to the LDAP server
IF LDAPConnect( sLDAP_Connection_Name ) = True THEN
Trace( "connected ok" )
LDAPDisconnect( sLDAP_Connection_Name )
ELSE
Trace( "connection failed" )
END


The connection fails. ErrorInfo() Returns:

LDAP Error - System Error Details: Server Down

I am not sure what I am doing wrong here. I get the server down message regardless of what I try for host name....

Any suggestions?

Thanks

von JP - am 06.11.2014 09:06
JP

'sHost_Name is string = "JP-Laptop"'
'sUser_Name is string = "JP"'
'sUser_Pwrd is string = "****"'

This should be the ip address of the server + an admin pw and username for the server concerned.

Have a look at 'WD LDAP Explorer' in the Complete examples section.

von DerekT - am 06.11.2014 09:21
Hi Derek,

I have the WD LDAP Explorer example up but still cannot connect. The reason is I dont know what to use for host name.

Q) Should this example work on my own laptop or must I run this on a Windows server?

Q) What is the host name when running from a laptop? "localhost", 127.0.0.1 dont work ...

von JP - am 06.11.2014 09:32
JP

I am not at my best when it comes to this systems stuff - just a user I am afraid.....
I wonder if you are barking up the wrong tree?

Have a look on Google for an explanation.

http://www.gracion.com/server/whatldap.html or similar will enlighten you.

LDAP, Lightweight Directory Access Protocol, is an Internet protocol that email and other programs use to look up information from a server.

von DerekT - am 06.11.2014 09:39
Hi JP,

For a simple way to get the user name you can try GetUserName and GetUserNameEx API:
http://msdn.microsoft.com/en-us/library/ms724432(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms724435(v=vs.85).aspx

LDAP is not super easy to implement and is usually managed on server side. But it is a solution for you if your final program will run on a LDAP enabled server. Many installations do not use LDAP, but usually only big companies or smaller ones with consultants that implemented it.

One solution I often turn to is to find an example in .Net and implement it in WinDev. Sometimes I have success (last time I tried I was faced to a lack of support for WMI objects.)

So for your case you could try to use and experiment with .Net framework using System.DirectoryServices.AccountManagement in your project:

Here is a .Net example I found: http://stackoverflow.com/questions/3471635/get-first-name-last-name-of-logedin-windows-user


UserPrincipal userPrincipal = UserPrincipal.Current;
String name = userPrincipal.DisplayName;


In WinDev this looks like this:


// Add to your project : System.DirectoryServices.AccountManagement in you project
up is UserPrincipal dynamic = UserPrincipal.get_Current()
Trace(up.Name)
Trace(up.DisplayName)
Trace(up.Description)
Trace(up.SamAccountName)
Trace(up.LastLogon)


There are many other things and properties you can do with the account management class: http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement(v=vs.110).aspx

Maybe you will be able to get all the information you desire using this .Net class. Hoping this can help you.

Best regards,
Alexandre Leclerc

Edit 1: Added some trace examples.

von Alexandre Leclerc - am 06.11.2014 13:43
Hi JP,

Just to add that if you really want to work with active directory, there is a .Net example of that in WinDev called: WD Active Directory DotNet.

Best regards,
Alexandre Leclerc

von Alexandre Leclerc - am 06.11.2014 14:02

Re: [WD19] - Finding the user login name and description - SOLUTION

Hi All,

I have found a solution which is a little bit of a hack but when encapsulated in a neat little function it works very easily. Basically the idea is to call "NET USER" in a hidden command shell and get all the user's Windows login details into a text file and then into a string variable for processing. This works on any local machine or servers. The steps are:

1) Get the user's login name
2) Run NET USER in a batch file with the user's login name and output the contents into a temporary text file
3) Read in the text file and extract the information required
4) Remove the temporary batch file and the temporary file

The whole thing runs in under a second so is very quick. Here is the code which you can turn into a function if you like:


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

// Setup variable to hold user's login account details
sUser_Login_Info is string = ""

// Get the user login name
sUser_Login_Name is string = NetworkUser()

// Setup variables to hold the batch file to run and the output file
sTemporary_Batch_File is string = fTempFile() + ".bat" // Add a "bat" extension to make it a batch file
sTemporary_Output_File is string = fTempFile()

// Setup variable to hold command to execute to retrieve user's login account details
sCommand_To_Execute is string = "net user " + sUser_Login_Name + " >" + sTemporary_Output_File

// Save the command to execute into the batch file
fSaveText( sTemporary_Batch_File , sCommand_To_Execute )

// Execute the batch file - run iconoized to hide window output and wait to make sure it completes
ExeRun( sTemporary_Batch_File , exeIconize , exeWait )

// Read in the output file created above
sUser_Login_Info = fLoadText( sTemporary_Output_File )

// Delete the batch file and the output text file
fDelete( sTemporary_Batch_File )
fDelete( sTemporary_Output_File )

// Process the user's login file and extract the data required
// sUser_Login_Info holds the data required

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

A typical Windows user login file created as above looks like the below and you can see the 2nd and 3rd lines provide the user login full name and description as I require. The lines are made up of 29 characters for the field description and then the actual field value starting at position 30.

// A typical Windows login account details looks like this (not with WD comments of course):
//
// User name JP
// Full Name JP - Code Developer
// Comment XYZ - Administrator
// User's comment
// Country/region code 000 (System Default)
// Account active Yes
// Account expires Never
//
// Password last set 23/02/2014 17:01:06
// Password expires Never
// Password changeable 23/02/2014 17:01:06
// Password required No
// User may change password Yes
//
// Workstations allowed All
// Logon script
// User profile
// Home directory
// Last logon 06/11/2014 12:04:52
//
// Logon hours allowed All
//
// Local Group Memberships *Administrators
// Global Group memberships *None
//


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

von JP - am 06.11.2014 14:15

Re: [WD19] - Finding the user login name and description - SOLUTION

Be carefull if the logon user is one domain user, in this case you have to add /DOMAIN to the net user command.

von Paulo Oliveira - am 06.11.2014 14:23

Re: [WD19] - Finding the user login name and description - SOLUTION

Hi JP,

To add to Paulo, if the local PC settings (language, date formatting, etc.) changes, you will not be able to interpret the results. If Windows version changes and the output of NET USER changes you will have to rewrite your code and support many versions.

You will be better served if you can access the original information. By the way, the .Net example I sent you is a copy-and-paste example and will work out of the box. You can get the exact same information and you can even change the information if you like (having the appropriate rights). The MSDN page explains in details what you can do.

Best regards,
Alexandre Leclerc

von Alexandre Leclerc - am 06.11.2014 15:40

Re: [WD19] - Finding the user login name and description - SOLUTION

Thanks Alexandre. I will look into that as well. This solution works for the moment and we only need it on our own servers running Terminal Services so that we can record which users are logging into the server. This is the sole purpose for this work. Thanks for the further info :)

von JP - am 06.11.2014 16:02
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.