📄 simplednsclient.cpp
字号:
}
STDMETHODIMP CSimpleDNSClient::GetInterfaceSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
{
ATLTRACE(_T("CObjectSafetyImpl::GetInterfaceSafetyOptions\n"));
if (!pdwSupportedOptions || !pdwEnabledOptions) {
return E_FAIL;
}
LPUNKNOWN pUnk = NULL;
if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE) {
// Our object doesn't even support this interface.
return E_NOINTERFACE;
} else {
// Cleanup after ourselves.
pUnk->Release();
pUnk = NULL;
}
if (riid == IID_IDispatch) {
// IDispatch is an interface used for scripting. If your
// control supports other IDispatch or Dual interfaces, you
// may decide to add them here as well. Client wants to know
// if object is safe for scripting. Only indicate safe for
// scripting when the interface is safe.
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
*pdwEnabledOptions = m_dwCurrentSafety &
INTERFACESAFE_FOR_UNTRUSTED_CALLER;
return S_OK;
} else if ((riid == IID_IPersistStreamInit) ||
(riid == IID_IPersistStorage)) {
// IID_IPersistStreamInit and IID_IPersistStorage are
// interfaces used for Initialization. If your control
// supports other Persistence interfaces, you may decide to
// add them here as well. Client wants to know if object is
// safe for initializing. Only indicate safe for initializing
// when the interface is safe.
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
*pdwEnabledOptions = m_dwCurrentSafety &
INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK;
} else {
// We are saying that no other interfaces in this control are
// safe for initializing or scripting.
*pdwSupportedOptions = 0;
*pdwEnabledOptions = 0;
return E_FAIL;
}
}
STDMETHODIMP CSimpleDNSClient::SetInterfaceSafetyOptions(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions)
{
ATLTRACE(_T("CObjectSafetyImpl::SetInterfaceSafetyOptions\n"));
if (!dwOptionSetMask && !dwEnabledOptions) {
return E_FAIL;
}
LPUNKNOWN pUnk = NULL;
if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE) {
// Our object doesn't even support this interface.
return E_NOINTERFACE;
} else {
// Cleanup after ourselves.
pUnk->Release();
pUnk = NULL;
}
// Store our current safety level to return in
// GetInterfaceSafetyOptions
m_dwCurrentSafety |= dwEnabledOptions & dwOptionSetMask;
if ((riid == IID_IDispatch) &&
(m_dwCurrentSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER)) {
// Client wants us to disable any functionality that would
// make the control unsafe for scripting. The same applies to
// any other IDispatch or Dual interfaces your control may
// support. Because our control is safe for scripting by
// default we just return S_OK.
return S_OK;
} else if (((riid == IID_IPersistStreamInit) ||
(riid == IID_IPersistStorage)) &&
(m_dwCurrentSafety & INTERFACESAFE_FOR_UNTRUSTED_DATA)) {
// Client wants us to make the control safe for initializing
// from persistent data. For these interfaces, this control
// is safe so we return S_OK. For Any interfaces that are not
// safe, we would return E_FAIL.
return S_OK;
} else {
// This control doesn't allow Initialization or Scripting
// from any other interfaces so return E_FAIL.
return E_FAIL;
}
}
/////////////////////////////////////////////////////////////////////////////
// Protected methods
/////////////////////////////////////////////////////////////////////////////
CString CSimpleDNSClient::FindAllDNSServers()
{
CString szDNSAllServers;
HKEY hKey = NULL;
LONG nResult = S_OK;
CString szServerKeys[] = {
"NameServer",
"DhcpNameServer"
};
// Find DNS servers with IP Helper API (function GetNetworkParams)
// CAUTION: this code should run on Windows 2000 and Windows Me only
PFIXED_INFO pNetInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
ULONG nNetInfoSize = sizeof(FIXED_INFO);
DWORD nErrorCode = GetNetworkParams(pNetInfo, &nNetInfoSize);
if (nErrorCode == ERROR_BUFFER_OVERFLOW) {
// OK, buffer is too small but function returns the needed size
GlobalFree(pNetInfo);
pNetInfo = NULL;
pNetInfo = (FIXED_INFO *)GlobalAlloc(GPTR, nNetInfoSize);
nErrorCode = GetNetworkParams(pNetInfo, &nNetInfoSize);
}
if (nErrorCode == ERROR_SUCCESS) {
IP_ADDR_STRING *oDNSServers = &(pNetInfo->DnsServerList);
szDNSAllServers += oDNSServers->IpAddress.String;
while (oDNSServers->Next) {
oDNSServers = oDNSServers->Next;
szDNSAllServers += " ";
szDNSAllServers += oDNSServers->IpAddress.String;
}
}
if (szDNSAllServers.GetLength() == 0) {
// Find DNS from Registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
// CAUTION: this code runs on NT4 only, but it's a good fallback
nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",
0,
KEY_READ,
&hKey
);
if (nResult == ERROR_SUCCESS) {
for (int i=0; i < sizeof(szServerKeys)/sizeof(szServerKeys[0]); i++) {
DWORD dwType;
BYTE lpData[256];
DWORD dwDataSize = sizeof(lpData);
nResult = RegQueryValueEx(hKey,
szServerKeys[i],
NULL,
&dwType,
lpData,
&dwDataSize
);
if (nResult == ERROR_SUCCESS) {
TCHAR lpszData[256];
memcpy((void *)lpszData, (const void *)lpData, dwDataSize);
lpszData[dwDataSize] = '\0';
szDNSAllServers = lpszData;
} else {
szDNSAllServers = "";
}
}
} else {
szDNSAllServers = "";
}
}
// Cleanup
if (hKey) {
RegCloseKey(hKey);
hKey = NULL;
}
if (pNetInfo) {
GlobalFree(pNetInfo);
pNetInfo = NULL;
}
return(szDNSAllServers);
}
CString CSimpleDNSClient::FindFirstDNSServer()
{
CString szDNSFirstServer;
CString szDNSAllServers = FindAllDNSServers();
if (szDNSAllServers != "") {
// Servers are separated by spaces
int nPosition = szDNSAllServers.Find(" ", 0);
if (nPosition!=-1) {
szDNSFirstServer = szDNSAllServers.Left(nPosition);
} else {
szDNSFirstServer = szDNSAllServers;
}
}
return(szDNSFirstServer);
}
HRESULT CSimpleDNSClient::SetError(LPCTSTR lpszErrorMessage, DWORD dwLastError)
{
CString szErrorMessage = lpszErrorMessage;
USES_CONVERSION;
if (dwLastError != 0) {
TCHAR lpszSystemErrorText[1024];
szErrorMessage+=WindowsGetErrorText(dwLastError, lpszSystemErrorText, sizeof(lpszSystemErrorText));
}
TRACE1("%s\n", szErrorMessage);
// Build COM error info object
ICreateErrorInfo *pcerrinfo;
IErrorInfo *perrinfo;
HRESULT hr;
hr = CreateErrorInfo(&pcerrinfo);
// Fill error info object
pcerrinfo->SetDescription(T2OLE(szErrorMessage));
pcerrinfo->SetSource(T2OLE("SITA.SimpleTelnetClientX"));
pcerrinfo->SetGUID(IID_ISimpleDNSClient);
// Set error info object for current thread
hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo);
if (SUCCEEDED(hr))
{
SetErrorInfo(0, perrinfo);
perrinfo->Release();
}
pcerrinfo->Release();
return(S_OK);
}
BOOL CSimpleDNSClient::AppendResult(int nRequestedType, int nType, LPCTSTR lpszResult, CString &szTotalResult)
{
if (nType == nRequestedType || nRequestedType == DNS_TYPE_ANY) {
if (szTotalResult == "") {
szTotalResult = lpszResult;
} else {
// Append name (using separator property to separate entries)
szTotalResult +=m_szSeparator;
szTotalResult += lpszResult;
}
}
return(TRUE);
}
STDMETHODIMP CSimpleDNSClient::GetEmailServers(BSTR pBDomainName, VARIANT *pvEmailServerNames)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
_bstr_t BDomainName = pBDomainName;
_variant_t vFoundNames;
HRESULT hResult = S_OK;
// Set parameters, if default
if (BDomainName == _bstr_t("")) {
_variant_t vLocalDomainName;
hResult = GetDNSDomain(&vLocalDomainName);
if (FAILED(hResult) || _bstr_t(vLocalDomainName) == _bstr_t("")) {
SetError("Invalid parameter: empty domain name and cannot read default domain: ", hResult);
return(hResult);
} else {
BDomainName = vLocalDomainName;
}
}
// Simply call the resolve method with class INternet and type Mail eXchanger
hResult = Resolve(BDomainName, &vFoundNames, _bstr_t("C_IN"), _bstr_t("T_MX"));
// Set output variable
if (pvEmailServerNames) {
*pvEmailServerNames = vFoundNames.Detach();
}
return(hResult);
}
STDMETHODIMP CSimpleDNSClient::get_ServerAddresses(BSTR *pVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
if (pVal) {
*pVal = _bstr_t((LPCTSTR)m_szServerAddresses);
}
return S_OK;
}
STDMETHODIMP CSimpleDNSClient::put_ServerAddresses(BSTR newVal)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
m_szServerAddresses = (LPCTSTR)bstr_t(newVal);
return S_OK;
}
BOOL CSimpleDNSClient::BuildListFromString(LPCTSTR lpszStringWithSeparators, CStringList &oList)
{
CString szStringWithSeparators = lpszStringWithSeparators;
TCHAR cSeparator = ',';
int nPos = -1;
oList.RemoveAll();
int nCurrentPos = 0;
int nCurrentLength = 0;
CString szCurrentItem;
nPos = szStringWithSeparators.Find(cSeparator);
if (nPos == -1) {
// Try semi-colon
cSeparator = ';';
nPos = szStringWithSeparators.Find(cSeparator);
if (nPos == -1) {
// Try space
cSeparator = ' ';
nPos = szStringWithSeparators.Find(cSeparator);
}
}
while (nPos != -1) {
nCurrentLength = nPos - nCurrentPos;
szCurrentItem = szStringWithSeparators.Mid(nCurrentPos, nCurrentLength);
// Remove starting/trailing spaces
szCurrentItem.TrimLeft(" ");
szCurrentItem.TrimRight(" ");
if (szCurrentItem != "") {
TRACE1("Adding string \"%s\"\n", szCurrentItem);
oList.AddTail(szCurrentItem);
}
nCurrentPos = nPos+1;
nPos = szStringWithSeparators.Find(cSeparator, nCurrentPos);
}
// Add last item
szCurrentItem = szStringWithSeparators.Mid(nCurrentPos);
// Remove starting/trailing spaces
szCurrentItem.TrimLeft(" ");
szCurrentItem.TrimRight(" ");
if (szCurrentItem != "") {
TRACE1("Adding string \"%s\"\n", szCurrentItem);
oList.AddTail(szCurrentItem);
}
return(!oList.IsEmpty());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -