@Override
 public DomainControllerInfo getDomainJoinInfo() {
   try {
     int joinStatus = Netapi32Util.getJoinStatus();
     if (WinNetSetupJoinStatus.NET_SETUP_DOMAIN_NAME.getStatus() == joinStatus) {
       return getDcInfo(null);
     } else {
       throw new HostNotJoinedException("Local host is not joined.");
     }
   } catch (HostNotJoinedException e) {
     throw e;
   } catch (Win32Exception e) {
     throw new DomainManagerException("Could not get domain join info" + e.getMessage());
   }
 }
  private DomainControllerInfo getDcInfo(String domainName, boolean bForceRediscovery) {
    DomainControllerInfo domainInfo = null;

    int dwError = 0;
    WinDcInfoNative dcInfo = null;
    PointerByReference ppDcInfo = new PointerByReference(Pointer.NULL);
    Pointer pDcInfo = Pointer.NULL;

    WinDcInfoNative dcInfo_flat = null;
    PointerByReference ppDcInfo_flat = new PointerByReference(Pointer.NULL);
    Pointer pDcInfo_flat = Pointer.NULL;

    try {
      dwError =
          WinNetApi32.INSTANCE.DsGetDcName(
              null,
              domainName,
              null,
              null,
              bForceRediscovery
                  ? PlatformUtils.getFlagsForGetDcInfoWithRediscovery()
                  : PlatformUtils.getFlagsForGetDcInfo(),
              ppDcInfo);
      logAndThrow(
          String.format("Failed to get domain controller information for %s ", domainName),
          dwError);

      pDcInfo = ppDcInfo.getValue();
      if (pDcInfo == Pointer.NULL) {
        throw new DomainManagerException(
            String.format("Failed to get domain controller information for %s", domainName));
      }

      try {
        dcInfo = new WinDcInfoNative(pDcInfo);
      } catch (Exception e) {
        throw new DomainManagerException(
            String.format("Failed to get domain controller information for %s", domainName));
      }

      dwError =
          WinNetApi32.INSTANCE.DsGetDcName(
              null,
              domainName,
              null,
              null,
              PlatformUtils.getFlagsForGetFlatDcInfo(),
              ppDcInfo_flat);
      logAndThrow(
          String.format("Failed to get domain controller information for %s ", domainName),
          dwError);

      pDcInfo_flat = ppDcInfo_flat.getValue();
      if (pDcInfo_flat == Pointer.NULL) {
        throw new DomainManagerException(
            String.format("Failed to get domain controller information for %s", domainName));
      }

      try {
        dcInfo_flat = new WinDcInfoNative(pDcInfo_flat);
      } catch (Exception e) {
        throw new DomainManagerException(
            String.format("Failed to get domain controller information for %s", domainName));
      }

      if (dcInfo != null && dcInfo_flat != null) {
        domainInfo =
            new DomainControllerInfo(
                dcInfo.domainName,
                dcInfo_flat.domainName,
                dcInfo.domainControllerAddress,
                dcInfo.domainControllerName,
                dcInfo.dnsForestName);
      }

      return domainInfo;

    } catch (Win32Exception e) {
      String errMsg =
          String.format(
              "Failed to get domain controller information for %s",
              (domainName == null || domainName.length() == 0) ? "local host domain" : domainName);
      logger.error(errMsg);
      throw new DomainManagerException(errMsg + e.getMessage());
    } finally {
      if (pDcInfo != Pointer.NULL) {
        WinNetApi32.INSTANCE.NetApiBufferFree(pDcInfo);
      }
      if (pDcInfo_flat != Pointer.NULL) {
        WinNetApi32.INSTANCE.NetApiBufferFree(pDcInfo_flat);
      }
    }
  }