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 LongPrivate Declare Function lstrcpyW Lib _"kernel32.dll" (bRet As Byte, ByVal _lPtr As Long) As LongPrivate Declare Function NetApiBufferFree Lib _"Netapi32" (ByVal pBuffer As Long) As LongPublic Function GetPrimaryDCName(ByVal DName As String) As StringDim DCName As String, DCNPtr As LongDim DNArray() As Byte, DCNArray(100) As ByteDim result As LongDNArray = DName & vbNullCharresult = NetGetDCName(0&, DNArray(0), DCNPtr)If result <> 0 Then  Err.Raise vbObjectError + 4000, "CNetworkInfo", result  Exit FunctionEnd If lstrcpyW DCNArray(0), DCNPtrresult = 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

See also  10 Best Apps to Help You Read More Books in Less Time

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist