Beispiel #1
0
        public uint GetDCName(
            string domainFQDN,
            uint flags,
            out CNetlogon.LWNET_DC_INFO DCInfo
            )
        {
            CNetlogon.LWNET_DC_INFO info = new CNetlogon.LWNET_DC_INFO();
            DCInfo = info;
            IntPtr pDCInfo = IntPtr.Zero;
            uint error = 0;

            try
            {

                Logger.Log(String.Format("GetDCName({0}, {1}) called",
                    domainFQDN,
                    flags),
                    Logger.NetlogonLogLevel);

                error = NetlogonAPI.LWNetGetDCName(
                            null,
                            domainFQDN,
                            null,
                            flags,
                            out pDCInfo);

                DCInfo = (CNetlogon.LWNET_DC_INFO)Marshal.PtrToStructure(pDCInfo, typeof(CNetlogon.LWNET_DC_INFO));

                Logger.Log(String.Format("GetDCName() returned {0}, error={1}",
                    CNetlogon.LWNETDCInfoToString(DCInfo), error),
                    Logger.NetlogonLogLevel);

            }

            catch (Exception e)
            {
                Logger.LogException("Netlogon.GetDCName", e);
                if (error == 0)
                {
                    error = CNetlogon.Definitions.LWNET_ERROR_INTEROP_EXCEPTION;
                }
            }

            return error;
        }
        public uint GetDCName(
            string domainFQDN,
            uint flags,
            out CNetlogon.LWNET_DC_INFO DCInfo
            )
        {
            uint error = 0;
            CNetlogon.LWNET_DC_INFO info = new CNetlogon.LWNET_DC_INFO();
            NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO DCInfoWindows;
            DCInfo = info;
            IntPtr pDCInfo = IntPtr.Zero;

            try
            {

                Logger.Log(String.Format("GetDCNameWindows({0}, {1}) called",
                    domainFQDN,
                    flags),
                    Logger.NetlogonLogLevel);

                error = (uint)NetlogonWindowsAPI.DsGetDcName(
                            "",
                            "",
                            IntPtr.Zero,
                            "",
                            (int)flags,
                            out pDCInfo);

                if (error != 0)
                {
                    return (uint)error;
                }

                DCInfoWindows = (NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCInfo, typeof(NetlogonWindowsAPI.DOMAIN_CONTROLLER_INFO));

                Logger.Log(String.Format("DsGetDcName() returned {0}, error={1}",
                    DCINFOToString(DCInfoWindows), error),
                    Logger.NetlogonLogLevel);

                DCInfo.pucDomainGUID = new byte[CNetlogon.Definitions.LWNET_GUID_SIZE];

                DCInfo.DomainControllerName = DCInfoWindows.DomainControllerName;
                DCInfo.DomainControllerAddress = DCInfoWindows.DomainControllerAddress;
                DCInfoWindows.DomainGuid.ToByteArray().CopyTo(DCInfo.pucDomainGUID, 0);
                DCInfo.FullyQualifiedDomainName = DCInfoWindows.DomainName;
                DCInfo.DnsForestName = DCInfoWindows.DnsForestName;
                DCInfo.Flags = (uint)DCInfoWindows.Flags;
                DCInfo.DCSiteName = DCInfoWindows.DcSiteName;
                DCInfo.ClientSiteName = DCInfoWindows.ClientSiteName;


                if (DCInfo.DomainControllerName.StartsWith(@"\\"))
                {
                    DCInfo.DomainControllerName = DCInfo.DomainControllerName.Substring(2);
                }

                if (DCInfo.DomainControllerAddress.StartsWith(@"\\"))
                {
                    DCInfo.DomainControllerAddress = DCInfo.DomainControllerAddress.Substring(2);
                }

                Logger.Log(String.Format("GetDCNameWindows() returning {0}, error={1}",
                    CNetlogon.LWNETDCInfoToString(DCInfo), error),
                    Logger.LogLevel.Debug);


                error = NetlogonWindowsAPI.NetApiBufferFree(pDCInfo);

                if (error != 0)
                {
                    Logger.Log("NetApiBufferFree() in GetDCNameWindows() failed!",
                        Logger.LogLevel.Error);
                }
            }
            catch (Exception e)
            {
                Logger.LogException("Netlogon.GetDCName", e);
                if (error == 0)
                {
                    error = CNetlogon.Definitions.LWNET_ERROR_INTEROP_EXCEPTION;
                }
                if (!IntPtr.Zero.Equals(pDCInfo))
                {
                    uint error2 = NetlogonWindowsAPI.NetApiBufferFree(pDCInfo);
                    if (error2 != 0)
                    {
                        Logger.Log("Netlogon.DCName: NetAPiBufferFree failed: " + error2);
                    }
                }
            }

            return error;
        }
        public uint GetDomainController(
            string domainFQDN,
            out string domainControllerFQDN
            )
        {
            domainControllerFQDN = null;
            uint error = 0;

            CNetlogon.LWNET_DC_INFO DCInfo = new CNetlogon.LWNET_DC_INFO();

            error = GetDCName(domainFQDN, 0, out DCInfo);

            if (error == 0 &&
               !String.IsNullOrEmpty(DCInfo.DomainControllerName))
            {
                domainControllerFQDN = DCInfo.DomainControllerName;
            }

            return error;
        }