C

Get Domain or Workgroup with C

You will need to include the libraries netapi32.lib and iphlpapi.lib into your project.

DsRoleGetPrimaryDomainInformation – retrieves state data for the computer. This data includes the state of the directory service installation and domain data.

DsRoleFreeMemory – frees memory allocated by the DsRoleGetPrimaryDomainInformation function.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dsrole.h>
 
void GetDomain(){
  DWORD retval;
  DSROLE_PRIMARY_DOMAIN_INFO_BASIC *info;
 
  retval = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic, (PBYTE *)&info);
 
  if(retval == ERROR_SUCCESS){
	  wprintf(L"Domain forest name: %s\n", info->DomainForestName); //null if not joined to a domain
	  wprintf(L"Domain DNS name: %s\n", info->DomainNameDns); //null if not joined to a domain
	  wprintf(L"Domain/Workgroup name: %s\n", info->DomainNameFlat);
  }
 
  if (info != NULL)
    DsRoleFreeMemory(info);
}
 
int main(void) {
	GetDomain();
	return EXIT_SUCCESS;
}