iptime 공유기에서 설정해놓은 ddns로 공유기의 유동ip를 가져오는 방법을 찾던 중에
DnsQuery() 라는 함수를 통해 가져올 수 있을것 같다는 판단하에 삽질 실행...
DNS_STATUS WINAPI DnsQuery(
_In_ PCTSTR lpstrName,
_In_ WORD wType,
_In_ DWORD Options,
_Inout_opt_ PVOID pExtra,
_Out_opt_ PDNS_RECORD *ppQueryResultsSet,
_Out_opt_ PVOID *pReserved
);
Parameters
lpstrName [in]
A pointer to a string that represents the DNS name to query.
wType [in]
A value that represents the Resource Record (RR) DNS Record Type that is queried. wType determines the format of data pointed to by ppQueryResultsSet. For example, if the value of wType is DNS_TYPE_A, the format of data pointed to by ppQueryResultsSet is DNS_A_DATA.
Options [in]
A value that contains a bitmap of DNS Query Options to use in the DNS query. Options can be combined and all options override DNS_QUERY_STANDARD.
pExtra [in, out, optional]
This parameter is reserved for future use and must be set to NULL.
ppQueryResultsSet [out, optional]
Optional. A pointer to a pointer that points to the list of RRs that comprise the response. For more information, see the Remarks section.
pReserved [out, optional]
This parameter is reserved for future use and must be set to NULL.
Return value
Returns success confirmation upon successful completion. Otherwise, returns the appropriate DNS-specific error code as defined in Winerror.h.
프로젝트 추가
include <WinDNS.h>
include Ws2_32.lib Dnsapi.lib
코드
// Calling function DnsQuery to query Host or PTR records
status = DnsQuery( pOwnerName, //Pointer to OwnerName.
DNS_TYPE_A, //Type of the record to be queried.
DNS_QUERY_STANDARD,//Standard query.
pSrvList, //Contains DNS server IP address.
&pDnsRecord, //Resource record that contains the esponse.
NULL); //Reserved for future use.
if (status) // DnsQuery Fail
{
if(wType == DNS_TYPE_A)
{
// printf("Failed to query the host record for %s and the error is %d \n", pOwnerName, status);
AtlMessageBox(m_hWnd,L"DNS_TYPE_A Fail",L"Fail 1",MB_OK | MB_ICONSTOP);
}
else
{
// printf("Failed to query the PTR record and the error is %d \n", status);
AtlMessageBox(m_hWnd,L"DNS_TYPE Fail",L"Fail 2",MB_OK | MB_ICONSTOP);
}
}
else
{
if(wType == DNS_TYPE_A) // Dns Name -> IP Address
{
//convert the Internet network address into a string
//in Internet standard dotted format.
ipaddr.S_un.S_addr = (pDnsRecord->Data.A.IpAddress);
//printf("The IP address of the host %s is %s \n", pOwnerName,inet_ntoa(ipaddr));
char stAddr[MAX_PATH]={0,};
strcpy(stAddr, inet_ntoa(ipaddr)); // inet_ntoa(ipaddr) -> Ansi로 리턴
int nLen = 0;
BSTR buf = NULL; // 유니코드 문자열 저장 버퍼
nLen = MultiByteToWideChar(CP_ACP, 0, stAddr, strlen(stAddr), NULL, NULL); // 변환된 후의 문자열의 길이를 확인
buf = SysAllocStringLen(NULL, nLen); // 필요한 길이만큼 메모리 할당
MultiByteToWideChar(CP_ACP, 0, stAddr, strlen(stAddr), buf, nLen); // 문자 변환
CString cStrAddress = (LPCWSTR)buf; // BSTR to CString
_ip_Temp = htonl(inet_addr((CStringA)cStrAddress)); // CString to const char*(DWORD)
/*
//CString -> const char*
CString str1 = _T("aaa");
const char* str2;
str2 = (CStringA) str1;
//const char* -> CString
const char* str1 = "aaa";
CString str2;
str2 = (CString)str1;
*/
//////////////////////////////////////////////////////////////////////////
// Free memory allocated for DNS records.
DnsRecordListFree(pDnsRecord, freetype);
}
else
{
//printf("The host name is %s \n",(pDnsRecord->Data.PTR.pNameHost));
CString cstrTemp;
cstrTemp.Format(L"The host name is %s", (pDnsRecord->Data.PTR.pNameHost));
// Free memory allocated for DNS records.
DnsRecordListFree(pDnsRecord, freetype);
}
}
LocalFree(pSrvList);
테스트 결과...
는 나중에... ㄷㄷㄷ