概述:windows 操作系统下获取主机域名|获取域名|获取域中域控制器名称
API 1 DsRoleGetPrimaryDomainInformation
主要调用 DsRoleGetPrimaryDomainInformation
这个函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <Windows.h> #include <DSRole.h>
#pragma comment(lib, "netapi32.lib")
#include <stdio.h>
int main(int argc, char ** argv) { DSROLE_PRIMARY_DOMAIN_INFO_BASIC * info; DWORD dw;
dw = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic, (PBYTE *)&info); if (dw != ERROR_SUCCESS) { wprintf(L"DsRoleGetPrimaryDomainInformation: %u\n", dw); return dw; }
if (info->DomainNameDns == NULL) { wprintf(L"DomainNameDns is NULL\n"); } else { wprintf(L"DomainNameDns: %s\n", info->DomainNameDns); }
system("pause");
return 0; }
|
输出示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| BOOL kull_m_net_getCurrentDomainInfo(PPOLICY_DNS_DOMAIN_INFO * pDomainInfo) { BOOL status = FALSE; LSA_HANDLE hLSA; LSA_OBJECT_ATTRIBUTES oaLsa = {0};
if(NT_SUCCESS(LsaOpenPolicy(NULL, &oaLsa, POLICY_VIEW_LOCAL_INFORMATION, &hLSA))) { status = NT_SUCCESS(LsaQueryInformationPolicy(hLSA, PolicyDnsDomainInformation, (PVOID *) pDomainInfo)); LsaClose(hLSA); } return status; }
|
获取域控制器名称
调用 API 为 DsGetDcName
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| BOOL kull_m_net_getDC(LPCWSTR fullDomainName, DWORD altFlags, LPWSTR * fullDCName) { BOOL status = FALSE; DWORD ret, size; PDOMAIN_CONTROLLER_INFO cInfo = NULL; ret = DsGetDcName(NULL, fullDomainName, NULL, NULL, altFlags | DS_IS_DNS_NAME | DS_RETURN_DNS_NAME, &cInfo); if(ret == ERROR_SUCCESS) { size = (DWORD) (wcslen(cInfo->DomainControllerName + 2) + 1) * sizeof(wchar_t); if(*fullDCName = (wchar_t *) LocalAlloc(LPTR, size)) { status = TRUE; RtlCopyMemory(*fullDCName, cInfo->DomainControllerName + 2, size); } NetApiBufferFree(cInfo); } else PRINT_ERROR(L"DsGetDcName: %u\n", ret); return status; }
LPWSTR szDc = NULL; LPCWSTR szDomain = NULL; if(kull_m_net_getDC("test.com", DS_DIRECTORY_SERVICE_REQUIRED, &szTmpDc)) szDc = szTmpDc;
|