LPWSTR Util::DomainFromIP(LPWSTR machine) { PWKSTA_INFO_100 workInfo; std::string retString; std::wstring hostString; LPWSTR siteName; DWORD ret; DSROLE_PRIMARY_DOMAIN_INFO_BASIC *buffer; //ZeroMemory(retString, sizeof(retString)); if(machine[0] != L'\\' && machine[1] != L'\\') { hostString.append(L"\\\\"); } hostString.append(machine); DsRoleGetPrimaryDomainInformation(machine, DsRolePrimaryDomainInfoBasic, (LPBYTE *)&buffer); retString.append(Util::AsciiString(buffer->DomainNameFlat)); // Well apparently my "useless" (according to me prior) conversion functions have // come in handy and fixed the aforementioned problem with returning a free'd string. return Util::WideString(retString.c_str()); //return buffer->DomainNameDns; }
char * GetDomainName(void) { static char *DomainName = NULL; PDSROLE_PRIMARY_DOMAIN_INFO_BASIC pDSRoleInfo; DWORD netret; if ((netret = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic, (PBYTE *) & pDSRoleInfo) == ERROR_SUCCESS)) { /* * Check the machine role. */ if ((pDSRoleInfo->MachineRole == DsRole_RoleMemberWorkstation) || (pDSRoleInfo->MachineRole == DsRole_RoleMemberServer) || (pDSRoleInfo->MachineRole == DsRole_RoleBackupDomainController) || (pDSRoleInfo->MachineRole == DsRole_RolePrimaryDomainController)) { size_t len = wcslen(pDSRoleInfo->DomainNameFlat); /* allocate buffer for str + null termination */ safe_free(DomainName); DomainName = (char *) xmalloc(len + 1); if (DomainName == NULL) return NULL; /* copy unicode buffer */ WideCharToMultiByte(CP_ACP, 0, pDSRoleInfo->DomainNameFlat, -1, DomainName, len, NULL, NULL); /* add null termination */ DomainName[len] = '\0'; /* * Member of a domain. Display it in debug mode. */ debug("Member of Domain %s\n", DomainName); debug("Into forest %S\n", pDSRoleInfo->DomainForestName); } else { debug("Not a Domain member\n"); } } else debug("DsRoleGetPrimaryDomainInformation Error: %ld\n", netret); /* * Free the allocated memory. */ if (pDSRoleInfo != NULL) DsRoleFreeMemory(pDSRoleInfo); return DomainName; }