By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,781 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,781 IT Pros & Developers. It's quick & easy.

Active Directory 'lastLogon' time conversion...

P: n/a
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(res ults.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).ToLongDateS tring());
}
}
}
}
Apr 4 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
| 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(res ults.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).ToLongDateS tring());
| }
| }
| }
| }
Apr 4 '06 #2

P: n/a
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 [MVP]" wrote:
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
| 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(res ults.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).ToLongDateS tring());
| }
| }
| }
| }

Apr 4 '06 #3

P: n/a
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
| 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(res ults.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).ToLongDateS tring());
| }
| }
| }
| }
Apr 4 '06 #4

P: n/a
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" wrote:
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 [MVP]" wrote:
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
| 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(res ults.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).ToLongDateS tring());
| }
| }
| }
| }

Apr 4 '06 #5

P: n/a
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
| 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(res ults.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).ToLongDateS tring());
| }
| }
| }
| }

Apr 4 '06 #6

P: n/a
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.
"Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
| 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.
| >
| >
| > "Robert Vasquez" <Ro***********@discussions.microsoft.com> wrote in
message
| > news:D3**********************************@microsof t.com...
| > | 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(res ults.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).ToLongDateS tring());
| > | }
| > | }
| > | }
| > | }
| >
| >
| >
Apr 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.