GetPrimaryDCName

Question:
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

or

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

Answer:
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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS