Getting the Host Domain Name
Topic Last Modified: 2006-06-12
When you use The File: URL Scheme to identify the location of items in the Exchange store, you need the fully qualified domain name of the host. The following programming task demonstrates how to retrieve the domain name of the host on which the code is running.
To increase performance, you should retrieve this name once, store it, and then use the stored version when constructing a URL. For example, if you are writing an ASP application, retrieve the domain name and then place this information in the ASP Application collection for subsequent use by other scripts.
Example
VBScript
Example
Function GetDomainName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetDomainName = Info.DomainDNSName
End Function
Function GetComputerName()
Dim InfoNT
Set InfoNT = CreateObject("WinNTSystemInfo")
GetComputerName = lcase(InfoNT.ComputerName)
End Function
Function GetForestDNSName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetForestDNSName = Info.ForestDNSName
End Function
Visual Basic
Function GetDomainName()
Dim Info As New ADSystemInfo
GetDomainName = Info.DomainDNSName
End Function
Function GetComputerName()
Dim InfoNT As New WinNTSystemInfo
GetComputerName = LCase(InfoNT.ComputerName)
End Function
Function GetForestDNSName()
Dim Info As New ADSystemInfo
GetForestDNSName = Info.ForestDNSName
End Function
Visual C++
#define _WIN32_WINNT 0x0500
#define _UNICODE
#define UNICODE
#include <windows.h>
#include <comdef.h>
#define BUF_SIZE 4096
bstr_t getHostDomainName() {
HRESULT hr = S_OK;
BSTR domainName;
DWORD dwSize = 0;
wchar_t* pBuf = new wchar_t[BUF_SIZE];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsDomain,
pBuf,
&dwSize)) {
if(GetLastError() == ERROR_MORE_DATA) {
delete [] pBuf;
pBuf = new wchar_t[dwSize+1];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsDomain, pBuf, &dwSize) ) {
hr = HRESULT_FROM_WIN32(GetLastError());
delete [] pBuf;
pBuf = NULL;
_com_issue_error(hr);
}
}
else
_com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
}
domainName = SysAllocString(pBuf);
delete [] pBuf;
return bstr_t(domainName, false);
}
#define _WIN32_WINNT 0x0500
#define _UNICODE
#define UNICODE
#include <windows.h>
#include <comdef.h>
#define BUF_SIZE 4096
bstr_t getHostName() {
HRESULT hr = S_OK;
BSTR hostName;
DWORD dwSize = 0;
wchar_t* pBuf = new wchar_t[BUF_SIZE];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsHostname,
pBuf,
&dwSize)) {
if(GetLastError() == ERROR_MORE_DATA) {
delete [] pBuf;
pBuf = new wchar_t[dwSize+1];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsHostname, pBuf, &dwSize) ) {
hr = HRESULT_FROM_WIN32(GetLastError());
delete [] pBuf;
pBuf = NULL;
_com_issue_error(hr);
}
}
else
_com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
}
hostName = SysAllocString(pBuf);
delete [] pBuf;
return bstr_t(hostName, false);
}
#define _WIN32_WINNT 0x0500
#define _UNICODE
#define UNICODE
#include <windows.h>
#include <comdef.h>
#define BUF_SIZE 4096
bstr_t getFQDName() {
HRESULT hr = S_OK;
BSTR fqdname;
DWORD dwSize = 0;
wchar_t* pBuf = new wchar_t[BUF_SIZE];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsFullyQualified,
pBuf,
&dwSize)) {
if(GetLastError() == ERROR_MORE_DATA) {
delete [] pBuf;
pBuf = new wchar_t[dwSize+1];
if(pBuf == NULL)
_com_issue_error(E_OUTOFMEMORY);
if(!GetComputerNameEx((COMPUTER_NAME_FORMAT)ComputerNameDnsFullyQualified, pBuf, &dwSize) ) {
hr = HRESULT_FROM_WIN32(GetLastError());
delete [] pBuf;
pBuf = NULL;
_com_issue_error(hr);
}
}
else
_com_issue_error(HRESULT_FROM_WIN32(GetLastError()));
}
fqdname = SysAllocString(pBuf);
delete [] pBuf;
return bstr_t(fqdname, false);
}