Re: .Net Directory Classes give COM errors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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@xxxxxxxxxxx> schreef in bericht
news:ed2wIWWXJHA.4596@xxxxxxxxxxxxxxxxxxxxxxx

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@xxxxxxxxxxxxx> schreef in bericht
news:OLA8Hm5WJHA.2016@xxxxxxxxxxxxxxxxxxxxxxx
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?






.



Relevant Pages

  • YUI--Competent?
    ... @param attr The attribute to get. ... ret = el.getAttribute; ... they change it to an empty string per some imagined "DOM spec". ... unit testing and ostensibly have the best JS developers working around ...
    (comp.lang.javascript)
  • Re: very simple Genetic Algorithm completed
    ... Change mutate to build a list instead of a string. ... the optimization of successive string concatentions is only ... Include psyco to cut execution time in half. ... ret = evalcache ...
    (comp.lang.python)
  • Re: [Full-disclosure] [NETRAGARD-20060810 SECURITY ADVISORY] [HP Tru64 dtmail Unchecked Buff
    ... A typical stack address is something like: ... So yes, you have nulls, but at the end of the string :-) ... SHELLCODE RET] ...
    (Full-Disclosure)
  • Re: how to replace a substring in a string using C?
    ... loop variable within a loop, even though it is also modified by ... > char *ret; ... To give an idea of the impact of string parsing missteps can make on ... char *ret, *r; ...
    (comp.lang.c)
  • Re: Access Violation Exception Error
    ... I'm trying to communicate with a TEC Controller Newport 350B using their ... Declare Function InitSystem _ ... ByVal response As String, _ ... Dim ret As Integer ...
    (microsoft.public.dotnet.languages.vb)