InvalidComObjectException when searching in AD

  • Thread starter Thread starter MP
  • Start date Start date
M

MP

Hello,

I am writting an application that requires to list all users in our domain,
the code is below. My application is written in C# 2.0. I enumerate all
users fine with my code, except that when I exit the application normally I
get a popup from MS-DEV saying "There is no source code available for the
current location."

After I click ok I get "InvalidComObjectException was unhandled", "COM
object that has been separated from its underlying RCW can not be used".

Here is my code. I have a similar behavior when I try to enumerate the
domains on our network.


Can someone help me on this one? I have no idea where to look.

Thank you!
Martin


private bool GetDomainUsers()
{
try
{
// Start from the root of the active active directory
DirectoryEntry domainRoot = new DirectoryEntry(LDAP://MyDomain);
DirectorySearcher domainSearch = new DirectorySearcher(domainRoot);
// Set the search filter.
domainSearch.Filter =
("(&(objectCategory=person)(objectClass=user))");
domainSearch.PageSize = 100;
// Perform the search
SearchResultCollection usersFound = domainSearch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult searchResult in usersFound)
{
lstUsers.Items.Add(searchResult.Properties["sAMAccountName"][0].ToString());
}
}
catch
{
}
return true;
}
 
Just out of curiosity, have you tried this with .NET 1.1? There could be a
bug or something in 2.0 (which is still beta). It isn't obvious to me what
the problem is.

It would be helpful to see what the stack trace of the exception is
(ex.ToString)

Joe K.
 
I do the same with vb.
but with 1.1, anyway
this looks ok, dunno what the problem is.
but would appreciate it if you tell me how you enumerate domains on your
network?
thanks and good luck
Sameh
 
Joe,
Thank you for your advise. I have ran a test with a smiple console
application and I had the very same problem. SO.... I decied to try a few
things and turns out that I have to call .Dispose on the
SearchResultCollection.

Thank you!

-Martin



private bool GetDomainUsers()
{
try
{
// Start from the root of the active active directory
DirectoryEntry domainRoot = new DirectoryEntry(LDAP://MyDomain);
DirectorySearcher domainSearch = new DirectorySearcher(domainRoot);
// Set the search filter.
domainSearch.Filter =
("(&(objectCategory=person)(objectClass=user))");
domainSearch.PageSize = 100;
// Perform the search
SearchResultCollection usersFound = domainSearch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult searchResult in usersFound)
{
lstUsers.Items.Add(searchResult.Properties["sAMAccountName"][0].ToString());
}
usersFound.Dispose;
}
catch
{
}
return true;
}


Joe Kaplan (MVP - ADSI) said:
Just out of curiosity, have you tried this with .NET 1.1? There could be
a bug or something in 2.0 (which is still beta). It isn't obvious to me
what the problem is.

It would be helpful to see what the stack trace of the exception is
(ex.ToString)

Joe K.

MP said:
Hello,

I am writting an application that requires to list all users in our
domain, the code is below. My application is written in C# 2.0. I
enumerate all users fine with my code, except that when I exit the
application normally I get a popup from MS-DEV saying "There is no source
code available for the current location."

After I click ok I get "InvalidComObjectException was unhandled", "COM
object that has been separated from its underlying RCW can not be used".

Here is my code. I have a similar behavior when I try to enumerate the
domains on our network.


Can someone help me on this one? I have no idea where to look.

Thank you!
Martin


private bool GetDomainUsers()
{
try
{
// Start from the root of the active active directory
DirectoryEntry domainRoot = new DirectoryEntry(LDAP://MyDomain);
DirectorySearcher domainSearch = new
DirectorySearcher(domainRoot);
// Set the search filter.
domainSearch.Filter =
("(&(objectCategory=person)(objectClass=user))");
domainSearch.PageSize = 100;
// Perform the search
SearchResultCollection usersFound = domainSearch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult searchResult in usersFound)
{

lstUsers.Items.Add(searchResult.Properties["sAMAccountName"][0].ToString());
}
}
catch
{
}
return true;
}
 
Back
Top