OnError_Fix
asked on
System.InvalidCastException: Cast from type '_ComObject' to type 'String' is not valid
Hi,
I'm using a function to retrieve properties from a DirectoryServices object.
Unfortunately, the function fails whenever I trry to assign the object properties "accountExpires" to a string value with the error: System.InvalidCastExceptio n: Cast from type '_ComObject' to type 'String' is not valid.
The function I am using is as follows:
Public Function GetObjectProperties(ByVal DN As String, ByVal PropertyName As String) As String
Dim strOutput As String
Dim DE As New DirectoryEntry
DE.Path = "LDAP://" & ServerName & "/" & DN
DE.Username = AdminUsername
DE.Password = AdminPassword
Try
DE.RefreshCache()
If DE.SchemaClassName = "user" Then
strOutput = CStr(DE.Properties(Propert yName).Val ue)
End If
Catch ex As Exception
'TODO: Raise an error
strOutput = "Exception! Input DN was = " & DN & "<BR>The error was: " & ex.ToString
End Try
Return strOutput
DE.Dispose()
End Function
I know this function works, as it WILL retreive other properties, such as 'CN'. But it dies on trying to retrieve the expiration date.
Cheers
I'm using a function to retrieve properties from a DirectoryServices object.
Unfortunately, the function fails whenever I trry to assign the object properties "accountExpires" to a string value with the error: System.InvalidCastExceptio
The function I am using is as follows:
Public Function GetObjectProperties(ByVal DN As String, ByVal PropertyName As String) As String
Dim strOutput As String
Dim DE As New DirectoryEntry
DE.Path = "LDAP://" & ServerName & "/" & DN
DE.Username = AdminUsername
DE.Password = AdminPassword
Try
DE.RefreshCache()
If DE.SchemaClassName = "user" Then
strOutput = CStr(DE.Properties(Propert
End If
Catch ex As Exception
'TODO: Raise an error
strOutput = "Exception! Input DN was = " & DN & "<BR>The error was: " & ex.ToString
End Try
Return strOutput
DE.Dispose()
End Function
I know this function works, as it WILL retreive other properties, such as 'CN'. But it dies on trying to retrieve the expiration date.
Cheers
ASKER
Nope, that didn't work either:
System.InvalidCastExceptio n: Cast from type '_ComObject' to type 'Date' is not valid.
System.InvalidCastExceptio
ASKER
Note: here's the latest function:
E.Path = "LDAP://" & ServerName & "/" & DN
DE.Username = AdminUsername
DE.Password = AdminPassword
Try
DE.RefreshCache()
If DE.SchemaClassName = "user" Then
Return DE.Properties(PropertyName ).Value
End If
Catch ex As Exception
'TODO: Raise an error
Return "Exception! Input DN was = " & DN & "<BR>The error was: " & ex.ToString
End Try
E.Path = "LDAP://" & ServerName & "/" & DN
DE.Username = AdminUsername
DE.Password = AdminPassword
Try
DE.RefreshCache()
If DE.SchemaClassName = "user" Then
Return DE.Properties(PropertyName
End If
Catch ex As Exception
'TODO: Raise an error
Return "Exception! Input DN was = " & DN & "<BR>The error was: " & ex.ToString
End Try
Hello again,
When you get the accountExpires attribute value from AD direct binding, it returns as COM interface IADsLargeInteger which is not very "friendly" to any CLR data type. Therefore you can't cast it directly to String or DateTime. One article on IADsLargeInteger interface.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iadslargeinteger.asp
In VB.NET (or C#) you have to use COM interop or reflection. One way is to get the HighPart and LowPart values of the interface and combine them into a long integer.
Dim int64Val as IADsLargeInteger = DirectCast( thePropertyValue, IADsLargeInteger )
Dim longInteger As Int64 = Convert.ToInt64( int64Val.HighPart ) * 4294967296 + Convert.ToInt64( int64Val.LowPart )
Finally to convert it to DateTime value,
Dim accountExpires As DateTime = DateTime.FromFileTime( longInteger )
When you get the accountExpires attribute value from AD direct binding, it returns as COM interface IADsLargeInteger which is not very "friendly" to any CLR data type. Therefore you can't cast it directly to String or DateTime. One article on IADsLargeInteger interface.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iadslargeinteger.asp
In VB.NET (or C#) you have to use COM interop or reflection. One way is to get the HighPart and LowPart values of the interface and combine them into a long integer.
Dim int64Val as IADsLargeInteger = DirectCast( thePropertyValue, IADsLargeInteger )
Dim longInteger As Int64 = Convert.ToInt64( int64Val.HighPart ) * 4294967296 + Convert.ToInt64( int64Val.LowPart )
Finally to convert it to DateTime value,
Dim accountExpires As DateTime = DateTime.FromFileTime( longInteger )
ASKER
Hi iHenry,
As always I appreciate your help. The problem this time was that I got an error, "The input string was in an invalid format" when executing your code. It didn't like the following line: Dim longInteger As Int64 = Convert.ToInt64( int64Val.HighPart ) * 4294967296 + Convert.ToInt64( int64Val.LowPart ) where it reported an 'overflow'.
I tried Microsoft's code:
Dim ExpiresOn As String = CStr(Hex(int64val.HighPart )) + CStr(Hex(int64val.LowPart) )
Which produced a string of the following:
7FFFFFFFFFFFFFFF
No matter which user it was retrieving the property for. So I'm confused!
Any ideas?
As always I appreciate your help. The problem this time was that I got an error, "The input string was in an invalid format" when executing your code. It didn't like the following line: Dim longInteger As Int64 = Convert.ToInt64( int64Val.HighPart ) * 4294967296 + Convert.ToInt64( int64Val.LowPart ) where it reported an 'overflow'.
I tried Microsoft's code:
Dim ExpiresOn As String = CStr(Hex(int64val.HighPart
Which produced a string of the following:
7FFFFFFFFFFFFFFF
No matter which user it was retrieving the property for. So I'm confused!
Any ideas?
I have the similar code in C#, I think I screwed up VB.NET type conversion. Try get rid the conversion and see how it goes.
Dim longInteger As Int64 = int64Val.HighPart * 4294967296 + int64Val.LowPart
Hello OnError_Fix,
Hows the problem, solved?
ASKER
Hi iHenry.... sorry for the delay in replying (work!!).
No, unfortunately the problem is still the same. Any ideas on how to proceed now?
No, unfortunately the problem is still the same. Any ideas on how to proceed now?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice one, iHenry....
Apologies for the delay in getting these to you. Thanks again!
Apologies for the delay in getting these to you. Thanks again!
Racin