How to extract out the domain name of given URL?
There are few ways to do it. Microsoft Win32 Internet API
provides InternetCrackUrl. Look at example below:
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#pragma comment( lib, "wininet" )
BOOL GetHostName(LPCTSTR pszURL, LPTSTR pszHost, int cbHostBuf)
{
URL_COMPONENTS uc;
memset(&uc, 0, sizeof(uc));
uc.dwStructSize = sizeof(uc);
uc.lpszHostName = pszHost;
uc.dwHostNameLength = cbHostBuf;
return InternetCrackUrl(pszURL, lstrlen(pszURL), ICU_DECODE, &uc);
}
int main(int argc, char* argv[], char *envp[])
{
LPCTSTR pszURL = _T("http://www.microsoft.com/msdownload/platformsdk/setuplauncher.asp");
TCHAR szHost[1024];
szHost[0] = NULL;
if( GetHostName(pszURL, szHost, sizeof(szHost)/sizeof(TCHAR)) )
{
OutputDebugString(szHost);
}
return 0;
}
Shell provides nice Shell Lightweight Utility APIs. UrlGetPart function performs URL parsing. The following sample needs in latest Microsoft Platform
SDK - http://www.microsoft.com/msdownload/platformsdk/setuplauncher.asp
#include <windows.h>
#include <shlwapi.h>
#include <tchar.h>
#pragma comment( lib, "shlwapi" )
int main(int argc, char* argv[], char *envp[])
{
LPCTSTR pszURL = _T("http://www.microsoft.com/msdownload/platformsdk/setuplauncher.asp");
TCHAR szHost[1024];
szHost[0] = NULL;
DWORD cbHostBuf = sizeof(szHost)/sizeof(TCHAR);
// extract hostname
if( SUCCEEDED( UrlGetPart(pszURL, szHost, &cbHostBuf, URL_PART_HOSTNAME, NULL) ) )
{
OutputDebugString(szHost);
OutputDebugString(_T("\n"));
}
return 0;
}
Revision:1, Last Modified: 05/17/01 16:30, by
Vadim Melnik.
Visit my
Homepage.