Active Directory 'lastLogon' time conversion...

G

Guest

I'm trying to convert the value of 'lastLogon' to a DateTime value by using
the code listed below, but the code doesn't seem to be working for it throws
an exception: 'The object's type must be__ComObject or derived from
__ComObject...' right at the CreateWrapperOfType() function. Any ideas as to
what I'm doing wrong?

using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using ActiveDs;

entry = new DirectoryEntry("LDAP://MyDomain.com/dc=MyDomain, dc=com");
dSearch = new DirectorySearcher(entry);
dSearch.Filter = "(objectClass=computer)";
foreach(SearchResult results in dSearch.FindAll())
{
foreach(string Key in results.Properties.PropertyNames)
{
if(results.Properties[Key]!=null)
{
if(Key.ToUpper()=="LASTLOGON")
{
LargeIntegerClass li;
li =
(LargeIntegerClass)Marshal.CreateWrapperOfType(results.Properties["lastLogon"][0], typeof(LargeIntegerClass));
long int64Value = (long)((uint)li.LowPart + (((long)li.HighPart) << 32 ));
Marshal.ReleaseComObject(li);
this.listBox1.Items.Add("Last logged on at:
"+DateTime.FromFileTimeUtc(int64Value).ToLongDateString());
}
}
}
}
 
W

Willy Denoyette [MVP]

if(Key.ToUpper...
LargeInteger li = results.Properties["lastlogon"].Value as LargeInteger;
long expDate = (((long)(li.HighPart) << 32) + (long) li.LowPart);
string dt = DateTime.FromFileTime(date).ToString();
....

Willy.


| I'm trying to convert the value of 'lastLogon' to a DateTime value by
using
| the code listed below, but the code doesn't seem to be working for it
throws
| an exception: 'The object's type must be__ComObject or derived from
| __ComObject...' right at the CreateWrapperOfType() function. Any ideas as
to
| what I'm doing wrong?
|
| using System;
| using System.DirectoryServices;
| using System.Runtime.InteropServices;
| using ActiveDs;
|
| entry = new DirectoryEntry("LDAP://MyDomain.com/dc=MyDomain, dc=com");
| dSearch = new DirectorySearcher(entry);
| dSearch.Filter = "(objectClass=computer)";
| foreach(SearchResult results in dSearch.FindAll())
| {
| foreach(string Key in results.Properties.PropertyNames)
| {
| if(results.Properties[Key]!=null)
| {
| if(Key.ToUpper()=="LASTLOGON")
| {
| LargeIntegerClass li;
| li =
|
(LargeIntegerClass)Marshal.CreateWrapperOfType(results.Properties["lastLogon"][0],
typeof(LargeIntegerClass));
| long int64Value = (long)((uint)li.LowPart + (((long)li.HighPart) << 32 ));
| Marshal.ReleaseComObject(li);
| this.listBox1.Items.Add("Last logged on at:
| "+DateTime.FromFileTimeUtc(int64Value).ToLongDateString());
| }
| }
| }
| }
 
G

Guest

If I write the following:

LargeInteger li = (LargeInteger)results.Properties["lastLogon"];

I get an 'Specified Cast Is Not Valid' error.

If I write the following:

LargeInteger li = results.Properties["lastLogon"] as LargeInteger;

I get an 'Object referenced not set to an instance of an object.' error.

Note that 'Value' is not a property of results.Properties["lastLogon"].
 
W

Willy Denoyette [MVP]

I see, you are using a SearchResultsCollection, here the property value is
already converted to a long (from a LARGEINTERGER).

Try this:
...
long lastLogon = (long)results.Properties[Key][0]
string dt = DateTime.FromFileTime(lastLogon ).ToString();
...

Willy.


| I'm trying to convert the value of 'lastLogon' to a DateTime value by
using
| the code listed below, but the code doesn't seem to be working for it
throws
| an exception: 'The object's type must be__ComObject or derived from
| __ComObject...' right at the CreateWrapperOfType() function. Any ideas as
to
| what I'm doing wrong?
|
| using System;
| using System.DirectoryServices;
| using System.Runtime.InteropServices;
| using ActiveDs;
|
| entry = new DirectoryEntry("LDAP://MyDomain.com/dc=MyDomain, dc=com");
| dSearch = new DirectorySearcher(entry);
| dSearch.Filter = "(objectClass=computer)";
| foreach(SearchResult results in dSearch.FindAll())
| {
| foreach(string Key in results.Properties.PropertyNames)
| {
| if(results.Properties[Key]!=null)
| {
| if(Key.ToUpper()=="LASTLOGON")
| {
| LargeIntegerClass li;
| li =
|
(LargeIntegerClass)Marshal.CreateWrapperOfType(results.Properties["lastLogon"][0],
typeof(LargeIntegerClass));
| long int64Value = (long)((uint)li.LowPart + (((long)li.HighPart) << 32 ));
| Marshal.ReleaseComObject(li);
| this.listBox1.Items.Add("Last logged on at:
| "+DateTime.FromFileTimeUtc(int64Value).ToLongDateString());
| }
| }
| }
| }
 
G

Guest

Actually the error I'm getting:

'Object referenced not set to an instance of an object.'

is when I try to execute the following:

long int64Value = (((long)(li.HighPart) << 32) + (long) li.LowPart);

Robert Vasquez said:
If I write the following:

LargeInteger li = (LargeInteger)results.Properties["lastLogon"];

I get an 'Specified Cast Is Not Valid' error.

If I write the following:

LargeInteger li = results.Properties["lastLogon"] as LargeInteger;

I get an 'Object referenced not set to an instance of an object.' error.

Note that 'Value' is not a property of results.Properties["lastLogon"].

Willy Denoyette said:
if(Key.ToUpper...
LargeInteger li = results.Properties["lastlogon"].Value as LargeInteger;
long expDate = (((long)(li.HighPart) << 32) + (long) li.LowPart);
string dt = DateTime.FromFileTime(date).ToString();
....

Willy.


| I'm trying to convert the value of 'lastLogon' to a DateTime value by
using
| the code listed below, but the code doesn't seem to be working for it
throws
| an exception: 'The object's type must be__ComObject or derived from
| __ComObject...' right at the CreateWrapperOfType() function. Any ideas as
to
| what I'm doing wrong?
|
| using System;
| using System.DirectoryServices;
| using System.Runtime.InteropServices;
| using ActiveDs;
|
| entry = new DirectoryEntry("LDAP://MyDomain.com/dc=MyDomain, dc=com");
| dSearch = new DirectorySearcher(entry);
| dSearch.Filter = "(objectClass=computer)";
| foreach(SearchResult results in dSearch.FindAll())
| {
| foreach(string Key in results.Properties.PropertyNames)
| {
| if(results.Properties[Key]!=null)
| {
| if(Key.ToUpper()=="LASTLOGON")
| {
| LargeIntegerClass li;
| li =
|
(LargeIntegerClass)Marshal.CreateWrapperOfType(results.Properties["lastLogon"][0],
typeof(LargeIntegerClass));
| long int64Value = (long)((uint)li.LowPart + (((long)li.HighPart) << 32 ));
| Marshal.ReleaseComObject(li);
| this.listBox1.Items.Add("Last logged on at:
| "+DateTime.FromFileTimeUtc(int64Value).ToLongDateString());
| }
| }
| }
| }
 
W

Willy Denoyette [MVP]

Yes it is, that's because the DirectorySearcher internally fetches the
LARGEINTEGER value (COM property) and stores it in a long. If you are
dealing directly with a DirectoryEntry, you need to convert this
LARGEINTEGER property to a long as in my first sample.


Willy.


| Thanks Willy, that worked and is a lot more simpler than I thought it was.
|
| "Willy Denoyette [MVP]" wrote:
|
| > I see, you are using a SearchResultsCollection, here the property value
is
| > already converted to a long (from a LARGEINTERGER).
| >
| > Try this:
| > ...
| > long lastLogon = (long)results.Properties[Key][0]
| > string dt = DateTime.FromFileTime(lastLogon ).ToString();
| > ...
| >
| > Willy.
| >
| >
message
| > | > | I'm trying to convert the value of 'lastLogon' to a DateTime value by
| > using
| > | the code listed below, but the code doesn't seem to be working for it
| > throws
| > | an exception: 'The object's type must be__ComObject or derived from
| > | __ComObject...' right at the CreateWrapperOfType() function. Any
ideas as
| > to
| > | what I'm doing wrong?
| > |
| > | using System;
| > | using System.DirectoryServices;
| > | using System.Runtime.InteropServices;
| > | using ActiveDs;
| > |
| > | entry = new DirectoryEntry("LDAP://MyDomain.com/dc=MyDomain, dc=com");
| > | dSearch = new DirectorySearcher(entry);
| > | dSearch.Filter = "(objectClass=computer)";
| > | foreach(SearchResult results in dSearch.FindAll())
| > | {
| > | foreach(string Key in results.Properties.PropertyNames)
| > | {
| > | if(results.Properties[Key]!=null)
| > | {
| > | if(Key.ToUpper()=="LASTLOGON")
| > | {
| > | LargeIntegerClass li;
| > | li =
| > |
| >
(LargeIntegerClass)Marshal.CreateWrapperOfType(results.Properties["lastLogon"][0],
| > typeof(LargeIntegerClass));
| > | long int64Value = (long)((uint)li.LowPart + (((long)li.HighPart) <<
32 ));
| > | Marshal.ReleaseComObject(li);
| > | this.listBox1.Items.Add("Last logged on at:
| > | "+DateTime.FromFileTimeUtc(int64Value).ToLongDateString());
| > | }
| > | }
| > | }
| > | }
| >
| >
| >
 
Top