Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: NT
Expertise: Beginner
Oct 25, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning


I'm trying to get the PDC name for a domain on my network using the following code. It works fine within the VB IDE, but the moment I compile it and run it as an executable, it errors out with either
NERR_DCNotFound(2453): Could not find domain controller for this domain
ERROR_BAD_NETPATH(53): The network path was not found
Any idea what's going wrong?

Here's my code:

Private Declare Function NetGetDCName Lib "NETAPI32.DLL" _
   (ServerName As Byte, DomainName As Byte, DCName As Long) As Long

Private Declare Function lstrcpyW Lib _
"kernel32.dll" (bRet As Byte, ByVal _
lPtr As Long) As Long

Private Declare Function NetApiBufferFree Lib _
"Netapi32" (ByVal pBuffer As Long) As Long

Public Function GetPrimaryDCName(ByVal DName As String) As String

Dim DCName As String, DCNPtr As Long
Dim DNArray() As Byte, DCNArray(100) As Byte
Dim result As Long
DNArray = DName & vbNullChar
result = NetGetDCName(0&, DNArray(0), DCNPtr)

If result <> 0 Then
  Err.Raise vbObjectError + 4000, "CNetworkInfo", result
  Exit Function
End If
lstrcpyW DCNArray(0), DCNPtr
result = NetApiBufferFree(DCNPtr)
DCName = DCNArray()
GetPrimaryDCName = Left(DCName, InStr(DCName, Chr(0)) - 1)

End Function

That is very odd. I could not get your code to fail on a W2K machine, but it did fail as an executable under NT 4.0. I solved the problem by changing the declare as follows:

Private Declare Function NetGetDCName _
   Lib "NETAPI32.DLL" _
   (ServerName As Any, _
    DomainName As Any, _
    ppBuffer As Long) As Long
DevX Pro
Thanks for your registration, follow us on our social networks to keep up-to-date