Home All Groups Group Topic Archive Search About

InvalidComObjectException when searching in AD

Author
12 Jan 2005 4:31 PM
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;
}

Author
12 Jan 2005 5:52 PM
Joe Kaplan (MVP - ADSI)
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.

Show quote
"MP" <martin.pare@I_Will_Not_Give_You_My_Address> wrote in message
news:uELPtQM%23EHA.936@TK2MSFTNGP12.phx.gbl...
> 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;
> }
>
>
Author
17 Jan 2005 2:19 PM
MP
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;
}


Show quote
"Joe Kaplan (MVP - ADSI)" <joseph.e.kap***@removethis.accenture.com> wrote
in message news:Oa6xp9M%23EHA.2788@TK2MSFTNGP15.phx.gbl...
> 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" <martin.pare@I_Will_Not_Give_You_My_Address> wrote in message
> news:uELPtQM%23EHA.936@TK2MSFTNGP12.phx.gbl...
>> 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;
>> }
>>
>>
>
>
Author
14 Jan 2005 5:05 PM
Sameh Ahmed
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
Show quote
"MP" <martin.pare@I_Will_Not_Give_You_My_Address> wrote in message
news:uELPtQM%23EHA.936@TK2MSFTNGP12.phx.gbl...
> 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;
> }
>
>

AddThis Social Bookmark Button