1, the role of Ipconfig/flushdns
Ipconfig/flushdns This is used to clear the DNS cache. When accessing a Web site, the system will read the IP address of the domain name from the DNS cache, and find the Hosts file in the system when it is not found, and if not, then request a DNS query from the DNS server, and the DNS server will return the IP corresponding to the domain name. After your system receives the resolution address, it will use that IP address for access, while the resolution is cached in the local DNS cache.
If the DNS address cannot be resolved, or if the address in the DNS cache is incorrect, Ipconfig/flushdns is generally used to clear all DNS caches.
2, by invoking the API function to implement the command line command
By the form of the command, it is known that/flushdns should be passed as a parameter to the Ipconfig.exe executable program. If you want to implement the command, you can use the system () function to pass the IPCONFIG/FLUSHDNS as a parameter, and the execution of the function is affected by the environment variable, and there may be a problem with the actual use. You can also consider using ShellExecute to start Ipconfig.exe, and pass/flushdns as a parameter, but there are also problems, such as the possibility of interception of anti-virus software, for Win7, Win8 system, may be warned because of the problem of the UAV permissions.
So to see if there are any corresponding API functions available, the direct call API is the most effective and secure. So I tried to search in MSDN, but I couldn't find the relevant function. Since it is passing/flushdns parameter execution Ipconfig.exe, then try to use VC's own depends tool to see if you can find the corresponding interface. It turns out that the DNSAPI.DLL in its dependencies is called the Dnsflushresolvercache function, as shown in the following figure:
This function should be Non-public API function, that can use LoadLibrary display load, and getprocess get function pointer, directly call it. By searching, get the prototype declaration of the function:
BOOL winapi dnsflushresolvercache (VOID);
The code that explicitly loads the call Dnsflushresolvercache from the DNSAPI.DLL dynamic library looks like this:
BOOL __stdcall Dnsflushresolvercache ()
{
bool bRet = FALSE;
typedef BOOL (WINAPI *pfuncdnsflushresolvercache) (VOID);
Hmodule hdnsmodule = LoadLibrary (_t ("Dnsapi.dll"));
if (hdnsmodule!= NULL)
{
Pfuncdnsflushresolvercache pflushfun = GetProcAddress (Hdnsmodule, " Dnsflushresolvercache ");
if (Pflushfun!= NULL)
{
pflushfun ();
BRet = TRUE;
}
FreeLibrary (Hdnsmodule);
}
return bRet;
}
3, by invoking the API function to implement the command line command
The terminal was unable to log on to the platform because of a system failure today. In order to solve this problem, the network of the platform is switched from Unicom network to telecom network, and the result shows that some terminals can log in and some terminals cannot log in. Considering that it may be caused by the system DNS cache, the address of the domain name resolved by the terminal side is always the previous Unicom address (that is, the address in the cache), so it has been unable to log in. Then use the Ipconfig/flushdns command to clean up the system's DNS cache to log on normally. To circumvent this problem caused by network switching and DNS caching, consider automatically cleaning up the system's DNS cache after multiple logon failures. Directly execute the Dnsflushresolvercache function found above.