Answered Cant convert objectGuid to string

a5m0d1

Member
Joined
Mar 14, 2010
Messages
15
Programming Experience
1-3
Ive tried and tried to fix this issue.
I cant get the objectGuid from a computer via Active Directory.
I cant work out the converstion to a string

I have added some code below to show where I am going wrong
It works fine write up until it tries to update the tree view with the "objectGuid"

Error = Conversion from type 'Byte()' to tyoe 'integer' is not valid

VB.NET:
'------------------------Computers-----------------------
        Using searcher As New System.DirectoryServices.DirectorySearcher

            searcher.SearchRoot = New System.DirectoryServices.DirectoryEntry("LDAP://DC=wigan,DC=org,DC=uk")
            searcher.Filter = "(&(objectCategory=computer) (name=*))"

            searcher.PropertiesToLoad.Add("name")
          searcher.PropertiesToLoad.Add("operatingSystemServicePack")
            searcher.PropertiesToLoad.Add("operatingSystem")
            searcher.PropertiesToLoad.Add("logonCount")
            searcher.PropertiesToLoad.Add("distinguishedName")
            searcher.PropertiesToLoad.Add("description")
searcher.PropertiesToLoad.Add("objectGUID")
            
            searcher.Sort = New System.DirectoryServices.SortOption("name", System.DirectoryServices.SortDirection.Ascending)
            searcher.SearchScope = DirectoryServices.SearchScope.Subtree
            searcher.SizeLimit = 0
            searcher.PageSize = 250

            results = searcher.FindAll
            If results IsNot Nothing Then

                For Each result As System.DirectoryServices.SearchResult In results

                    'get the actual directory
                    Dim dir As System.DirectoryServices.DirectoryEntry = result.GetDirectoryEntry

                    'within the organizational unit there are child entries
                    For Each childDirectory As System.DirectoryServices.DirectoryEntry In dir.Children


                    Next

                    TreeView1.Nodes(0).Nodes.Add(New TreeNode(dir.Properties.Item("name").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).ImageIndex = 1
                    TreeView1.Nodes(0).Nodes(nodecount).SelectedImageIndex = 1
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("operatingSystemServicePack").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("operatingSystem").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("distinguishedName").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("logonCount").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("description").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes.Add(New TreeNode(dir.Properties.Item("objectGUID").Value))
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(0).ImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(0).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(1).ImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(1).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(2).ImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(2).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(3).ImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(3).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(4).ImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(4).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(4).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(5).SelectedImageIndex = 2
                    TreeView1.Nodes(0).Nodes(nodecount).Nodes(5).SelectedImageIndex = 2
                    

                    TreeView1.Nodes(0).Expand()


                    nodecount = nodecount + 1


                Next

            End If
        End Using
 
Last edited:
Last Logon is a bit tricky as it's stored as an IADsLargeInteger with the Date being stored in the HighPart and the Time being stored in the LowPart.

The first thing you'll need to do is add a reference to Active DS Type Library (COM tab).

From there you need to convert it to IADsLargeInteger and then to DateTime.

VB.NET:
                    If dir.Properties("lastLogon").Value IsNot Nothing Then
                        Dim lgInt As ActiveDs.IADsLargeInteger = DirectCast(dir.Properties("lastLogon").Value, ActiveDs.IADsLargeInteger)
                        Dim lastLogon As DateTime = DateTime.FromFileTime((CLng(lgInt.HighPart) << 32) + lgInt.LowPart)
                    End If

Edit: Request for a mod to split this to another thread to help others find it in the future.
 
Last edited:
Thank you so much, im still a nube at this programing lark but for u to pick that out of the air u must know ur stuff

Thanks again!
 
Back
Top