VB and VBA Users Source Code: Determining the name of the Primary Domain Controller (PDC)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determining the name of the Primary Domain Controller (PDC)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, July 31, 2001
Hits:
1334
Category:
Networks
Article:
The following function returns the Primary Domain Controller name (Domain Name): Option Explicit Private Declare Function NetGetDCName Lib "netapi32.dll" (strServerName As Any, strDomainName As Any, pBuffer As Long) As Long Private Declare Function NetApiBufferFree Lib "netapi32.dll" (Buffer As Any) As Long Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) 'Purpose : Returns the name of the Primary Domain Controller for the local machine. 'Inputs : N/A 'Outputs : If succeeds returns the domain name else returns an empty string 'Author : Andrew Baker 'Date : 13/Jul/2001 'Notes : Only supported on Windows NT/2000 (Requires Windows NT 3.1 or later) Function DomainName() As String Const clMaxBuffer As Long = 256 Dim szServer As String Dim lptrBuffer As Long, lRetVal As Long Dim abResults() As Byte '# this will return nothing if the machine is not in a domain lRetVal = NetGetDCName(vbNullString, vbNullString, lptrBuffer) If lRetVal = 0 Then ' success ReDim abResults(clMaxBuffer) 'Copy contents of pointer to buffer MoveMemory abResults(0), lptrBuffer, clMaxBuffer 'Free buffer lRetVal = NetApiBufferFree(lptrBuffer) If lRetVal = 0 Then 'Clean result szServer = abResults DomainName = Left$(szServer, InStr(szServer, vbNullChar) - 1) End If Else 'Failed DomainName = "" End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder