CrAzY hOuSe
Locos por la programación
Obtiene el Nombre del Controlar de Dominio Primario para un Dominio Especificado o Estación de Trabajo
.net

Syndication

Este código va en el Formulario.

Option Explicit
Private Sub Form_Click()
    MsgBox GetPDCName("", "")
End Sub

Y este otro en un módulo.

Option Explicit

' La función NetGetDCName obtiene el nombre del Control Primario de Dominio (PDC) 
' de un dominio especificado.
Private Declare Function NetGetDCName Lib "netapi32" (ServerName As Any, DomainName As Any, _
            lpBuffer As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal pBuffer As Long) As Long

Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, _
            ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long

Private Const NERR_Success As Long = 0&

Private Function PtrToString(lpwString As Long) As String
    ' Convierte el puntero en una cadena
    Dim Buffer() As Byte
    Dim nLen As Long

    If lpwString Then
        nLen = lstrlenW(lpwString) * 2
        If nLen Then
            ReDim Buffer(0 To (nLen - 1)) As Byte
            CopyMem Buffer(0), ByVal lpwString, nLen
            PtrToString = Buffer
        End If
    End If
End Function
Public Function GetPDCName(ComputerName As String, DomainName As String) As String
    ' ComputerName
    ' Contiene el nombre del servidor remoto, devuelve NULL si es el ordenador local.

    'DomainnNme
    ' Contiene el nombre del dominio, devuelve NULL indicando que la función devuelve el
    ' nombre Controlador de Dominio Primario.

    Dim bComputer() As Byte
    Dim bDomain() As Byte
    Dim ret As Long
    Dim lpBuffer As Long
    Dim s As String

    If Trim(ComputerName) = "" Then
        'Local users
        bComputer = vbNullChar
    Else
        'Check the syntax of the ServerName string
        If InStr(ComputerName, "\") = 1 Then
            bComputer = ComputerName & vbNullChar
        Else
            bComputer = "\" & ComputerName & vbNullChar
        End If
    End If
    If Trim(DomainName) = "" Then
        'Default Domain
        bDomain = vbNullChar
    Else
        bDomain = DomainName & vbNullChar
    End If
    ret = NetGetDCName(bComputer(0), bDomain(0), lpBuffer)
    If ret = NERR_Success And lpBuffer Then
        s = PtrToString(lpBuffer)
    End If
    If lpBuffer Then
        Call NetApiBufferFree(lpBuffer)
    End If
    GetPDCName = s
End Function

Posted jue, ago 24 2000 19:16 by Maverick
Filed under: ,

Add a Comment

(optional)  
(optional)
(required)  
Remember Me?
MavericK
Powered by Community Server (Non-Commercial Edition), by Telligent Systems