Link to home
Start Free TrialLog in
Avatar of derek7467
derek7467

asked on

vb.net and querying ldap lastLogon

I am attempting to pull back lastLogon from LDAP, which returns system.com object or some junk like that.  I am using the below code to attempt to translate that code into date/time format, but im kinda of confused.

If you use the DirectoryEntry, it marshals the value as an ADSI 
IADsLargeInteger type, which at runtime is a System.__ComObject.  This is 
annoying, but you can get the value with a little interop and some data 
munging.  This works for me:

dim entry as new DirectoryEntry("LDAP://yourdn here")
dim pwd as object = entry.Properties("lastLogon").Value
dim pwdDate as DateTime
pwdDate = DateTime.FromFileTimeUtc(GetInt64FromLargeInteger(pwd))

Function GetInt64FromLargeInteger(byval largeInteger as Object) as Int64

  dim low as int32
  dim high as int32
  dim valBytes(7) as byte

  dim longInt as IADsLargeInteger = Ctype(largeInteger, IADsLargeInteger)
  low = longInt.LowPart
  high = longInt.HighPart

  BitConverter.GetBytes(low).CopyTo(valBytes, 0)
  BitConverter.GetBytes(high).CopyTo(valBytes, 4)

  Return BitConverter.ToInt64(valBytes, 0)

   End Function


        <ComImport(), Guid("9068270b-0939-11D1-8be1-00c04fd8d503"), 
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)> _
        public interface IADsLargeInteger
             property HighPart as int32
             property LowPart as int32
        end interface

Open in new window


Can someone tell me how to take the below and make it equal a textbox:
dim pwd as object = entry.Properties("lastLogon").Value

Like
pwd = textbox1.text???
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Surely you just need:
textbox1.text = pwd.ToString()

Open in new window

pwd here is DateTime so any of the extended string formatting options should work for you as well.

Chris
Avatar of derek7467
derek7467

ASKER

didnt work, still get the sys_comobject in the textbox
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry i shoulda realized that.  It worked!  You ROCK!