import { sspi, sysinfo, adsi } from '../../lib/api'; import { IADsContainer, IDirectorySearch, IDispatch } from '../../lib/adsi'; import { openADConnection, closeADConnection } from './adConnection'; /** * Get the domain (Microsoft domain) or hostname (workgroup) of this machine. * * @returns {string} domain name */ export function getDefaultDomain(): string { const str = sspi.GetUserNameEx('NameSamCompatible'); const domain = str.split('\\')[0]; return domain; } /** * Want to know if your computer has joined a Microsoft Windows domain ? * * @export * @returns {boolean} true if this computer joined a domain, false otherwise. */ export function isOnDomain(): boolean { return sysinfo.GetComputerNameEx('ComputerNameDnsDomain').length > 0; } export function isActiveDirectoryReachable(): boolean { let gc: IADsContainer | undefined; let element: IDispatch | undefined; let ds: IDirectorySearch | undefined; try { openADConnection(); gc = adsi.ADsOpenObjectSync({ binding: 'GC:', riid: 'IID_IADsContainer', }); element = gc.Next(); ds = element.QueryInterface('IID_IDirectorySearch'); } catch (e) { return false; } finally { ds!.Release(); element?.Release(); gc?.Release(); closeADConnection(); } return true; }