Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - .Net Directory Classes give COM errors

Reply
 
Old 12-11-2008   #1 (permalink)
Waldy


 
 

.Net Directory Classes give COM errors

Hi there,
does anyone have any idea why I cannot use the .Net Active
Directory classes? COM errors occur when I try to use them. I
can't even use the static method DirectoryEntry.Exists without a COM
Exception (Unknown Error). This is with Visual Studio 2005. I also have
2003 and 2008 installed, would that make a difference?



My System SpecsSystem Spec
Old 12-13-2008   #2 (permalink)
Michel Posseth [MCP]


 
 

Re: .Net Directory Classes give COM errors


Hello "Waldy"

I use these classes with success in my environments ( VS 2005 and 2008 ) so
my first guesses are that
1. it is a user rights issue
2. AD is not setup correctly on the network

regards

Michel



"Waldy" <someonenot@xxxxxx> schreef in bericht
news:OLA8Hm5WJHA.2016@xxxxxx
Quote:

> Hi there,
> does anyone have any idea why I cannot use the .Net Active
> Directory classes? COM errors occur when I try to use them. I
> can't even use the static method DirectoryEntry.Exists without a COM
> Exception (Unknown Error). This is with Visual Studio 2005. I also have
> 2003 and 2008 installed, would that make a difference?
>
>

My System SpecsSystem Spec
Old 12-13-2008   #3 (permalink)
Michel Posseth [MCP]


 
 

Re: .Net Directory Classes give COM errors


Here is a class i wrote that works perfectly on our network in both
environments , it might just get you started

Imports System.DirectoryServices

Public Class ADQuery

Implements IDisposable

Private _DirEntry As DirectoryEntry

Public Property DirEntry() As DirectoryEntry

Get

Return _DirEntry

End Get

Private Set(ByVal value As DirectoryEntry)

_DirEntry = value

End Set

End Property

''' <summary>

''' Initializes a new instance of the <see cref="ADQuery" /> class.

''' </summary>

''' <param name="DomainName">Name of the domain.</param>

Public Sub New(ByVal DomainName As String)

DirEntry = New DirectoryEntry(String.Concat("LDAP://", DomainName)) 'connect
to direrctory

DirSearch = New DirectorySearcher(DirEntry)





End Sub

Private _DirSearch As DirectorySearcher

Public Property DirSearch() As DirectorySearcher

Get

Return _DirSearch

End Get

Private Set(ByVal value As DirectorySearcher)

_DirSearch = value

End Set

End Property

''' <summary>

''' Mails the adress from LDAP.

''' </summary>

''' <param name="sAMAccountName">Name of the s AM account.</param>

''' <returns></returns>

Public Function MailAdressFromLDAP(ByVal sAMAccountName As String) As String

Dim ret As String = String.Empty 'declare ret var

DirSearch.Filter = String.Concat("(&(objectClass=user)(sAMAccountName=",
sAMAccountName, "))") 'filter relevant info

Dim oResult As SearchResult = DirSearch.FindOne

ret = oResult.GetDirectoryEntry().Properties("mail").Value.ToString

Return ret ' return the retvar

End Function

Public Structure stFullName

''' <summary>

'''

''' </summary>

Dim FirstName As String

Dim LastName As String

End Structure

''' <summary>

''' Fulls the name from LDAP.

''' </summary>

''' <param name="sAMAccountName">Name of the s AM account.</param>

''' <returns></returns>

Public Function FullNameFromLDAP(ByVal sAMAccountName As String) As
stFullName

Dim Ret As stFullName = Nothing

DirSearch.Filter = String.Concat("(&(objectClass=user)(sAMAccountName=",
sAMAccountName, "))") 'filter relevant info

Dim oResult As SearchResult = DirSearch.FindOne

With Ret

Try

..FirstName =
oResult.GetDirectoryEntry().Properties("givenName").Value.ToString

..LastName = oResult.GetDirectoryEntry().Properties("sn").Value.ToString

Catch ex As Exception

End Try

End With

Return Ret

End Function

#Region " IDisposable Support "

Private disposedValue As Boolean = False ' To detect redundant calls

''' <summary>

''' Releases unmanaged and - optionally - managed resources

''' </summary>

''' <param name="disposing"><c>true</c> to release both managed and
unmanaged resources; <c>false</c> to release only unmanaged
resources.</param>

Protected Overridable Sub Dispose(ByVal disposing As Boolean)

If Not Me.disposedValue Then

If disposing Then

If DirEntry IsNot Nothing Then

DirEntry.Dispose()

End If

If DirSearch IsNot Nothing Then

DirSearch.Dispose()

End If

' TODO: free other state (managed objects).

End If

' TODO: free your own state (unmanaged objects).

' TODO: set large fields to null.

End If

Me.disposedValue = True

End Sub

''' <summary>

''' Performs application-defined tasks associated with freeing, releasing,
or resetting unmanaged resources.

''' </summary>

Public Sub Dispose() Implements IDisposable.Dispose

' Do not change this code. Put cleanup code in Dispose(ByVal disposing As
Boolean) above.

Dispose(True)

GC.SuppressFinalize(Me)

End Sub

#End Region

End Class

C# Version


using System.DirectoryServices;
public class ADQuery : IDisposable
{
private DirectoryEntry _DirEntry;
public DirectoryEntry DirEntry {
get { return _DirEntry; }
private set { _DirEntry = value; }
}

/// <summary>
/// Initializes a new instance of the <see cref="ADQuery" /> class.
/// </summary>
/// <param name="DomainName">Name of the domain.</param>
public ADQuery(string DomainName)
{
DirEntry = new DirectoryEntry(string.Concat("LDAP://", DomainName));
//connect to direrctory
DirSearch = new DirectorySearcher(DirEntry);



}
private DirectorySearcher _DirSearch;
public DirectorySearcher DirSearch {
get { return _DirSearch; }
private set { _DirSearch = value; }
}
/// <summary>
/// Mails the adress from LDAP.
/// </summary>
/// <param name="sAMAccountName">Name of the s AM account.</param>
/// <returns></returns>
public string MailAdressFromLDAP(string sAMAccountName)
{
string ret = string.Empty;
//declare ret var
DirSearch.Filter =
string.Concat("(&(objectClass=user)(sAMAccountName=", sAMAccountName, "))");
//filter relevant info
SearchResult oResult = DirSearch.FindOne;
ret = oResult.GetDirectoryEntry().Properties("mail").Value.ToString;
return ret;
// return the retvar
}
public struct stFullName
{
/// <summary>
///
/// </summary>
public string FirstName;
public string LastName;
}
/// <summary>
/// Fulls the name from LDAP.
/// </summary>
/// <param name="sAMAccountName">Name of the s AM account.</param>
/// <returns></returns>
public stFullName FullNameFromLDAP(string sAMAccountName)
{
stFullName Ret = null;
DirSearch.Filter =
string.Concat("(&(objectClass=user)(sAMAccountName=", sAMAccountName, "))");
//filter relevant info
SearchResult oResult = DirSearch.FindOne;
{
try {
Ret.FirstName =
oResult.GetDirectoryEntry().Properties("givenName").Value.ToString;
Ret.LastName =
oResult.GetDirectoryEntry().Properties("sn").Value.ToString;
}
catch (Exception ex) {
}

}
return Ret;
}
#region " IDisposable Support "
private bool disposedValue = false;
// To detect redundant calls
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and
unmanaged resources; <c>false</c> to release only unmanaged
resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue) {
if (disposing) {
if (DirEntry != null) {
DirEntry.Dispose();
}
if (DirSearch != null) {
DirSearch.Dispose();
}
}
// TODO: free other state (managed objects).
}

// TODO: free your own state (unmanaged objects).
// TODO: set large fields to null.
this.disposedValue = true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing,
releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(true);
GC.SuppressFinalize(this);
}
}
#endregion








regards

Michel




"Michel Posseth [MCP]" <MSDN@xxxxxx> schreef in bericht
news:ed2wIWWXJHA.4596@xxxxxx
Quote:

>
> Hello "Waldy"
>
> I use these classes with success in my environments ( VS 2005 and 2008 )
> so my first guesses are that
> 1. it is a user rights issue
> 2. AD is not setup correctly on the network
>
> regards
>
> Michel
>
>
>
> "Waldy" <someonenot@xxxxxx> schreef in bericht
> news:OLA8Hm5WJHA.2016@xxxxxx
Quote:

>> Hi there,
>> does anyone have any idea why I cannot use the .Net Active
>> Directory classes? COM errors occur when I try to use them. I
>> can't even use the static method DirectoryEntry.Exists without a COM
>> Exception (Unknown Error). This is with Visual Studio 2005. I also have
>> 2003 and 2008 installed, would that make a difference?
>>
>>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to enumerate classes from a DLL .NET General
Directory Errors, 90 picture folders General Discussion
how to load .net classes PowerShell
RC1 and WMI - mising classes? Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46