Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform "nslookup host server"

Tags:

c#

api

nslookup

My C# service needs to periodically poll nslookup host server. Currently it spawns a Process that executes batch script. Due to performance reason I'm considering to do this check using some API. But the problem is, that using, for example, System.Net.Dns.GetHostAddresses I can only emulate nslookup host check, but not nslookup host server (without seconds param).

I've looked at bunch of similar SO questions, but none of them seem to solve my issue.

Are there any way to perform nslookup host server in C# without using some heavy third-patry library?

like image 751
Roman Avatar asked Mar 29 '11 05:03

Roman


2 Answers

The problem was solved!

http://msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx

See "Great Example here" section

[DllImport("dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);
[DllImport("dnsapi", CharSet=CharSet.Auto, SetLastError=true)]
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

...
like image 93
Roman Avatar answered Sep 19 '22 12:09

Roman


I looked into this a while back. It is not possible in the standard class libraries, so you are going to have to use an external component to do this properly.

There are a number of free and paid choices available to you. My implementation was based around a posting on CodeProject, which worked quite well. DNS Client Library for .NET (also mentioned by kprobst) was released after I finished mine, or I would've used this one initially.

like image 45
Richard Avatar answered Sep 19 '22 12:09

Richard