public bool CanGmsaBeUsedOnThisMachine(string samAccountName) { var result = NetApi32.NetQueryServiceAccount(null, samAccountName, 0, out NetApi32.SafeNetApiBuffer buffer); result.ThrowIfFailed(); MsaInfo0 msaInfo = buffer.ToStructure <MsaInfo0>(); this.logger.LogTrace($"NetQueryServiceAccount returned {msaInfo.State}"); return(msaInfo.State == MsaInfoState.MsaInfoInstalled); }
private Dictionary <string, IPAddress> LoadNetworkDevices() { Dictionary <string, IPAddress> host_to_ip = new Dictionary <string, IPAddress>(); ArrayList s = NetApi32.GetServerList(NetApi32.SV_101_TYPES.SV_TYPE_WORKSTATION); foreach (object c in s) { string host = ((NetApi32.SERVER_INFO_101)c).sv101_name; host_to_ip.Add(host, MainClient.GetIP(host)); } return(host_to_ip); }
public static string[] GetShares(string hostName) { var sharePtr = IntPtr.Zero; string[] shares; try { int errorCode, entriesRead = 0, totalEntries = 0, resumeHandle = 0; if ((errorCode = NetApi32.NetShareEnum( new StringBuilder(hostName), 0, ref sharePtr, uint.MaxValue, ref entriesRead, ref totalEntries, ref resumeHandle)) != 0) { throw new Win32Exception(errorCode); } var structSize = Marshal.SizeOf <SHARE_INFO_0>(); //var shareInfo = Marshal.PtrToStructure<SHARE_INFO_0>(sharePtr); shares = Enumerable .Range(0, entriesRead) .Select(x => Marshal .PtrToStructure <SHARE_INFO_0>(sharePtr + (x * structSize)) .shi0_netname) .ToArray(); } finally { if (sharePtr != IntPtr.Zero) { NetApi32.NetApiBufferFree(sharePtr); } } return(shares); }
public void DsQuerySitesByCostTest() { var err = DsBindToISTG(null, out var hDs); Assert.That(err, Is.EqualTo(Win32Error.ERROR_SUCCESS)); err = NetApi32.DsGetSiteName(null, out var hSiteName); Assert.That(err, Is.EqualTo(Win32Error.ERROR_SUCCESS)); var site = hSiteName.ToString(); var sites = GetAllSiteNames(); err = DsQuerySitesByCost(hDs, site, sites, (uint)sites.Length, 0, out var hnr); Assert.That(err, Is.EqualTo(Win32Error.ERROR_SUCCESS)); var nrs = hnr.GetItems(sites.Length); Assert.That(nrs.Length, Is.EqualTo(sites.Length)); var nr = nrs[0]; Assert.That(nr.cost, Is.Not.Null); for (var i = 0; i < sites.Length; i++) { TestContext.WriteLine($"{sites[i]}: {(nrs[i].errorCode == Win32Error.ERROR_SUCCESS ? nrs[i].cost.ToString() : "Not found")}"); } }
public static List<SERVER_INFO_101> GetServerList(NetApi32.SV_101_TYPES ServerType) { int entriesread = 0, totalentries = 0; List<SERVER_INFO_101> alServers = new List<SERVER_INFO_101>(); do { // Buffer to store the available servers // Filled by the NetServerEnum function IntPtr buf = new IntPtr(); SERVER_INFO_101 server; int ret = NetServerEnum(null, 101, out buf, -1, ref entriesread, ref totalentries, ServerType, null, IntPtr.Zero); // if the function returned any data, fill the tree view if (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA || entriesread > 0) { IntPtr ptr = buf; for (int i = 0; i < entriesread; i++) { // cast pointer to a SERVER_INFO_101 structure server = (SERVER_INFO_101)Marshal.PtrToStructure(ptr, typeof(SERVER_INFO_101)); //Cast the pointer to a ulong so this addition will work on 32-bit or 64-bit systems. ptr = (IntPtr)((ulong)ptr + (ulong)Marshal.SizeOf(server)); // add the machine name and comment to the arrayList. //You could return the entire structure here if desired alServers.Add(server); } } // free the buffer NetApiBufferFree(buf); } while ( entriesread < totalentries && entriesread != 0 ); try { alServers = alServers.OrderBy(item => item.sv101_comment).ToList<SERVER_INFO_101>(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return alServers; }
public string GetComputerSiteNameManual(string dnsDomain, string dnsHostName) { this.logger.LogTrace("Attempting to query IP addresses for {host}", dnsHostName); IPAddress[] ipAddresses; try { ipAddresses = Dns.GetHostAddresses(dnsHostName); } catch (Exception ex) { this.logger.LogTrace(ex, "Unable to resolve DNS entry for {host}", dnsHostName); return(null); } if (ipAddresses.Length == 0) { return(null); } this.logger.LogTrace("Host {host} has addresses {addresses}", dnsHostName, string.Join(", ", ipAddresses.Select(t => t.ToString()))); List <Ws2_32.SOCKADDR> resolvedAddresses = new List <Ws2_32.SOCKADDR>(); Win32Error result; foreach (IPAddress ipAddress in ipAddresses.OrderBy(t => (int)t.AddressFamily)) { Ws2_32.SOCKADDR socketAddress; if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { socketAddress = new Ws2_32.SOCKADDR_IN(); } else if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { socketAddress = new Ws2_32.SOCKADDR_IN6(); } else { this.logger.LogTrace("Ignoring unknown address type {ipAddressFamily}", ipAddress.AddressFamily.ToString()); continue; } int length = socketAddress.Size; string addr = ipAddress.ToString(); result = Ws2_32.WSAStringToAddress(addr, (Ws2_32.ADDRESS_FAMILY)ipAddress.AddressFamily, IntPtr.Zero, socketAddress, ref length); if (result.Failed) { this.logger.LogTrace(result.GetException(), "WSAStringToAddress failed"); } resolvedAddresses.Add(socketAddress); } if (resolvedAddresses.Count == 0) { return(null); } var socketAddresses = new Ws2_32.SOCKET_ADDRESS[resolvedAddresses.Count]; for (int i = 0; i < resolvedAddresses.Count; i++) { socketAddresses[i].iSockaddrLength = resolvedAddresses[i].Size; socketAddresses[i].lpSockaddr = resolvedAddresses[i].DangerousGetHandle(); } NetApi32.SafeNetApiBuffer siteNames = null; this.FindDcAndExecuteWithRetry(dnsDomain, dc => { result = NetApi32.DsAddressToSiteNames(dc, (uint)socketAddresses.Length, socketAddresses, out siteNames); result.ThrowIfFailed("DsAddressToSiteNames failed"); }); if (siteNames == null || siteNames.IsInvalid) { return(null); } List <string> sites = siteNames.ToStringEnum(resolvedAddresses.Count).ToList(); string site = sites.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t)); if (site == null) { this.logger.LogTrace("There was no site found for host {host} in domain {domain}", dnsHostName, dnsDomain); return(null); } this.logger.LogTrace("Selecting site {site} from site list {sites}", site, string.Join(", ", sites)); return(site); }