[Sample of Mar 20th] Set DNS SRV Record entry in Domain Naming Server

 

Homepage image
image RSS Feed

Sample Download: https://code.msdn.microsoft.com/CppCreateDNSRecord-9edac3c1

Today’s code sample demonstrates how to set the DNS SRV Record entry in the Domain Naming Server via C++. We are using here WMI’s class MicrosoftDNS_SRVType under ROOT\MicrosoftDNS namespace.  This namespace is provided by Microsoft to manage DNS.  This application takes the advantages of WMI classes and executes the method CreateInstanceFromPropertydata.

The sample was written by Microsoft Support Escalation Engineer - Shaleen Thapa.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

 

Introduction

This sample application demonstrates how to set the DNS SRV Record entry in the Domain Naming Server via C++. We are using here WMI’s class MicrosoftDNS_SRVType under ROOT\MicrosoftDNS namespace.

This namespace is provided by Microsoft to manage DNS.  This application takes the advantages of WMI classes and executes the method CreateInstanceFromPropertydata.

To know more about SRV Records, please visit https://technet.microsoft.com/en-us/library/cc742513.aspx & https://technet.microsoft.com/en-us/library/cc772362.aspx

Running the Sample

In order to execute this sample code, we need a DNS Server which is in a domain. You must execute this application under Domain Administrator privileges as this requires domain admin privileges to update DNS SRV records.

Once we have this DNS Server, we need to know what are the following parameters value in domain:

  • DNS Server Name
  • Container Name
  • Owner Name
  • Priority
  • Weight
  • Port
  • Domain Name

 

Using the Code

Since this is a C++ Application, we need to include certain header files: comdef.h for COM & Wbemidl.h for WMI. In the code, first we need to initialize COM by calling (CoInitializeEx). Once the COM is initialized we need to set the COM general security calling (CoInitializeSecurity). Then we require to get the WMI Locator in order to connect to a WMI service by calling (CoCreateInstance) & (IWbemServices::ConnectServer). While connecting to the WMI service we need to connect to the ROOT\MicrosoftDNS namespace. This namespace gives us the facilities to work with the Domain Naming Server. After we obtain the connection to the WMI service we would require to setting the proxy setting via CoSetProxyBlanket, make sure we use the impersonation level as RPC_C_IMP_LEVEL_IMPERSONATE.

For creating the DNS record we have a class called MicrosoftDNS_SRVType which has a method called CreateInstanceFromPropertydata. This methods takes certain important parameters as described below:

DnsServerName => This takes the FQDN of the DNS Server

ContainerName => FQDN of domain controller or called Container

OwnerName => Should be the New alias name, in FQDN format

RecordClass => It should be the record class, generally passed as 0

TTL => It must be 0 while creating the DNS SVR Record

Priority => Priority can be defined as per your need, generally it is 0

Weight => As Priority, Weight can be as per your requirement.

Port       => This will be the port on which DNS SRV Record will work.

DomainName => This should be the FQDN of the Domain.

Once we set the above parameters, we will execute CreateInstanceFromPropertydata method with the help of IWbemServices::ExecMethod.This method returns the HRESULT and if successful we need to release all the pointers and must exit out gracefully.

 

More Information

For more information on:

·         Working on WMI via C++:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa394558(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/aa389762(v=vs.85).aspx

·         Microsoft DNS Namespace & Classes:

https://technet.microsoft.com/en-us/library/dd197491(WS.10).aspx

.